Browse Source

Fix failing tests with electron-settings in redux

Tests were failing due to being unable to find the settings file while
running tests.
master
jeffvli 3 years ago
parent
commit
c032460651
  1. 0
      src/__tests__/settings.json
  2. 9
      src/components/shared/setDefaultSettings.ts
  3. 17
      src/redux/miscSlice.ts
  4. 42
      src/redux/playQueueSlice.ts
  5. 37
      src/shared/utils.ts

0
src/shared/settings.json → src/__tests__/settings.json

9
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');
}

17
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({

42
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) => {

37
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'))
);
};

Loading…
Cancel
Save