ed-safari/test/Settings.test.ts

97 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-05-18 20:13:30 -07:00
import { expect, jest } from '@jest/globals';
import { EliteMatrix } from 'elite-matrix';
import { Settings } from '../src/models/Settings';
import { mockSettings } from './mockData';
2023-05-17 14:43:14 -07:00
jest.mock('fs');
describe('Settings', () => {
const settingsFile = {
minValue: 500000,
maxDistance: 10000,
matrixFile: '',
};
beforeEach(() => {
require('fs').__setFileContents(JSON.stringify(settingsFile));
});
2023-05-18 20:13:30 -07:00
afterEach(() => {
jest.resetAllMocks();
Settings.destroy();
});
2023-05-17 14:43:14 -07:00
describe('get()', () => {
it('should get instance', () => {
expect(Settings.get()).toBeInstanceOf(Settings);
});
it('should set initial values', () => {
const minValue = Settings.get().minValue;
expect(minValue).toBeDefined();
expect(typeof minValue).toBe('number');
const maxDistance = Settings.get().maxDistance;
expect(maxDistance).toBeDefined();
expect(typeof maxDistance).toBe('number');
const matrix = Settings.get().matrix;
expect(matrix).toBeUndefined();
});
});
describe('save()', () => {
2023-05-17 16:18:45 -07:00
const writeFileMock = jest.spyOn(require('fs/promises'), 'writeFile');
2023-05-18 20:13:30 -07:00
const readFileMock = jest.spyOn(require('fs/promises'), 'readFile');
2023-05-17 16:18:45 -07:00
2023-05-17 14:43:14 -07:00
it('should return boolean', async () => {
2023-05-17 16:18:45 -07:00
writeFileMock.mockResolvedValue(undefined);
readFileMock.mockResolvedValue(settingsFile);
2023-05-17 14:43:14 -07:00
const result = await Settings.get().save(settingsFile);
expect(typeof result).toBe('boolean');
2023-05-17 16:18:45 -07:00
});
2023-05-17 14:43:14 -07:00
2023-05-17 16:18:45 -07:00
it('should return false when writeFile fails', async () => {
writeFileMock.mockImplementation(() => {
throw new Error();
});
const result = await Settings.get().save(settingsFile);
expect(result).toBe(false);
});
2023-05-17 14:43:14 -07:00
2023-05-17 16:18:45 -07:00
it('should return true when readFile fails', async () => {
writeFileMock.mockResolvedValue(undefined);
readFileMock.mockImplementation(() => {
throw new Error();
});
const result = await Settings.get().save(settingsFile);
expect(result).toBe(true);
2023-05-17 14:43:14 -07:00
});
});
2023-05-17 16:18:45 -07:00
describe('#setMatrix()', () => {
const readFileMock = jest.spyOn(require('fs/promises'), 'readFile');
2023-05-18 21:29:49 -07:00
it.each([
{ file: 'GraphicsConfiguration.xml', contents: mockSettings.graphicsConfigXml },
{ file: 'XML-Profile.ini', contents: mockSettings.xmlProfileIni },
])('should set matrix from $file', ({ file, contents }, done) => {
2023-05-17 16:18:45 -07:00
const settingsFile = {
minValue: 500000,
maxDistance: 10000,
2023-05-18 21:29:49 -07:00
matrixFile: file,
2023-05-17 16:18:45 -07:00
};
require('fs').__setFileContents(JSON.stringify(settingsFile));
2023-05-18 21:29:49 -07:00
readFileMock.mockResolvedValue(contents);
2023-05-17 16:18:45 -07:00
const settings = Settings.get();
settings.on('CUSTOM_COLORS_SET', () => {
expect(Settings.get().matrix).toBeDefined();
expect(Settings.get().matrix).toBeInstanceOf(EliteMatrix);
done();
});
});
});
2023-05-17 14:43:14 -07:00
});