From 7de0f1f529d241db03bf4228afb2a968325c9f13 Mon Sep 17 00:00:00 2001 From: VICTOR RAPHAEL BRAGA DE SALES MASCARENHAS Date: Mon, 4 Feb 2019 12:23:58 -0200 Subject: [PATCH] typescript: activityDetection unit test (#560) --- spec/__mocks__/electron.ts | 14 +++++++- spec/activityDetection.spec.ts | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 spec/activityDetection.spec.ts diff --git a/spec/__mocks__/electron.ts b/spec/__mocks__/electron.ts index c4f60dc2..9c65c59c 100644 --- a/spec/__mocks__/electron.ts +++ b/spec/__mocks__/electron.ts @@ -2,12 +2,15 @@ import { EventEmitter } from 'events'; import * as path from 'path'; const ipcEmitter = new EventEmitter(); +const mockIdleTime: number = 15; 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; @@ -19,6 +22,9 @@ interface IIpcRenderer { send(event: any, cb: any): void; removeListener(eventName: any, cb: any): void; } +interface IPowerMonitor { + querySystemIdleTime(): void; +} // use config provided by test framework const pathToConfigDir = (): string => { @@ -35,6 +41,7 @@ const app: IApp = { return pathToConfigDir(); }, getName: () => appName, + isReady: () => isReady, }; // simple ipc mocks for render and main process ipc using @@ -55,6 +62,10 @@ const ipcMain: IIpcMain = { }, }; +const powerMonitor: IPowerMonitor = { + querySystemIdleTime: jest.fn().mockImplementation((cb) => cb(mockIdleTime)), +}; + const ipcRenderer: IIpcRenderer = { sendSync: (event, args) => { const listeners = ipcEmitter.listeners(event); @@ -88,6 +99,7 @@ export = { app, ipcMain, ipcRenderer, + powerMonitor, require: jest.fn(), match: jest.fn(), remote: jest.fn(), diff --git a/spec/activityDetection.spec.ts b/spec/activityDetection.spec.ts new file mode 100644 index 00000000..2f6dfe58 --- /dev/null +++ b/spec/activityDetection.spec.ts @@ -0,0 +1,63 @@ +describe('activity detection', () => { + const originalTimeout: number = jasmine.DEFAULT_TIMEOUT_INTERVAL; + jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; + let activityDetectionInstance; + + beforeEach(() => { + jest.resetModules(); + jest.useFakeTimers(); + // I did it for reset module imported between tests + const { activityDetection } = require('../src/app/activity-detection'); + activityDetectionInstance = activityDetection; + }); + + afterAll((done) => { + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; + done(); + }); + + it('should call `setWindowAndThreshold` correctly', () => { + // mocking startActivityMonitor + const spy: jest.SpyInstance = jest.spyOn(activityDetectionInstance, 'setWindowAndThreshold'); + const idleThresholdMock: number = 1000; + + jest.spyOn(activityDetectionInstance, 'startActivityMonitor') + .mockImplementation(() => jest.fn()); + + activityDetectionInstance.setWindowAndThreshold({}, idleThresholdMock); + + expect(spy).toBeCalledWith({}, 1000); + }); + + it('should start activity monitor when `setWindowAndThreshold` is called', () => { + const idleThresholdMock: number = 1000; + const spy: jest.SpyInstance = jest.spyOn(activityDetectionInstance, 'startActivityMonitor') + .mockImplementation(() => jest.fn()); + + activityDetectionInstance.setWindowAndThreshold({}, idleThresholdMock); + + expect(spy).toBeCalled(); + }); + + it('should call `activity` when `startActivityMonitor` is called', () => { + const spy: jest.SpyInstance = jest.spyOn(activityDetectionInstance, 'activity'); + + activityDetectionInstance.startActivityMonitor(); + + jest.runOnlyPendingTimers(); + + expect(spy).toBeCalled(); + }); + + it('should call `sendActivity` when period was greater than idleTime', () => { + // period is this.idleThreshold = 60 * 60 * 1000; + const mockIdleTime: number = 50; + const spy: jest.SpyInstance = jest.spyOn(activityDetectionInstance, 'sendActivity'); + const mockIdleTimeinMillis: number = mockIdleTime * 1000; + + activityDetectionInstance.activity(mockIdleTime); + + expect(spy).toBeCalledWith(mockIdleTimeinMillis); + }); + +});