diff options
Diffstat (limited to 'packages/main/src/controllers/__tests__/initConfig.spec.ts')
-rw-r--r-- | packages/main/src/controllers/__tests__/initConfig.spec.ts | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/packages/main/src/controllers/__tests__/initConfig.spec.ts b/packages/main/src/controllers/__tests__/initConfig.spec.ts index dc00b9d..fdd22c9 100644 --- a/packages/main/src/controllers/__tests__/initConfig.spec.ts +++ b/packages/main/src/controllers/__tests__/initConfig.spec.ts | |||
@@ -20,16 +20,15 @@ | |||
20 | 20 | ||
21 | import { jest } from '@jest/globals'; | 21 | import { jest } from '@jest/globals'; |
22 | import { mocked } from 'jest-mock'; | 22 | import { mocked } from 'jest-mock'; |
23 | import { getSnapshot } from 'mobx-state-tree'; | ||
24 | import ms from 'ms'; | 23 | import ms from 'ms'; |
25 | 24 | ||
26 | import type ConfigPersistence from '../../infrastructure/ConfigPersistence'; | 25 | import type ConfigPersistence from '../../infrastructure/ConfigPersistence'; |
27 | import { Config, config as configModel } from '../../stores/Config'; | 26 | import { sharedStore, SharedStore } from '../../stores/SharedStore'; |
28 | import type Disposer from '../../utils/Disposer'; | 27 | import type Disposer from '../../utils/Disposer'; |
29 | import { silenceLogger } from '../../utils/log'; | 28 | import { silenceLogger } from '../../utils/log'; |
30 | import initConfig from '../initConfig'; | 29 | import initConfig from '../initConfig'; |
31 | 30 | ||
32 | let config: Config; | 31 | let store: SharedStore; |
33 | const persistenceService: ConfigPersistence = { | 32 | const persistenceService: ConfigPersistence = { |
34 | readConfig: jest.fn(), | 33 | readConfig: jest.fn(), |
35 | writeConfig: jest.fn(), | 34 | writeConfig: jest.fn(), |
@@ -44,7 +43,7 @@ beforeAll(() => { | |||
44 | }); | 43 | }); |
45 | 44 | ||
46 | beforeEach(() => { | 45 | beforeEach(() => { |
47 | config = configModel.create(); | 46 | store = sharedStore.create(); |
48 | }); | 47 | }); |
49 | 48 | ||
50 | describe('when initializing', () => { | 49 | describe('when initializing', () => { |
@@ -56,7 +55,7 @@ describe('when initializing', () => { | |||
56 | }); | 55 | }); |
57 | 56 | ||
58 | it('should create a new config file', async () => { | 57 | it('should create a new config file', async () => { |
59 | await initConfig(config, persistenceService); | 58 | await initConfig(store, persistenceService); |
60 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); | 59 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); |
61 | }); | 60 | }); |
62 | 61 | ||
@@ -65,7 +64,7 @@ describe('when initializing', () => { | |||
65 | new Error('boo'), | 64 | new Error('boo'), |
66 | ); | 65 | ); |
67 | await expect(() => | 66 | await expect(() => |
68 | initConfig(config, persistenceService), | 67 | initConfig(store, persistenceService), |
69 | ).rejects.toBeInstanceOf(Error); | 68 | ).rejects.toBeInstanceOf(Error); |
70 | }); | 69 | }); |
71 | }); | 70 | }); |
@@ -76,16 +75,16 @@ describe('when initializing', () => { | |||
76 | found: true, | 75 | found: true, |
77 | data: { | 76 | data: { |
78 | // Use a default empty config file to not trigger config rewrite. | 77 | // Use a default empty config file to not trigger config rewrite. |
79 | ...getSnapshot(config), | 78 | ...store.config, |
80 | themeSource: 'dark', | 79 | themeSource: 'dark', |
81 | }, | 80 | }, |
82 | }); | 81 | }); |
83 | }); | 82 | }); |
84 | 83 | ||
85 | it('should read the existing config file is there is one', async () => { | 84 | it('should read the existing config file is there is one', async () => { |
86 | await initConfig(config, persistenceService); | 85 | await initConfig(store, persistenceService); |
87 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); | 86 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); |
88 | expect(config.themeSource).toBe('dark'); | 87 | expect(store.settings.themeSource).toBe('dark'); |
89 | }); | 88 | }); |
90 | 89 | ||
91 | it('should bail if it cannot set up a watcher', async () => { | 90 | it('should bail if it cannot set up a watcher', async () => { |
@@ -93,7 +92,7 @@ describe('when initializing', () => { | |||
93 | throw new Error('boo'); | 92 | throw new Error('boo'); |
94 | }); | 93 | }); |
95 | await expect(() => | 94 | await expect(() => |
96 | initConfig(config, persistenceService), | 95 | initConfig(store, persistenceService), |
97 | ).rejects.toBeInstanceOf(Error); | 96 | ).rejects.toBeInstanceOf(Error); |
98 | }); | 97 | }); |
99 | }); | 98 | }); |
@@ -108,7 +107,7 @@ describe('when initializing', () => { | |||
108 | }, | 107 | }, |
109 | }, | 108 | }, |
110 | }); | 109 | }); |
111 | await initConfig(config, persistenceService); | 110 | await initConfig(store, persistenceService); |
112 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); | 111 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); |
113 | }); | 112 | }); |
114 | 113 | ||
@@ -119,15 +118,15 @@ describe('when initializing', () => { | |||
119 | themeSource: -1, | 118 | themeSource: -1, |
120 | }, | 119 | }, |
121 | }); | 120 | }); |
122 | await initConfig(config, persistenceService); | 121 | await initConfig(store, persistenceService); |
123 | expect(config.themeSource).not.toBe(-1); | 122 | expect(store.settings.themeSource).not.toBe(-1); |
124 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); | 123 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); |
125 | }); | 124 | }); |
126 | 125 | ||
127 | it('should bail if it cannot determine whether there is a config file', async () => { | 126 | it('should bail if it cannot determine whether there is a config file', async () => { |
128 | mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); | 127 | mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); |
129 | await expect(() => | 128 | await expect(() => |
130 | initConfig(config, persistenceService), | 129 | initConfig(store, persistenceService), |
131 | ).rejects.toBeInstanceOf(Error); | 130 | ).rejects.toBeInstanceOf(Error); |
132 | }); | 131 | }); |
133 | }); | 132 | }); |
@@ -140,10 +139,10 @@ describe('when it has loaded the config', () => { | |||
140 | beforeEach(async () => { | 139 | beforeEach(async () => { |
141 | mocked(persistenceService.readConfig).mockResolvedValueOnce({ | 140 | mocked(persistenceService.readConfig).mockResolvedValueOnce({ |
142 | found: true, | 141 | found: true, |
143 | data: getSnapshot(config), | 142 | data: store.config, |
144 | }); | 143 | }); |
145 | mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer); | 144 | mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer); |
146 | sutDisposer = await initConfig(config, persistenceService, throttleMs); | 145 | sutDisposer = await initConfig(store, persistenceService, throttleMs); |
147 | [[configChangedCallback]] = mocked( | 146 | [[configChangedCallback]] = mocked( |
148 | persistenceService.watchConfig, | 147 | persistenceService.watchConfig, |
149 | ).mock.calls; | 148 | ).mock.calls; |
@@ -152,16 +151,16 @@ describe('when it has loaded the config', () => { | |||
152 | 151 | ||
153 | it('should throttle saving changes to the config file', () => { | 152 | it('should throttle saving changes to the config file', () => { |
154 | mocked(persistenceService.writeConfig).mockResolvedValue(); | 153 | mocked(persistenceService.writeConfig).mockResolvedValue(); |
155 | config.setThemeSource('dark'); | 154 | store.settings.setThemeSource('dark'); |
156 | jest.advanceTimersByTime(lessThanThrottleMs); | 155 | jest.advanceTimersByTime(lessThanThrottleMs); |
157 | config.setThemeSource('light'); | 156 | store.settings.setThemeSource('light'); |
158 | jest.advanceTimersByTime(throttleMs); | 157 | jest.advanceTimersByTime(throttleMs); |
159 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); | 158 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); |
160 | }); | 159 | }); |
161 | 160 | ||
162 | it('should handle config writing errors gracefully', () => { | 161 | it('should handle config writing errors gracefully', () => { |
163 | mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); | 162 | mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); |
164 | config.setThemeSource('dark'); | 163 | store.settings.setThemeSource('dark'); |
165 | jest.advanceTimersByTime(throttleMs); | 164 | jest.advanceTimersByTime(throttleMs); |
166 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); | 165 | expect(persistenceService.writeConfig).toHaveBeenCalledTimes(1); |
167 | }); | 166 | }); |
@@ -171,14 +170,14 @@ describe('when it has loaded the config', () => { | |||
171 | found: true, | 170 | found: true, |
172 | data: { | 171 | data: { |
173 | // Use a default empty config file to not trigger config rewrite. | 172 | // Use a default empty config file to not trigger config rewrite. |
174 | ...getSnapshot(config), | 173 | ...store.config, |
175 | themeSource: 'dark', | 174 | themeSource: 'dark', |
176 | }, | 175 | }, |
177 | }); | 176 | }); |
178 | await configChangedCallback(); | 177 | await configChangedCallback(); |
179 | // Do not write back the changes we have just read. | 178 | // Do not write back the changes we have just read. |
180 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); | 179 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); |
181 | expect(config.themeSource).toBe('dark'); | 180 | expect(store.settings.themeSource).toBe('dark'); |
182 | }); | 181 | }); |
183 | 182 | ||
184 | it('should update the config file if new details are added', async () => { | 183 | it('should update the config file if new details are added', async () => { |
@@ -203,7 +202,7 @@ describe('when it has loaded the config', () => { | |||
203 | }, | 202 | }, |
204 | }); | 203 | }); |
205 | await configChangedCallback(); | 204 | await configChangedCallback(); |
206 | expect(config.themeSource).not.toBe(-1); | 205 | expect(store.settings.themeSource).not.toBe(-1); |
207 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); | 206 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); |
208 | }); | 207 | }); |
209 | 208 | ||
@@ -222,7 +221,7 @@ describe('when it has loaded the config', () => { | |||
222 | }); | 221 | }); |
223 | 222 | ||
224 | it('should not listen to store changes any more', () => { | 223 | it('should not listen to store changes any more', () => { |
225 | config.setThemeSource('dark'); | 224 | store.settings.setThemeSource('dark'); |
226 | jest.advanceTimersByTime(2 * throttleMs); | 225 | jest.advanceTimersByTime(2 * throttleMs); |
227 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); | 226 | expect(persistenceService.writeConfig).not.toHaveBeenCalled(); |
228 | }); | 227 | }); |