Settings unit tests.
This commit is contained in:
parent
72648f7430
commit
87779e88ab
3 changed files with 60 additions and 25 deletions
|
@ -6,12 +6,6 @@ function __setFileContents(contents) {
|
|||
fileContents = contents;
|
||||
}
|
||||
|
||||
let writePromise = null;
|
||||
|
||||
function __setWritePromise(resolve) {
|
||||
writePromise = Promise.resolve(resolve);
|
||||
}
|
||||
|
||||
// example info from Node docs
|
||||
function statSync(file) {
|
||||
return {
|
||||
|
@ -44,16 +38,9 @@ function readFileSync(file, options) {
|
|||
return fileContents;
|
||||
}
|
||||
|
||||
let promises = {
|
||||
writeFile: jest.fn(() => writePromise),
|
||||
readFile: jest.fn(() => fileContents),
|
||||
};
|
||||
|
||||
fs.__setFileContents = __setFileContents;
|
||||
fs.__setWritePromise = __setWritePromise;
|
||||
fs.statSync = statSync;
|
||||
fs.writeFileSync = writeFileSync;
|
||||
fs.readFileSync = readFileSync;
|
||||
fs.promises = promises;
|
||||
|
||||
module.exports = fs;
|
||||
|
|
|
@ -20,10 +20,9 @@ interface settingsFile {
|
|||
}
|
||||
|
||||
export class Settings extends EventEmitter {
|
||||
static #instance: Settings;
|
||||
static #instance: Settings | undefined;
|
||||
|
||||
readonly #file: string;
|
||||
#writing: boolean;
|
||||
|
||||
minValue: number;
|
||||
maxDistance: number;
|
||||
|
@ -61,7 +60,6 @@ export class Settings extends EventEmitter {
|
|||
this.minValue = contents.minValue;
|
||||
this.maxDistance = contents.maxDistance;
|
||||
this.#matrixFile = contents.matrixFile;
|
||||
this.#writing = false;
|
||||
|
||||
if (this.#matrixFile) {
|
||||
this.#setMatrix();
|
||||
|
@ -76,6 +74,10 @@ export class Settings extends EventEmitter {
|
|||
return Settings.#instance;
|
||||
}
|
||||
|
||||
static destroy(): void {
|
||||
Settings.#instance = undefined;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------------- save ---- */
|
||||
|
||||
async save(settings: settingsFile): Promise<boolean> {
|
||||
|
@ -86,8 +88,11 @@ export class Settings extends EventEmitter {
|
|||
|
||||
Log.write('Settings saved!');
|
||||
|
||||
// Update Settings props.
|
||||
await this.#read();
|
||||
try {
|
||||
await this.#read();
|
||||
} catch (err) {
|
||||
Log.write(err);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import {expect} from '@jest/globals';
|
||||
import {expect, jest} from '@jest/globals';
|
||||
import {EliteMatrix} from 'elite-matrix';
|
||||
import {Settings} from '../src/models/Settings';
|
||||
|
||||
jest.mock('fs');
|
||||
|
@ -34,17 +35,59 @@ describe('Settings', () => {
|
|||
});
|
||||
|
||||
describe('save()', () => {
|
||||
const writeFileMock = jest.spyOn(require('fs/promises'), 'writeFile');
|
||||
const readFileMock = jest.spyOn(require('fs/promises'), 'readFile');
|
||||
|
||||
it('should return boolean', async () => {
|
||||
writeFileMock.mockResolvedValue(undefined);
|
||||
readFileMock.mockResolvedValue(settingsFile);
|
||||
const result = await Settings.get().save(settingsFile);
|
||||
expect(typeof result).toBe('boolean');
|
||||
});
|
||||
|
||||
require('fs').__setWritePromise(true);
|
||||
const resultResolve = await Settings.get().save(settingsFile);
|
||||
expect(resultResolve).toBe(true);
|
||||
it('should return false when writeFile fails', async () => {
|
||||
writeFileMock.mockImplementation(() => {
|
||||
throw new Error();
|
||||
});
|
||||
const result = await Settings.get().save(settingsFile);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
require('fs').__setWritePromise(false);
|
||||
const resultReject = await Settings.get().save(settingsFile);
|
||||
expect(resultReject).toBe(false);
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#setMatrix()', () => {
|
||||
const readFileMock = jest.spyOn(require('fs/promises'), 'readFile');
|
||||
|
||||
it('should set matrix from GraphicsConfiguration.xml', (done) => {
|
||||
const settingsFile = {
|
||||
minValue: 500000,
|
||||
maxDistance: 10000,
|
||||
matrixFile: 'GraphicsConfiguration.xml',
|
||||
};
|
||||
require('fs').__setFileContents(JSON.stringify(settingsFile));
|
||||
const matrixFile = '<GraphicsConfig><GUIColour><Default><MatrixRed>1,0,0</MatrixRed><MatrixGreen>0,1,0</MatrixGreen><MatrixBlue>0,0,1</MatrixBlue></Default></GUIColour></GraphicsConfig>';
|
||||
readFileMock.mockResolvedValue(matrixFile);
|
||||
|
||||
const settings = Settings.get();
|
||||
|
||||
settings.on('CUSTOM_COLORS_SET', () => {
|
||||
expect(Settings.get().matrix).toBeDefined();
|
||||
expect(Settings.get().matrix).toBeInstanceOf(EliteMatrix);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
Settings.destroy();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue