Skip to content

Commit

Permalink
typescript: activityDetection unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor R. Braga de Sales Mascarenhasm committed Jan 25, 2019
1 parent efe914d commit e3f124f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
5 changes: 4 additions & 1 deletion spec/__mocks__/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,6 +37,7 @@ const app: IApp = {
return pathToConfigDir();
},
getName: () => appName,
isReady: () => isReady,
};

// simple ipc mocks for render and main process ipc using
Expand Down
66 changes: 66 additions & 0 deletions spec/activityDetection.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});

});

0 comments on commit e3f124f

Please sign in to comment.