aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/controllers
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-31 01:52:28 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-31 01:56:30 +0100
commit7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f (patch)
treef8c0450a6e1b62f7e7f8470efd375b3659b91b2b /packages/main/src/controllers
parentrefactor: Install devtools extensions earlier (diff)
downloadsophie-7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f.tar.gz
sophie-7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f.tar.zst
sophie-7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f.zip
test: Add tests for main package
- Changed jest to run from the root package and reference the packages as projects. This required moving the base jest config file away from the project root. - Module isolation seems to prevent ts-jest from loading the shared package, so we disabled it for now. - To better facilitate mocking, services should be split into interfaces and implementation - Had to downgrade to chald 4.1.2 as per https://github.com/chalk/chalk/releases/tag/v5.0.0 at least until https://github.com/microsoft/TypeScript/issues/46452 is resolved.
Diffstat (limited to 'packages/main/src/controllers')
-rw-r--r--packages/main/src/controllers/__tests__/config.spec.ts184
-rw-r--r--packages/main/src/controllers/__tests__/nativeTheme.spec.ts71
-rw-r--r--packages/main/src/controllers/config.ts16
3 files changed, 263 insertions, 8 deletions
diff --git a/packages/main/src/controllers/__tests__/config.spec.ts b/packages/main/src/controllers/__tests__/config.spec.ts
new file mode 100644
index 0000000..9471ca9
--- /dev/null
+++ b/packages/main/src/controllers/__tests__/config.spec.ts
@@ -0,0 +1,184 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import { jest } from '@jest/globals';
22import { mocked } from 'jest-mock';
23import ms from 'ms';
24
25import { initConfig } from '../config';
26import type { ConfigPersistenceService } from '../../services/ConfigPersistenceService';
27import { Config, config as configModel } from '../../stores/Config';
28import { Disposer, silenceLogger } from '../../utils';
29
30let config: Config;
31let persistenceService: ConfigPersistenceService = {
32 readConfig: jest.fn(),
33 writeConfig: jest.fn(),
34 watchConfig: jest.fn(),
35};
36let lessThanThrottleMs = ms('0.1s');
37let throttleMs = ms('1s');
38
39beforeAll(() => {
40 jest.useFakeTimers();
41 silenceLogger();
42});
43
44beforeEach(() => {
45 config = configModel.create();
46});
47
48describe('when initializing', () => {
49 describe('when there is no config file', () => {
50 beforeEach(() => {
51 mocked(persistenceService.readConfig).mockResolvedValueOnce({
52 found: false,
53 });
54 });
55
56 it('should create a new config file', async () => {
57 await initConfig(config, persistenceService);
58 expect(persistenceService.writeConfig).toBeCalledTimes(1);
59 });
60
61 it('should bail if there is an an error creating the config file', async () => {
62 mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo'));
63 await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error);
64 });
65 });
66
67 describe('when there is a valid config file', () => {
68 beforeEach(() => {
69 mocked(persistenceService.readConfig).mockResolvedValueOnce({
70 found: true,
71 data: {
72 themeSource: 'dark',
73 },
74 });
75 });
76
77 it('should read the existing config file is there is one', async () => {
78 await initConfig(config, persistenceService);
79 expect(persistenceService.writeConfig).not.toBeCalled();
80 expect(config.themeSource).toBe('dark');
81 });
82
83 it('should bail if it cannot set up a watcher', async () => {
84 mocked(persistenceService.watchConfig).mockImplementationOnce(() => {
85 throw new Error('boo');
86 });
87 await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error);
88 });
89 });
90
91 it('should not apply an invalid config file', async () => {
92 mocked(persistenceService.readConfig).mockResolvedValueOnce({
93 found: true,
94 data: {
95 themeSource: -1,
96 },
97 });
98 await initConfig(config, persistenceService);
99 expect(config.themeSource).not.toBe(-1);
100 });
101
102 it('should bail if it cannot determine whether there is a config file', async () => {
103 mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo'));
104 await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error);
105 });
106});
107
108describe('when it has loaded the config', () => {
109 let sutDisposer: Disposer;
110 let watcherDisposer: Disposer = jest.fn();
111 let configChangedCallback: () => Promise<void>;
112
113 beforeEach(async () => {
114 mocked(persistenceService.readConfig).mockResolvedValueOnce({
115 found: true,
116 data: {},
117 });
118 mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer);
119 sutDisposer = await initConfig(config, persistenceService, throttleMs);
120 configChangedCallback = mocked(persistenceService.watchConfig).mock.calls[0][0];
121 jest.resetAllMocks();
122 });
123
124 it('should throttle saving changes to the config file', () => {
125 mocked(persistenceService.writeConfig).mockResolvedValue(undefined);
126 config.setThemeSource('dark');
127 jest.advanceTimersByTime(lessThanThrottleMs);
128 config.setThemeSource('light');
129 jest.advanceTimersByTime(throttleMs);
130 expect(persistenceService.writeConfig).toBeCalledTimes(1);
131 });
132
133 it('should handle config writing errors gracefully', () => {
134 mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo'));
135 config.setThemeSource('dark');
136 jest.advanceTimersByTime(throttleMs);
137 expect(persistenceService.writeConfig).toBeCalledTimes(1);
138 });
139
140 it('should read the config file when it has changed', async () => {
141 mocked(persistenceService.readConfig).mockResolvedValueOnce({
142 found: true,
143 data: {
144 themeSource: 'dark',
145 },
146 });
147 await configChangedCallback();
148 // Do not write back the changes we have just read.
149 expect(persistenceService.writeConfig).not.toBeCalled();
150 expect(config.themeSource).toBe('dark');
151 });
152
153 it('should not apply an invalid config file when it has changed', async () => {
154 mocked(persistenceService.readConfig).mockResolvedValueOnce({
155 found: true,
156 data: {
157 themeSource: -1,
158 },
159 });
160 await configChangedCallback();
161 expect(config.themeSource).not.toBe(-1);
162 });
163
164 it('should handle config writing errors gracefully', async () => {
165 mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo'));
166 await configChangedCallback();
167 });
168
169 describe('when it was disposed', () => {
170 beforeEach(() => {
171 sutDisposer();
172 });
173
174 it('should dispose the watcher', () => {
175 expect(watcherDisposer).toBeCalled();
176 });
177
178 it('should not listen to store changes any more', () => {
179 config.setThemeSource('dark');
180 jest.advanceTimersByTime(2 * throttleMs);
181 expect(persistenceService.writeConfig).not.toBeCalled();
182 });
183 });
184});
diff --git a/packages/main/src/controllers/__tests__/nativeTheme.spec.ts b/packages/main/src/controllers/__tests__/nativeTheme.spec.ts
new file mode 100644
index 0000000..cfb557c
--- /dev/null
+++ b/packages/main/src/controllers/__tests__/nativeTheme.spec.ts
@@ -0,0 +1,71 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import { jest } from '@jest/globals';
22import { mocked } from 'jest-mock';
23
24import { createMainStore, MainStore } from '../../stores/MainStore';
25import { Disposer } from '../../utils';
26
27let shouldUseDarkColors = false;
28
29jest.unstable_mockModule('electron', () => ({
30 nativeTheme: {
31 themeSource: 'system',
32 get shouldUseDarkColors() {
33 return shouldUseDarkColors;
34 },
35 on: jest.fn(),
36 off: jest.fn(),
37 },
38}));
39
40const { nativeTheme } = await import('electron');
41const { initNativeTheme } = await import('../nativeTheme');
42
43let store: MainStore;
44let disposeSut: Disposer;
45
46beforeEach(() => {
47 store = createMainStore();
48 disposeSut = initNativeTheme(store);
49});
50
51it('should register a nativeTheme updated listener', () => {
52 expect(nativeTheme.on).toBeCalledWith('updated', expect.anything());
53});
54
55it('should synchronize themeSource changes to the nativeTheme', () => {
56 store.config.setThemeSource('dark');
57 expect(nativeTheme.themeSource).toBe('dark');
58});
59
60it('should synchronize shouldUseDarkColors changes to the store', () => {
61 const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1];
62 shouldUseDarkColors = true;
63 listener();
64 expect(store.shared.shouldUseDarkColors).toBe(true);
65});
66
67it('should remove the listener on dispose', () => {
68 const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1];
69 disposeSut();
70 expect(nativeTheme.off).toBeCalledWith('updated', listener);
71});
diff --git a/packages/main/src/controllers/config.ts b/packages/main/src/controllers/config.ts
index 600a142..d3559c8 100644
--- a/packages/main/src/controllers/config.ts
+++ b/packages/main/src/controllers/config.ts
@@ -60,12 +60,8 @@ export async function initConfig(
60 60
61 if (!await readConfig()) { 61 if (!await readConfig()) {
62 log.info('Config file was not found'); 62 log.info('Config file was not found');
63 try { 63 await writeConfig();
64 await writeConfig(); 64 log.info('Created config file');
65 log.info('Created config file');
66 } catch (err) {
67 log.error('Failed to initialize config', err);
68 }
69 } 65 }
70 66
71 const disposeOnSnapshot = onSnapshot(config, debounce((snapshot) => { 67 const disposeOnSnapshot = onSnapshot(config, debounce((snapshot) => {
@@ -73,12 +69,16 @@ export async function initConfig(
73 if (lastSnapshotOnDisk !== snapshot) { 69 if (lastSnapshotOnDisk !== snapshot) {
74 writeConfig().catch((err) => { 70 writeConfig().catch((err) => {
75 log.error('Failed to write config on config change', err); 71 log.error('Failed to write config on config change', err);
76 }) 72 });
77 } 73 }
78 }, debounceTime)); 74 }, debounceTime));
79 75
80 const disposeWatcher = persistenceService.watchConfig(async () => { 76 const disposeWatcher = persistenceService.watchConfig(async () => {
81 await readConfig(); 77 try {
78 await readConfig();
79 } catch (err) {
80 log.error('Failed to read config', err);
81 }
82 }, debounceTime); 82 }, debounceTime);
83 83
84 return () => { 84 return () => {