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;
|
fileContents = contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
let writePromise = null;
|
|
||||||
|
|
||||||
function __setWritePromise(resolve) {
|
|
||||||
writePromise = Promise.resolve(resolve);
|
|
||||||
}
|
|
||||||
|
|
||||||
// example info from Node docs
|
// example info from Node docs
|
||||||
function statSync(file) {
|
function statSync(file) {
|
||||||
return {
|
return {
|
||||||
|
@ -44,16 +38,9 @@ function readFileSync(file, options) {
|
||||||
return fileContents;
|
return fileContents;
|
||||||
}
|
}
|
||||||
|
|
||||||
let promises = {
|
|
||||||
writeFile: jest.fn(() => writePromise),
|
|
||||||
readFile: jest.fn(() => fileContents),
|
|
||||||
};
|
|
||||||
|
|
||||||
fs.__setFileContents = __setFileContents;
|
fs.__setFileContents = __setFileContents;
|
||||||
fs.__setWritePromise = __setWritePromise;
|
|
||||||
fs.statSync = statSync;
|
fs.statSync = statSync;
|
||||||
fs.writeFileSync = writeFileSync;
|
fs.writeFileSync = writeFileSync;
|
||||||
fs.readFileSync = readFileSync;
|
fs.readFileSync = readFileSync;
|
||||||
fs.promises = promises;
|
|
||||||
|
|
||||||
module.exports = fs;
|
module.exports = fs;
|
||||||
|
|
|
@ -20,10 +20,9 @@ interface settingsFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Settings extends EventEmitter {
|
export class Settings extends EventEmitter {
|
||||||
static #instance: Settings;
|
static #instance: Settings | undefined;
|
||||||
|
|
||||||
readonly #file: string;
|
readonly #file: string;
|
||||||
#writing: boolean;
|
|
||||||
|
|
||||||
minValue: number;
|
minValue: number;
|
||||||
maxDistance: number;
|
maxDistance: number;
|
||||||
|
@ -61,7 +60,6 @@ export class Settings extends EventEmitter {
|
||||||
this.minValue = contents.minValue;
|
this.minValue = contents.minValue;
|
||||||
this.maxDistance = contents.maxDistance;
|
this.maxDistance = contents.maxDistance;
|
||||||
this.#matrixFile = contents.matrixFile;
|
this.#matrixFile = contents.matrixFile;
|
||||||
this.#writing = false;
|
|
||||||
|
|
||||||
if (this.#matrixFile) {
|
if (this.#matrixFile) {
|
||||||
this.#setMatrix();
|
this.#setMatrix();
|
||||||
|
@ -76,6 +74,10 @@ export class Settings extends EventEmitter {
|
||||||
return Settings.#instance;
|
return Settings.#instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static destroy(): void {
|
||||||
|
Settings.#instance = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------------- save ---- */
|
/* -------------------------------------------------------------------------------- save ---- */
|
||||||
|
|
||||||
async save(settings: settingsFile): Promise<boolean> {
|
async save(settings: settingsFile): Promise<boolean> {
|
||||||
|
@ -86,8 +88,11 @@ export class Settings extends EventEmitter {
|
||||||
|
|
||||||
Log.write('Settings saved!');
|
Log.write('Settings saved!');
|
||||||
|
|
||||||
// Update Settings props.
|
try {
|
||||||
await this.#read();
|
await this.#read();
|
||||||
|
} catch (err) {
|
||||||
|
Log.write(err);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
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';
|
import {Settings} from '../src/models/Settings';
|
||||||
|
|
||||||
jest.mock('fs');
|
jest.mock('fs');
|
||||||
|
@ -34,17 +35,59 @@ describe('Settings', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('save()', () => {
|
describe('save()', () => {
|
||||||
|
const writeFileMock = jest.spyOn(require('fs/promises'), 'writeFile');
|
||||||
|
const readFileMock = jest.spyOn(require('fs/promises'), 'readFile');
|
||||||
|
|
||||||
it('should return boolean', async () => {
|
it('should return boolean', async () => {
|
||||||
|
writeFileMock.mockResolvedValue(undefined);
|
||||||
|
readFileMock.mockResolvedValue(settingsFile);
|
||||||
const result = await Settings.get().save(settingsFile);
|
const result = await Settings.get().save(settingsFile);
|
||||||
expect(typeof result).toBe('boolean');
|
expect(typeof result).toBe('boolean');
|
||||||
|
});
|
||||||
|
|
||||||
require('fs').__setWritePromise(true);
|
it('should return false when writeFile fails', async () => {
|
||||||
const resultResolve = await Settings.get().save(settingsFile);
|
writeFileMock.mockImplementation(() => {
|
||||||
expect(resultResolve).toBe(true);
|
throw new Error();
|
||||||
|
});
|
||||||
|
const result = await Settings.get().save(settingsFile);
|
||||||
|
expect(result).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
require('fs').__setWritePromise(false);
|
it('should return true when readFile fails', async () => {
|
||||||
const resultReject = await Settings.get().save(settingsFile);
|
writeFileMock.mockResolvedValue(undefined);
|
||||||
expect(resultReject).toBe(false);
|
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