|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +type AppSetting = { |
| 4 | + darkMode: 'auto' | 'dark' | 'light'; |
| 5 | + contentMode: 'compact' | 'full'; |
| 6 | + fileTreeOpen: boolean; |
| 7 | + outlineOpen: boolean; |
| 8 | +}; |
| 9 | + |
| 10 | +type MockStore = { |
| 11 | + getState: jest.Mock; |
| 12 | + subscribe: jest.Mock; |
| 13 | +}; |
| 14 | + |
| 15 | +const createStore = (appSetting: AppSetting) => { |
| 16 | + let listener: (() => void) | undefined; |
| 17 | + const store: MockStore = { |
| 18 | + getState: jest.fn(() => ({ appSetting })), |
| 19 | + subscribe: jest.fn((callback: () => void) => { |
| 20 | + listener = callback; |
| 21 | + return jest.fn(); |
| 22 | + }), |
| 23 | + }; |
| 24 | + |
| 25 | + return { store, trigger: () => listener?.() }; |
| 26 | +}; |
| 27 | + |
| 28 | +const loadEntryPoint = async (store: MockStore) => { |
| 29 | + const render = jest.fn(); |
| 30 | + const createRoot = jest.fn(() => ({ render })); |
| 31 | + |
| 32 | + jest.resetModules(); |
| 33 | + document.body.innerHTML = '<div id="root"></div>'; |
| 34 | + |
| 35 | + jest.doMock('react-dom/client', () => ({ |
| 36 | + __esModule: true, |
| 37 | + default: { createRoot }, |
| 38 | + createRoot, |
| 39 | + })); |
| 40 | + jest.doMock('react-redux', () => ({ |
| 41 | + __esModule: true, |
| 42 | + Provider: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
| 43 | + })); |
| 44 | + jest.doMock('react-router-dom', () => ({ |
| 45 | + __esModule: true, |
| 46 | + BrowserRouter: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
| 47 | + })); |
| 48 | + jest.doMock('../../src/App', () => ({ |
| 49 | + __esModule: true, |
| 50 | + default: () => <div>App</div>, |
| 51 | + })); |
| 52 | + jest.doMock('../../src/store/store', () => ({ |
| 53 | + __esModule: true, |
| 54 | + store, |
| 55 | + })); |
| 56 | + |
| 57 | + await jest.isolateModulesAsync(async () => { |
| 58 | + await import('../../src/index'); |
| 59 | + }); |
| 60 | + |
| 61 | + return { createRoot, render }; |
| 62 | +}; |
| 63 | + |
| 64 | +describe('index', () => { |
| 65 | + beforeEach(() => { |
| 66 | + jest.restoreAllMocks(); |
| 67 | + localStorage.clear(); |
| 68 | + document.body.innerHTML = ''; |
| 69 | + }); |
| 70 | + |
| 71 | + it('renders the app and persists app settings on store updates', async () => { |
| 72 | + const appSetting = { |
| 73 | + darkMode: 'dark', |
| 74 | + contentMode: 'full', |
| 75 | + fileTreeOpen: false, |
| 76 | + outlineOpen: true, |
| 77 | + } as const; |
| 78 | + const setItemSpy = jest.spyOn(Storage.prototype, 'setItem'); |
| 79 | + const { store, trigger } = createStore(appSetting); |
| 80 | + const { createRoot, render } = await loadEntryPoint(store); |
| 81 | + |
| 82 | + expect(createRoot).toHaveBeenCalledWith(document.getElementById('root')); |
| 83 | + expect(render).toHaveBeenCalledTimes(1); |
| 84 | + expect(store.subscribe).toHaveBeenCalledWith(expect.any(Function)); |
| 85 | + |
| 86 | + trigger(); |
| 87 | + |
| 88 | + expect(setItemSpy).toHaveBeenCalledWith('appSetting', JSON.stringify(appSetting)); |
| 89 | + }); |
| 90 | + |
| 91 | + it('logs an error when app settings cannot be persisted', async () => { |
| 92 | + const error = new Error('save failed'); |
| 93 | + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 94 | + jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { |
| 95 | + throw error; |
| 96 | + }); |
| 97 | + const { store, trigger } = createStore({ |
| 98 | + darkMode: 'light', |
| 99 | + contentMode: 'compact', |
| 100 | + fileTreeOpen: true, |
| 101 | + outlineOpen: false, |
| 102 | + }); |
| 103 | + |
| 104 | + await loadEntryPoint(store); |
| 105 | + trigger(); |
| 106 | + |
| 107 | + expect(consoleErrorSpy).toHaveBeenCalledWith('Could not save state to localStorage', error); |
| 108 | + }); |
| 109 | +}); |
0 commit comments