From e3f124f8841403270d3fb90133dbdb453ad0b94c Mon Sep 17 00:00:00 2001 From: "Victor R. Braga de Sales Mascarenhasm" Date: Fri, 25 Jan 2019 14:19:58 -0200 Subject: [PATCH] typescript: activityDetection unit test --- spec/__mocks__/electron.ts | 5 ++- spec/activityDetection.spec.ts | 66 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 spec/activityDetection.spec.ts diff --git a/spec/__mocks__/electron.ts b/spec/__mocks__/electron.ts index c4f60dc2d..8fdc8c4e4 100644 --- a/spec/__mocks__/electron.ts +++ b/spec/__mocks__/electron.ts @@ -3,11 +3,13 @@ import * as path from 'path'; const ipcEmitter = new EventEmitter(); const appName: string = 'Symphony'; -const executableName = '/Symphony.exe'; +const executableName: string = '/Symphony.exe'; +const isReady: boolean = true; interface IApp { getAppPath(): string; getPath(type: string): string; getName(): string; + isReady(): boolean; } interface IIpcMain { on(event: any, cb: any): void; @@ -35,6 +37,7 @@ const app: IApp = { return pathToConfigDir(); }, getName: () => appName, + isReady: () => isReady, }; // simple ipc mocks for render and main process ipc using diff --git a/spec/activityDetection.spec.ts b/spec/activityDetection.spec.ts new file mode 100644 index 000000000..42775e0ca --- /dev/null +++ b/spec/activityDetection.spec.ts @@ -0,0 +1,66 @@ +describe('activity detection', () => { + + const originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; + jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; + + beforeEach(() => { + jest.resetModules(); + jest.useFakeTimers(); + }); + + afterAll((done) => { + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; + done(); + }); + + it('should call `setWindowAndThreshold` correctly', () => { + const { activityDetection } = require('../src/app/activity-detection'); + + // mocking startActivityMonitor + jest.spyOn(activityDetection, 'startActivityMonitor') + .mockImplementation(() => jest.fn()); + + const spy = jest.spyOn(activityDetection, 'setWindowAndThreshold'); + + activityDetection.setWindowAndThreshold({}, 1000); + + expect(spy).toBeCalledWith({}, 1000); + }); + + it('should start activity monitor when `setWindowAndThreshold` is called', () => { + const { activityDetection } = require('../src/app/activity-detection'); + + const spy = jest.spyOn(activityDetection, 'startActivityMonitor') + .mockImplementation(() => jest.fn()); + + activityDetection.setWindowAndThreshold({}, 1000); + + expect(spy).toBeCalled(); + }); + + it('should call `activity` when `startActivityMonitor` is called', () => { + const { activityDetection } = require('../src/app/activity-detection'); + const electron = require('./__mocks__/electron'); + + electron.powerMonitor = { querySystemIdleTime: jest.fn().mockImplementation((cb) => cb(15)) }; + + const spy = jest.spyOn(activityDetection, 'activity'); + + activityDetection.startActivityMonitor(); + + jest.runOnlyPendingTimers(); + + expect(spy).toBeCalledWith(15); + }); + + it('should call `sendActivity` when period was greater than idleTime', () => { + const { activityDetection } = require('../src/app/activity-detection'); + // period is this.idleThreshold = 60 * 60 * 1000; + const spy = jest.spyOn(activityDetection, 'sendActivity'); + + activityDetection.activity(50); + + expect(spy).toBeCalledWith(50 * 1000); + }); + +}); \ No newline at end of file