From c0324606519b9b2cb81e1ac9f14a5a4a2bf29bce Mon Sep 17 00:00:00 2001 From: jeffvli Date: Sun, 19 Sep 2021 01:40:04 -0700 Subject: [PATCH] Fix failing tests with electron-settings in redux Tests were failing due to being unable to find the settings file while running tests. --- src/{shared => __tests__}/settings.json | 0 src/components/shared/setDefaultSettings.ts | 9 +---- src/redux/miscSlice.ts | 17 ++++++--- src/redux/playQueueSlice.ts | 42 ++++++++++----------- src/shared/utils.ts | 37 ++---------------- 5 files changed, 38 insertions(+), 67 deletions(-) rename src/{shared => __tests__}/settings.json (100%) diff --git a/src/shared/settings.json b/src/__tests__/settings.json similarity index 100% rename from src/shared/settings.json rename to src/__tests__/settings.json diff --git a/src/components/shared/setDefaultSettings.ts b/src/components/shared/setDefaultSettings.ts index c501acc..0cfa95d 100644 --- a/src/components/shared/setDefaultSettings.ts +++ b/src/components/shared/setDefaultSettings.ts @@ -1,8 +1,6 @@ import settings from 'electron-settings'; import path from 'path'; -import fs from 'fs'; -import { getSongCachePath, getImageCachePath } from '../../shared/utils'; -// Set setting defaults on first login + const setDefaultSettings = (force: boolean) => { if (force || !settings.hasSync('theme')) { settings.setSync('theme', 'defaultDark'); @@ -21,7 +19,7 @@ const setDefaultSettings = (force: boolean) => { } if (force || !settings.hasSync('globalMediaHotkeys')) { - settings.setSync('globalMediaHotkeys', true); + settings.setSync('globalMediaHotkeys', false); } if (force || !settings.hasSync('cachePath')) { @@ -44,9 +42,6 @@ const setDefaultSettings = (force: boolean) => { settings.setSync('volumeFade', true); } - fs.mkdirSync(getSongCachePath(), { recursive: true }); - fs.mkdirSync(getImageCachePath(), { recursive: true }); - if (force || !settings.hasSync('repeat')) { settings.setSync('repeat', 'all'); } diff --git a/src/redux/miscSlice.ts b/src/redux/miscSlice.ts index 5778353..9927094 100644 --- a/src/redux/miscSlice.ts +++ b/src/redux/miscSlice.ts @@ -1,7 +1,14 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; -import { getSettings } from '../shared/utils'; +import settings from 'electron-settings'; -const parsedSettings = getSettings(); +if (process.env.NODE_ENV === 'test') { + settings.configure({ + dir: '/src/__tests__/', + fileName: 'settings.json', + }); +} + +const parsedSettings = settings.getSync(); export interface ModalPage { pageType: string; @@ -32,8 +39,8 @@ export interface General { } const initialState: General = { - theme: parsedSettings.theme, - font: parsedSettings.font, + theme: String(parsedSettings.theme), + font: String(parsedSettings.font), modal: { show: false, currentPageIndex: undefined, @@ -44,7 +51,7 @@ const initialState: General = { contextMenu: { show: false, }, - dynamicBackground: parsedSettings.dynamicBackground, + dynamicBackground: Boolean(parsedSettings.dynamicBackground), }; const miscSlice = createSlice({ diff --git a/src/redux/playQueueSlice.ts b/src/redux/playQueueSlice.ts index 9a6054a..f27f510 100644 --- a/src/redux/playQueueSlice.ts +++ b/src/redux/playQueueSlice.ts @@ -1,14 +1,18 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import _ from 'lodash'; +import settings from 'electron-settings'; import { nanoid } from 'nanoid/non-secure'; import arrayMove from 'array-move'; -import { - areConsecutive, - consecutiveRanges, - getSettings, -} from '../shared/utils'; +import { areConsecutive, consecutiveRanges } from '../shared/utils'; -const parsedSettings = getSettings(); +if (process.env.NODE_ENV === 'test') { + settings.configure({ + dir: '/src/__tests__/', + fileName: 'settings.json', + }); +} + +const parsedSettings = settings.getSync(); export interface Entry { id: string; @@ -102,25 +106,25 @@ const initialState: PlayQueue = { timeData: [], }, }, - scrollWithCurrentSong: parsedSettings.scrollWithCurrentSong, - fadeDuration: parsedSettings.fadeDuration, - fadeType: parsedSettings.fadeType, - pollingInterval: parsedSettings.pollingInterval, - volumeFade: parsedSettings.volumeFade, + scrollWithCurrentSong: Boolean(parsedSettings.scrollWithCurrentSong), + fadeDuration: Number(parsedSettings.fadeDuration), + fadeType: String(parsedSettings.fadeType), + pollingInterval: Number(parsedSettings.pollingInterval), + volumeFade: Boolean(parsedSettings.volumeFade), currentIndex: 0, currentSongId: '', currentSongUniqueId: '', currentPlayer: 1, isFading: false, autoIncremented: false, - volume: parsedSettings.volume, - isLoading: false, - repeat: parsedSettings.repeat, - shuffle: parsedSettings.shuffle, + volume: Number(parsedSettings.volume), + isLoading: Boolean(false), + repeat: String(parsedSettings.repeat), + shuffle: Boolean(parsedSettings.shuffle), sortColumn: undefined, sortType: 'asc', displayQueue: false, - showDebugWindow: parsedSettings.showDebugWindow, + showDebugWindow: Boolean(parsedSettings.showDebugWindow), entry: [], shuffledEntry: [], sortedEntry: [], @@ -859,11 +863,7 @@ const playQueueSlice = createSlice({ ) => { const currentEntry = entrySelect(state); const tempQueue = state[currentEntry].slice(); - - const uniqueIds: any[] = []; - action.payload.entries.map((entry: Entry) => - uniqueIds.push(entry.uniqueId) - ); + const uniqueIds = _.map(action.payload.entries, 'uniqueId'); // Remove the selected entries from the queue const newQueue = tempQueue.filter((entry: Entry) => { diff --git a/src/shared/utils.ts b/src/shared/utils.ts index e1ac891..52494f1 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -1,48 +1,17 @@ import fs from 'fs'; import path from 'path'; import moment from 'moment'; - -let settingsPath = path.join( - String(process.env.APPDATA), - process.env.NODE_ENV !== 'production' ? 'Electron' : 'sonixd', - 'settings.json' -); - -// For CI -settingsPath = fs.existsSync(settingsPath) - ? settingsPath - : 'src/shared/settings.json'; - -export const getSettings = () => { - const parsedSettings = JSON.parse(String(fs.readFileSync(settingsPath))); - return { - repeat: parsedSettings.repeat, - shuffle: parsedSettings.shuffle, - volume: parsedSettings.volume, - fadeDuration: parsedSettings.fadeDuration, - fadeType: parsedSettings.fadeType, - pollingInterval: parsedSettings.pollingInterval, - volumeFade: parsedSettings.volumeFade, - showDebugWindow: parsedSettings.showDebugWindow, - theme: parsedSettings.theme, - font: parsedSettings.font, - scrollWithCurrentSong: parsedSettings.scrollWithCurrentSong, - dynamicBackground: parsedSettings.dynamicBackground, - cachePath: parsedSettings.cachePath, - serverBase64: parsedSettings.serverBase64, - }; -}; +import settings from 'electron-settings'; export const isCached = (filePath: string) => { return fs.existsSync(filePath); }; export const getRootCachePath = () => { - const parsedSettings = getSettings(); return path.join( - parsedSettings.cachePath, + String(settings.getSync('cachePath')), 'sonixdCache', - parsedSettings.serverBase64 + String(settings.getSync('serverBase64')) ); };