From d07e7b834831230b53860d0919a68edc2d36193d Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sat, 8 Jan 2022 21:36:43 +0100 Subject: build: Eslint fixes for multi-module project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kristóf Marussy --- .../main/src/controllers/__tests__/config.spec.ts | 185 --------------------- .../src/controllers/__tests__/initConfig.spec.ts | 185 +++++++++++++++++++++ .../controllers/__tests__/initNativeTheme.spec.ts | 71 ++++++++ .../src/controllers/__tests__/nativeTheme.spec.ts | 71 -------- 4 files changed, 256 insertions(+), 256 deletions(-) delete mode 100644 packages/main/src/controllers/__tests__/config.spec.ts create mode 100644 packages/main/src/controllers/__tests__/initConfig.spec.ts create mode 100644 packages/main/src/controllers/__tests__/initNativeTheme.spec.ts delete mode 100644 packages/main/src/controllers/__tests__/nativeTheme.spec.ts (limited to 'packages/main/src/controllers/__tests__') diff --git a/packages/main/src/controllers/__tests__/config.spec.ts b/packages/main/src/controllers/__tests__/config.spec.ts deleted file mode 100644 index eb67df0..0000000 --- a/packages/main/src/controllers/__tests__/config.spec.ts +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { jest } from '@jest/globals'; -import { mocked } from 'jest-mock'; -import ms from 'ms'; - -import { initConfig } from '../config'; -import type { ConfigPersistenceService } from '../../services/ConfigPersistenceService'; -import { Config, config as configModel } from '../../stores/Config'; -import { Disposer } from '../../utils/disposer'; -import { silenceLogger } from '../../utils/logging'; - -let config: Config; -let persistenceService: ConfigPersistenceService = { - readConfig: jest.fn(), - writeConfig: jest.fn(), - watchConfig: jest.fn(), -}; -let lessThanThrottleMs = ms('0.1s'); -let throttleMs = ms('1s'); - -beforeAll(() => { - jest.useFakeTimers(); - silenceLogger(); -}); - -beforeEach(() => { - config = configModel.create(); -}); - -describe('when initializing', () => { - describe('when there is no config file', () => { - beforeEach(() => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: false, - }); - }); - - it('should create a new config file', async () => { - await initConfig(config, persistenceService); - expect(persistenceService.writeConfig).toBeCalledTimes(1); - }); - - it('should bail if there is an an error creating the config file', async () => { - mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); - await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); - }); - }); - - describe('when there is a valid config file', () => { - beforeEach(() => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: 'dark', - }, - }); - }); - - it('should read the existing config file is there is one', async () => { - await initConfig(config, persistenceService); - expect(persistenceService.writeConfig).not.toBeCalled(); - expect(config.themeSource).toBe('dark'); - }); - - it('should bail if it cannot set up a watcher', async () => { - mocked(persistenceService.watchConfig).mockImplementationOnce(() => { - throw new Error('boo'); - }); - await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); - }); - }); - - it('should not apply an invalid config file', async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: -1, - }, - }); - await initConfig(config, persistenceService); - expect(config.themeSource).not.toBe(-1); - }); - - it('should bail if it cannot determine whether there is a config file', async () => { - mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); - await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); - }); -}); - -describe('when it has loaded the config', () => { - let sutDisposer: Disposer; - let watcherDisposer: Disposer = jest.fn(); - let configChangedCallback: () => Promise; - - beforeEach(async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: {}, - }); - mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer); - sutDisposer = await initConfig(config, persistenceService, throttleMs); - configChangedCallback = mocked(persistenceService.watchConfig).mock.calls[0][0]; - jest.resetAllMocks(); - }); - - it('should throttle saving changes to the config file', () => { - mocked(persistenceService.writeConfig).mockResolvedValue(undefined); - config.setThemeSource('dark'); - jest.advanceTimersByTime(lessThanThrottleMs); - config.setThemeSource('light'); - jest.advanceTimersByTime(throttleMs); - expect(persistenceService.writeConfig).toBeCalledTimes(1); - }); - - it('should handle config writing errors gracefully', () => { - mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); - config.setThemeSource('dark'); - jest.advanceTimersByTime(throttleMs); - expect(persistenceService.writeConfig).toBeCalledTimes(1); - }); - - it('should read the config file when it has changed', async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: 'dark', - }, - }); - await configChangedCallback(); - // Do not write back the changes we have just read. - expect(persistenceService.writeConfig).not.toBeCalled(); - expect(config.themeSource).toBe('dark'); - }); - - it('should not apply an invalid config file when it has changed', async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: -1, - }, - }); - await configChangedCallback(); - expect(config.themeSource).not.toBe(-1); - }); - - it('should handle config writing errors gracefully', async () => { - mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); - await configChangedCallback(); - }); - - describe('when it was disposed', () => { - beforeEach(() => { - sutDisposer(); - }); - - it('should dispose the watcher', () => { - expect(watcherDisposer).toBeCalled(); - }); - - it('should not listen to store changes any more', () => { - config.setThemeSource('dark'); - jest.advanceTimersByTime(2 * throttleMs); - expect(persistenceService.writeConfig).not.toBeCalled(); - }); - }); -}); diff --git a/packages/main/src/controllers/__tests__/initConfig.spec.ts b/packages/main/src/controllers/__tests__/initConfig.spec.ts new file mode 100644 index 0000000..e386a07 --- /dev/null +++ b/packages/main/src/controllers/__tests__/initConfig.spec.ts @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { jest } from '@jest/globals'; +import { mocked } from 'jest-mock'; +import ms from 'ms'; + +import type ConfigPersistenceService from '../../services/ConfigPersistenceService'; +import { Config, config as configModel } from '../../stores/Config'; +import type Disposer from '../../utils/Disposer'; +import { silenceLogger } from '../../utils/log'; +import initConfig from '../initConfig'; + +let config: Config; +const persistenceService: ConfigPersistenceService = { + readConfig: jest.fn(), + writeConfig: jest.fn(), + watchConfig: jest.fn(), +}; +const lessThanThrottleMs = ms('0.1s'); +const throttleMs = ms('1s'); + +beforeAll(() => { + jest.useFakeTimers(); + silenceLogger(); +}); + +beforeEach(() => { + config = configModel.create(); +}); + +describe('when initializing', () => { + describe('when there is no config file', () => { + beforeEach(() => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: false, + }); + }); + + it('should create a new config file', async () => { + await initConfig(config, persistenceService); + expect(persistenceService.writeConfig).toBeCalledTimes(1); + }); + + it('should bail if there is an an error creating the config file', async () => { + mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); + await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); + }); + }); + + describe('when there is a valid config file', () => { + beforeEach(() => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: 'dark', + }, + }); + }); + + it('should read the existing config file is there is one', async () => { + await initConfig(config, persistenceService); + expect(persistenceService.writeConfig).not.toBeCalled(); + expect(config.themeSource).toBe('dark'); + }); + + it('should bail if it cannot set up a watcher', async () => { + mocked(persistenceService.watchConfig).mockImplementationOnce(() => { + throw new Error('boo'); + }); + await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); + }); + }); + + it('should not apply an invalid config file', async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: -1, + }, + }); + await initConfig(config, persistenceService); + expect(config.themeSource).not.toBe(-1); + }); + + it('should bail if it cannot determine whether there is a config file', async () => { + mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); + await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); + }); +}); + +describe('when it has loaded the config', () => { + let sutDisposer: Disposer; + const watcherDisposer: Disposer = jest.fn(); + let configChangedCallback: () => Promise; + + beforeEach(async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: {}, + }); + mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer); + sutDisposer = await initConfig(config, persistenceService, throttleMs); + [[configChangedCallback]] = mocked(persistenceService.watchConfig).mock.calls; + jest.resetAllMocks(); + }); + + it('should throttle saving changes to the config file', () => { + mocked(persistenceService.writeConfig).mockResolvedValue(undefined); + config.setThemeSource('dark'); + jest.advanceTimersByTime(lessThanThrottleMs); + config.setThemeSource('light'); + jest.advanceTimersByTime(throttleMs); + expect(persistenceService.writeConfig).toBeCalledTimes(1); + }); + + it('should handle config writing errors gracefully', () => { + mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); + config.setThemeSource('dark'); + jest.advanceTimersByTime(throttleMs); + expect(persistenceService.writeConfig).toBeCalledTimes(1); + }); + + it('should read the config file when it has changed', async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: 'dark', + }, + }); + await configChangedCallback(); + // Do not write back the changes we have just read. + expect(persistenceService.writeConfig).not.toBeCalled(); + expect(config.themeSource).toBe('dark'); + }); + + it('should not apply an invalid config file when it has changed', async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: -1, + }, + }); + await configChangedCallback(); + expect(config.themeSource).not.toBe(-1); + }); + + it('should handle config writing errors gracefully', async () => { + mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); + await configChangedCallback(); + }); + + describe('when it was disposed', () => { + beforeEach(() => { + sutDisposer(); + }); + + it('should dispose the watcher', () => { + expect(watcherDisposer).toBeCalled(); + }); + + it('should not listen to store changes any more', () => { + config.setThemeSource('dark'); + jest.advanceTimersByTime(2 * throttleMs); + expect(persistenceService.writeConfig).not.toBeCalled(); + }); + }); +}); diff --git a/packages/main/src/controllers/__tests__/initNativeTheme.spec.ts b/packages/main/src/controllers/__tests__/initNativeTheme.spec.ts new file mode 100644 index 0000000..bd33f48 --- /dev/null +++ b/packages/main/src/controllers/__tests__/initNativeTheme.spec.ts @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { jest } from '@jest/globals'; +import { mocked } from 'jest-mock'; + +import { createMainStore, MainStore } from '../../stores/MainStore'; +import type Disposer from '../../utils/Disposer'; + +let shouldUseDarkColors = false; + +jest.unstable_mockModule('electron', () => ({ + nativeTheme: { + themeSource: 'system', + get shouldUseDarkColors() { + return shouldUseDarkColors; + }, + on: jest.fn(), + off: jest.fn(), + }, +})); + +const { nativeTheme } = await import('electron'); +const { default: initNativeTheme } = await import('../initNativeTheme'); + +let store: MainStore; +let disposeSut: Disposer; + +beforeEach(() => { + store = createMainStore(); + disposeSut = initNativeTheme(store); +}); + +it('should register a nativeTheme updated listener', () => { + expect(nativeTheme.on).toBeCalledWith('updated', expect.anything()); +}); + +it('should synchronize themeSource changes to the nativeTheme', () => { + store.config.setThemeSource('dark'); + expect(nativeTheme.themeSource).toBe('dark'); +}); + +it('should synchronize shouldUseDarkColors changes to the store', () => { + const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; + shouldUseDarkColors = true; + listener(); + expect(store.shared.shouldUseDarkColors).toBe(true); +}); + +it('should remove the listener on dispose', () => { + const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; + disposeSut(); + expect(nativeTheme.off).toBeCalledWith('updated', listener); +}); diff --git a/packages/main/src/controllers/__tests__/nativeTheme.spec.ts b/packages/main/src/controllers/__tests__/nativeTheme.spec.ts deleted file mode 100644 index 85d6dd2..0000000 --- a/packages/main/src/controllers/__tests__/nativeTheme.spec.ts +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { jest } from '@jest/globals'; -import { mocked } from 'jest-mock'; - -import { createMainStore, MainStore } from '../../stores/MainStore'; -import { Disposer } from '../../utils/disposer'; - -let shouldUseDarkColors = false; - -jest.unstable_mockModule('electron', () => ({ - nativeTheme: { - themeSource: 'system', - get shouldUseDarkColors() { - return shouldUseDarkColors; - }, - on: jest.fn(), - off: jest.fn(), - }, -})); - -const { nativeTheme } = await import('electron'); -const { initNativeTheme } = await import('../nativeTheme'); - -let store: MainStore; -let disposeSut: Disposer; - -beforeEach(() => { - store = createMainStore(); - disposeSut = initNativeTheme(store); -}); - -it('should register a nativeTheme updated listener', () => { - expect(nativeTheme.on).toBeCalledWith('updated', expect.anything()); -}); - -it('should synchronize themeSource changes to the nativeTheme', () => { - store.config.setThemeSource('dark'); - expect(nativeTheme.themeSource).toBe('dark'); -}); - -it('should synchronize shouldUseDarkColors changes to the store', () => { - const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; - shouldUseDarkColors = true; - listener(); - expect(store.shared.shouldUseDarkColors).toBe(true); -}); - -it('should remove the listener on dispose', () => { - const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; - disposeSut(); - expect(nativeTheme.off).toBeCalledWith('updated', listener); -}); -- cgit v1.2.3-54-g00ecf