Typescript autolaunch unittest (#608)

* mocking electron-log

* auto launch controller unit test
This commit is contained in:
VICTOR RAPHAEL BRAGA DE SALES MASCARENHAS
2019-03-25 04:47:30 -03:00
committed by Kiran Niranjan
parent 82b57abaa7
commit de6502035b
5 changed files with 126 additions and 23 deletions

View File

@@ -15,6 +15,7 @@ interface IApp {
isReady(): boolean;
getVersion(): string;
on(): void;
setPath(value: string, path: string): void;
}
interface IIpcMain {
on(event: any, cb: any): void;
@@ -54,6 +55,7 @@ export const app: IApp = {
isReady: () => isReady,
getVersion: () => version,
on: () => jest.fn(),
setPath: () => jest.fn(),
};
// simple ipc mocks for render and main process ipc using
@@ -107,12 +109,6 @@ export const ipcRenderer: IIpcRenderer = {
},
};
export const session = {
defaultSession: {
clearCache: jest.fn(),
},
};
export const shell = {
openExternal: jest.fn(),
};
@@ -152,6 +148,21 @@ export const dialog = {
showMessageBox: jest.fn(),
};
// tslint:disable-next-line:variable-name
export const BrowserWindow = {
getFocusedWindow: jest.fn(() => {
return {
isDestroyed: jest.fn(() => false),
};
}),
};
export const session = {
defaultSession: {
clearCache: jest.fn(),
},
};
export const remote = {
app,
getCurrentWindow,

View File

@@ -1,3 +1,5 @@
jest.mock('electron-log');
describe('activity detection', () => {
const originalTimeout: number = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

View File

@@ -0,0 +1,88 @@
import { autoLaunchInstance } from '../src/app/auto-launch-controller';
import { config } from '../src/app/config-handler';
import * as electron from './__mocks__/electron';
jest.mock('electron-log');
jest.mock('../src/app/config-handler', () => {
return {
config: {
getGlobalConfigFields: jest.fn(() => ''),
getConfigFields: jest.fn(() => {
return {
launchOnStartup: true,
};
}),
},
};
});
describe('auto launch controller', async () => {
beforeEach(() => {
jest.spyOn(config, 'getConfigFields').mockImplementation(() => {
return {
launchOnStartup: true,
};
});
jest.clearAllMocks();
});
it('should call `enableAutoLaunch` correctly', async () => {
const spyFn = 'enable';
const spy = jest.spyOn(autoLaunchInstance, spyFn);
await autoLaunchInstance.enableAutoLaunch();
expect(spy).toBeCalled();
});
it('should call `disableAutoLaunch` correctly', async () => {
const spyFn = 'disable';
const spy = jest.spyOn(autoLaunchInstance, spyFn);
await autoLaunchInstance.disableAutoLaunch();
expect(spy).toBeCalled();
});
it('should call `isAutoLaunchEnabled` correctly', async () => {
const spyFn = 'isEnabled';
const spy = jest.spyOn(autoLaunchInstance, spyFn);
await autoLaunchInstance.isAutoLaunchEnabled();
expect(spy).toBeCalled();
});
it('should fail `enableAutoLaunch` when catch is trigged', async () => {
const spyFn = 'showMessageBox';
const spy = jest.spyOn(electron.dialog, spyFn);
autoLaunchInstance.enable = jest.fn(() => Promise.reject());
await autoLaunchInstance.enableAutoLaunch();
expect(spy).toBeCalled();
});
it('should fail `disableAutoLaunch` when catch is trigged', async () => {
const spyFn = 'showMessageBox';
const spy = jest.spyOn(electron.dialog, spyFn);
autoLaunchInstance.disable = jest.fn(() => Promise.reject());
await autoLaunchInstance.disableAutoLaunch();
expect(spy).toBeCalled();
});
it('should enable AutoLaunch when `handleAutoLaunch` is called', async () => {
const spyFn = 'enableAutoLaunch';
const spy = jest.spyOn(autoLaunchInstance, spyFn);
jest.spyOn(autoLaunchInstance,'isAutoLaunchEnabled').mockImplementation(() => false);
await autoLaunchInstance.handleAutoLaunch();
expect(spy).toBeCalled();
});
it('should disable AutoLaunch when `handleAutoLaunch` is called', async () => {
jest.spyOn(config, 'getConfigFields').mockImplementation(() => {
return {
launchOnStartup: false,
};
});
const spyFn = 'disableAutoLaunch';
const spy = jest.spyOn(autoLaunchInstance, spyFn);
jest.spyOn(autoLaunchInstance,'isAutoLaunchEnabled').mockImplementation(() => true);
await autoLaunchInstance.handleAutoLaunch();
expect(spy).toBeCalled();
});
});

View File

@@ -3,6 +3,8 @@ import * as os from 'os';
import * as path from 'path';
import { IConfig } from '../src/app/config-handler';
jest.mock('electron-log');
describe('config', () => {
const configFileName: string = 'Symphony.config';
let userConfigDir: string;

View File

@@ -1,4 +1,18 @@
import { ILogMsg } from '../src/common/logger';
import * as os from 'os';
jest.mock('../src/common/utils.ts', () => {
return {
getCommandLineArgs: jest.fn(() => os.tmpdir()),
};
});
jest.mock('../src/common/env', () => {
return {
isElectronQA: false,
};
});
jest.mock('electron-log');
describe('logger', () => {
let instance;
@@ -9,10 +23,10 @@ describe('logger', () => {
jest.resetModules();
});
it('when no logger registered then queue items', () => {
it('when no logger registered then queue items', () => {
instance.debug('test');
instance.debug('test2');
const queue: ILogMsg[] = instance.getLogQueue();
const queue = instance.getLogQueue();
expect(queue.length).toBe(2);
});
@@ -30,49 +44,37 @@ describe('logger', () => {
it('should call `logger.error` correctly', () => {
const spy = jest.spyOn(instance, 'log');
instance.error('test error', { error: 'test error' });
expect(spy).toBeCalledWith('error', 'test error', [{ error: 'test error' }]);
});
it('should call `logger.warn` correctly', () => {
const spy = jest.spyOn(instance, 'log');
instance.warn('test warn', { warn: 'test warn' });
expect(spy).toBeCalledWith('warn', 'test warn', [{ warn: 'test warn' }]);
});
it('should call `logger.debug` correctly', () => {
const spy = jest.spyOn(instance, 'log');
instance.debug('test debug', { debug: 'test debug' });
expect(spy).toBeCalledWith('debug', 'test debug', [{ debug: 'test debug' }]);
});
it('should call `logger.info` correctly', () => {
const spy = jest.spyOn(instance, 'log');
instance.info('test info', { info: 'test info' });
expect(spy).toBeCalledWith('info', 'test info', [{ info: 'test info' }]);
});
it('should call `logger.verbose` correctly', () => {
const spy = jest.spyOn(instance, 'log');
instance.verbose('test verbose', { verbose: 'test verbose' });
expect(spy).toBeCalledWith('verbose', 'test verbose', [{ verbose: 'test verbose' }]);
});
it('should call `logger.silly` correctly', () => {
const spy = jest.spyOn(instance, 'log');
instance.silly('test silly', { silly: 'test silly' });
expect(spy).toBeCalledWith('silly', 'test silly', [{ silly: 'test silly' }]);
});
@@ -80,10 +82,8 @@ describe('logger', () => {
const spyLog = jest.spyOn(instance, 'log');
const spySendToCloud = jest.spyOn(instance, 'sendToCloud');
instance.debug('test');
expect(spyLog).toBeCalled();
expect(spySendToCloud).toBeCalled();
});
it('should cap at 100 queued log messages', () => {