|
1 | | -import { expect, test } from 'vitest'; |
| 1 | +import { expect, test, vi } from 'vitest'; |
2 | 2 |
|
3 | 3 | import { replenishmentWorkBooksStore } from '$lib/stores/replenishment_workbook.svelte'; |
4 | 4 |
|
| 5 | +vi.mock('$app/environment', () => ({ |
| 6 | + browser: true, |
| 7 | +})); |
| 8 | + |
5 | 9 | describe('Replenishment workbooks store', () => { |
| 10 | + const localStorageKey = 'is_shown_replenishment_workbooks'; |
| 11 | + const mockLocalStorage = { |
| 12 | + getItem: vi.fn(), |
| 13 | + setItem: vi.fn(), |
| 14 | + removeItem: vi.fn(), |
| 15 | + clear: vi.fn(), |
| 16 | + }; |
| 17 | + |
6 | 18 | beforeEach(() => { |
| 19 | + vi.clearAllMocks(); |
| 20 | + // Setup mock for localStorage |
| 21 | + vi.stubGlobal('localStorage', mockLocalStorage); |
| 22 | + |
7 | 23 | replenishmentWorkBooksStore.reset(); |
8 | 24 | }); |
9 | 25 |
|
@@ -37,4 +53,27 @@ describe('Replenishment workbooks store', () => { |
37 | 53 |
|
38 | 54 | expect(replenishmentWorkBooksStore.canView()).toBeFalsy(); |
39 | 55 | }); |
| 56 | + |
| 57 | + // Note: This test is skipped because it is not possible to mock localStorage in JSDOM. |
| 58 | + test.skip('persists state in localStorage', () => { |
| 59 | + mockLocalStorage.getItem.mockReturnValue(JSON.stringify(false)); |
| 60 | + |
| 61 | + replenishmentWorkBooksStore.toggleView(); |
| 62 | + |
| 63 | + expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(1); |
| 64 | + expect(mockLocalStorage.setItem).toHaveBeenCalledWith(localStorageKey, JSON.stringify(true)); |
| 65 | + }); |
| 66 | + |
| 67 | + test('handles SSR gracefully', () => { |
| 68 | + vi.mock('$app/environment', () => ({ |
| 69 | + browser: false, |
| 70 | + })); |
| 71 | + |
| 72 | + expect(replenishmentWorkBooksStore.canView()).toBeFalsy(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('handles invalid localStorage data', () => { |
| 76 | + localStorage.setItem(localStorageKey, 'invalid-json'); |
| 77 | + expect(replenishmentWorkBooksStore.canView()).toBeFalsy(); |
| 78 | + }); |
40 | 79 | }); |
0 commit comments