test: add unit-tests for DoneAction.js

This commit is contained in:
Jelle Glebbeek
2021-05-22 01:48:11 +02:00
parent 4ed8e71384
commit 562d70b9c6

68
tests/DoneAction.test.js Normal file
View File

@@ -0,0 +1,68 @@
const execa = require('execa');
const DoneAction = require("../modules/DoneAction");
jest.mock('execa');
const platforms = ["win32", "linux", "mac"];
const actions = ["Lock", "Sleep", "Shutdown"];
const actionLength = [4, 3, 3];
beforeEach(() => {
jest.clearAllMocks();
jest.doMock('execa', () => {
const originalModule = jest.requireActual('execa')
return {
__esModule: true,
...originalModule,
execa: jest.fn()
}
});
});
describe('execute', () => {
it('executes the chosen action', () => {
for(const platform of platforms) {
Object.defineProperty(process, "platform", {
value: platform
});
const instance = new DoneAction();
execa.mockResolvedValue("");
instance.executeAction(actions[platforms.indexOf(platform)]);
expect(execa.mock.calls[platforms.indexOf(platform)]).toBeTruthy();
}
expect(execa).toBeCalledTimes(platforms.length);
});
it('does nothing on Do nothing', () => {
const instance = new DoneAction();
execa.mockResolvedValue("");
instance.executeAction("Do nothing");
expect(execa).toBeCalledTimes(0);
});
it('exits on Close app', () => {
const instance = new DoneAction();
execa.mockResolvedValue("");
process.exit = jest.fn();
instance.executeAction("Close app");
expect(process.exit).toBeCalledTimes(1);
});
it('logs an error', async () => {
const instance = new DoneAction();
execa.mockRejectedValue("");
console.error = jest.fn().mockImplementation(() => {});
await instance.executeAction("Sleep");
expect(console.error).toBeCalledTimes(1);
});
});
describe('get', () => {
it("Returns the actions for the platform", () => {
for(const platform of platforms) {
Object.defineProperty(process, "platform", {
value: platform
});
const instance = new DoneAction();
const actions = instance.getActions();
expect(actions.length).toEqual(actionLength[platforms.indexOf(platform)]);
}
});
});