From 9546dc2aa39ab096ccc723786e718a739d0bdaf9 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 27 Jan 2022 00:17:22 +0100 Subject: refactor: Coding conventions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure that files have a default import with the same name as the file whenever possible to reduce surprise. Also shuffles around some file names for better legibility. Signed-off-by: Kristóf Marussy --- .../reactions/__tests__/synchronizeConfig.spec.ts | 225 +++++++++++++++++++++ .../__tests__/synchronizeNativeTheme.spec.ts | 77 +++++++ 2 files changed, 302 insertions(+) create mode 100644 packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts create mode 100644 packages/main/src/reactions/__tests__/synchronizeNativeTheme.spec.ts (limited to 'packages/main/src/reactions/__tests__') diff --git a/packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts b/packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts new file mode 100644 index 0000000..c145bf3 --- /dev/null +++ b/packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts @@ -0,0 +1,225 @@ +/* + * 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 ConfigRepository from '../../infrastructure/config/ConfigRepository'; +import SharedStore from '../../stores/SharedStore'; +import type Disposer from '../../utils/Disposer'; +import { silenceLogger } from '../../utils/log'; +import synchronizeConfig from '../synchronizeConfig'; + +let store: SharedStore; +const repository: ConfigRepository = { + readConfig: jest.fn(), + writeConfig: jest.fn(), + watchConfig: jest.fn(), +}; +const lessThanThrottleMs = ms('0.1s'); +const throttleMs = ms('1s'); + +beforeAll(() => { + jest.useFakeTimers(); + silenceLogger(); +}); + +beforeEach(() => { + store = SharedStore.create(); +}); + +describe('when synchronizeializing', () => { + describe('when there is no config file', () => { + beforeEach(() => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: false, + }); + }); + + it('should create a new config file', async () => { + await synchronizeConfig(store, repository); + expect(repository.writeConfig).toHaveBeenCalledTimes(1); + }); + + it('should bail if there is an an error creating the config file', async () => { + mocked(repository.writeConfig).mockRejectedValue(new Error('boo')); + await expect(() => + synchronizeConfig(store, repository), + ).rejects.toBeInstanceOf(Error); + }); + }); + + describe('when there is a valid config file', () => { + beforeEach(() => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: true, + data: { + // Use a default empty config file to not trigger config rewrite. + ...store.config, + themeSource: 'dark', + }, + }); + }); + + it('should read the existing config file is there is one', async () => { + await synchronizeConfig(store, repository); + expect(repository.writeConfig).not.toHaveBeenCalled(); + expect(store.settings.themeSource).toBe('dark'); + }); + + it('should bail if it cannot set up a watcher', async () => { + mocked(repository.watchConfig).mockImplementationOnce(() => { + throw new Error('boo'); + }); + await expect(() => + synchronizeConfig(store, repository), + ).rejects.toBeInstanceOf(Error); + }); + }); + + it('should update the config file if new details are added during read', async () => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: 'light', + profile: { + name: 'Test profile', + }, + }, + }); + await synchronizeConfig(store, repository); + expect(repository.writeConfig).toHaveBeenCalledTimes(1); + }); + + it('should not apply an invalid config file but should not overwrite it', async () => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: -1, + }, + }); + await synchronizeConfig(store, repository); + expect(store.settings.themeSource).not.toBe(-1); + expect(repository.writeConfig).not.toHaveBeenCalled(); + }); + + it('should bail if it cannot determine whether there is a config file', async () => { + mocked(repository.readConfig).mockRejectedValue(new Error('boo')); + await expect(() => + synchronizeConfig(store, repository), + ).rejects.toBeInstanceOf(Error); + }); +}); + +describe('when it has loaded the config', () => { + let sutDisposer: Disposer; + const watcherDisposer: Disposer = jest.fn(); + let configChangedCallback: () => Promise; + + beforeEach(async () => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: true, + data: store.config, + }); + mocked(repository.watchConfig).mockReturnValueOnce(watcherDisposer); + sutDisposer = await synchronizeConfig(store, repository, throttleMs); + [[configChangedCallback]] = mocked(repository.watchConfig).mock.calls; + jest.resetAllMocks(); + }); + + it('should throttle saving changes to the config file', () => { + mocked(repository.writeConfig).mockResolvedValue(); + store.settings.setThemeSource('dark'); + jest.advanceTimersByTime(lessThanThrottleMs); + store.settings.setThemeSource('light'); + jest.advanceTimersByTime(throttleMs); + expect(repository.writeConfig).toHaveBeenCalledTimes(1); + }); + + it('should handle config writing errors gracefully', () => { + mocked(repository.writeConfig).mockRejectedValue(new Error('boo')); + store.settings.setThemeSource('dark'); + jest.advanceTimersByTime(throttleMs); + expect(repository.writeConfig).toHaveBeenCalledTimes(1); + }); + + it('should read the config file when it has changed', async () => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: true, + data: { + // Use a default empty config file to not trigger config rewrite. + ...store.config, + themeSource: 'dark', + }, + }); + await configChangedCallback(); + // Do not write back the changes we have just read. + expect(repository.writeConfig).not.toHaveBeenCalled(); + expect(store.settings.themeSource).toBe('dark'); + }); + + it('should update the config file if new details are added', async () => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: 'light', + profile: { + name: 'Test profile', + }, + }, + }); + await configChangedCallback(); + expect(repository.writeConfig).toHaveBeenCalledTimes(1); + }); + + it('should not apply an invalid config file when it has changed but should not overwrite it', async () => { + mocked(repository.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: -1, + }, + }); + await configChangedCallback(); + expect(store.settings.themeSource).not.toBe(-1); + expect(repository.writeConfig).not.toHaveBeenCalled(); + }); + + it('should handle config reading errors gracefully', async () => { + mocked(repository.readConfig).mockRejectedValue(new Error('boo')); + await expect(configChangedCallback()).resolves.not.toThrow(); + }); + + describe('when it was disposed', () => { + beforeEach(() => { + sutDisposer(); + }); + + it('should dispose the watcher', () => { + expect(watcherDisposer).toHaveBeenCalled(); + }); + + it('should not listen to store changes any more', () => { + store.settings.setThemeSource('dark'); + jest.advanceTimersByTime(2 * throttleMs); + expect(repository.writeConfig).not.toHaveBeenCalled(); + }); + }); +}); diff --git a/packages/main/src/reactions/__tests__/synchronizeNativeTheme.spec.ts b/packages/main/src/reactions/__tests__/synchronizeNativeTheme.spec.ts new file mode 100644 index 0000000..cf37568 --- /dev/null +++ b/packages/main/src/reactions/__tests__/synchronizeNativeTheme.spec.ts @@ -0,0 +1,77 @@ +/* + * 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 SharedStore from '../../stores/SharedStore'; +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: synchronizeNativeTheme } = await import( + '../synchronizeNativeTheme' +); + +let store: SharedStore; +let disposeSut: Disposer; + +beforeEach(() => { + store = SharedStore.create(); + disposeSut = synchronizeNativeTheme(store); +}); + +it('should register a nativeTheme updated listener', () => { + expect(nativeTheme.on).toHaveBeenCalledWith('updated', expect.anything()); +}); + +it('should synchronize themeSource changes to the nativeTheme', () => { + store.settings.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.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).toHaveBeenCalledWith('updated', listener); +}); -- cgit v1.2.3-70-g09d2