Typescript chromeflags unittest (#612)

* fixing electron mock

* chrome flags unit test
This commit is contained in:
VICTOR RAPHAEL BRAGA DE SALES MASCARENHAS 2019-03-28 10:34:31 -03:00 committed by Kiran Niranjan
parent bb9d547ae3
commit 1276f066f4
3 changed files with 118 additions and 7 deletions

View File

@ -9,6 +9,7 @@ const executableName: string = '/Symphony.exe';
const isReady: boolean = true;
const version: string = '4.0.0';
interface IApp {
commandLine: any;
getAppPath(): string;
getPath(type: string): string;
getName(): string;
@ -57,6 +58,9 @@ export const app: IApp = {
getVersion: () => version,
on: () => jest.fn(),
setPath: () => jest.fn(),
commandLine: {
appendSwitch: jest.fn(),
},
};
// simple ipc mocks for render and main process ipc using
@ -118,12 +122,6 @@ export const shell = {
openExternal: jest.fn(),
};
// tslint:disable-next-line:variable-name
export const BrowserWindow = {
fromWebContents: (arg) => arg,
getAllWindows: jest.fn(() => []),
};
// tslint:disable-next-line:variable-name
export const Menu = {
buildFromTemplate: jest.fn(),
@ -157,6 +155,7 @@ const getCurrentWindow = jest.fn(() => {
export const dialog = {
showMessageBox: jest.fn(),
showErrorBox: jest.fn(),
};
// tslint:disable-next-line:variable-name
@ -166,6 +165,8 @@ export const BrowserWindow = {
isDestroyed: jest.fn(() => false),
};
}),
fromWebContents: (arg) => arg,
getAllWindows: jest.fn(() => []),
};
export const session = {

View File

@ -16,7 +16,6 @@ const getMainWindow = {
setAlwaysOnTop: jest.fn(),
};
jest.mock('../src/common/env', () => {
return {
isWindowsOS: true,

111
spec/chromeFlags.spec.ts Normal file
View File

@ -0,0 +1,111 @@
import { setChromeFlags } from '../src/app/chrome-flags';
import { config } from '../src/app/config-handler';
import { isDevEnv, isMac, isWindowsOS } from '../src/common/env';
import { getCommandLineArgs } from '../src/common/utils';
import { app } from './__mocks__/electron';
jest.mock('../src/common/utils', () => {
return {
config: {
getGlobalConfigFields: jest.fn(() => {
return {
customFlags: {
authServerWhitelist: 'url',
authNegotiateDelegateWhitelist: 'whitelist',
disableGpu: true,
},
};
}),
},
};
});
jest.mock('../src/common/utils', () => {
return {
getCommandLineArgs: jest.fn(() => '--chrome-flags=remote-debugging-port:5858,host-rules:MAP * 127.0.0.1'),
compareVersions: jest.fn(),
};
});
jest.mock('electron-log');
describe('chrome flags', () => {
beforeEach(() => {
(isDevEnv as any) = false;
(isMac as any) = true;
(isWindowsOS as any) = false;
(getCommandLineArgs as any) = jest.fn(() => '--chrome-flags=remote-debugging-port:5858,host-rules:MAP * 127.0.0.1');
config.getGlobalConfigFields = jest.fn(() => {
return {
customFlags: {
authServerWhitelist: 'url',
authNegotiateDelegateWhitelist: 'whitelist',
disableGpu: true,
},
};
});
jest.clearAllMocks();
});
it('should call `setChromeFlags` correctly', () => {
const spy = jest.spyOn(app.commandLine, 'appendSwitch');
setChromeFlags();
expect(spy).nthCalledWith(1, 'auth-negotiate-delegate-whitelist', 'url');
expect(spy).nthCalledWith(2, 'auth-server-whitelist', 'whitelist');
expect(spy).nthCalledWith(3, 'disable-background-timer-throttling', 'true');
expect(spy).nthCalledWith(4, 'disable-d3d11', true);
expect(spy).nthCalledWith(5, 'disable-gpu', true);
expect(spy).nthCalledWith(6, 'disable-gpu-compositing', true);
});
it('should call `setChromeFlags` correctly when `disableGpu` is false', () => {
config.getGlobalConfigFields = jest.fn(() => {
return {
customFlags: {
authServerWhitelist: 'url',
authNegotiateDelegateWhitelist: 'whitelist',
},
};
});
const spy = jest.spyOn(app.commandLine, 'appendSwitch');
setChromeFlags();
expect(spy).nthCalledWith(1, 'auth-negotiate-delegate-whitelist', 'url');
expect(spy).nthCalledWith(2, 'auth-server-whitelist', 'whitelist');
expect(spy).nthCalledWith(3, 'disable-background-timer-throttling', 'true');
expect(spy).not.nthCalledWith(4);
});
describe('`isDevEnv`', () => {
beforeEach(() => {
(isDevEnv as any) = true;
});
it('should call `setChromeFlags` correctly', () => {
const spy = jest.spyOn(app.commandLine, 'appendSwitch');
setChromeFlags();
expect(spy).nthCalledWith(1, 'auth-negotiate-delegate-whitelist', 'url');
expect(spy).nthCalledWith(2, 'auth-server-whitelist', 'whitelist');
expect(spy).nthCalledWith(3, 'disable-background-timer-throttling', 'true');
expect(spy).nthCalledWith(4, 'disable-d3d11', true);
expect(spy).nthCalledWith(5, 'disable-gpu', true);
expect(spy).nthCalledWith(6, 'disable-gpu-compositing', true);
expect(spy).nthCalledWith(7, 'remote-debugging-port', '5858');
expect(spy).nthCalledWith(8, 'host-rules', 'MAP * 127.0.0.1');
});
it('should return `undefined` when `chromeFlagsFromCmd` is null', () => {
(getCommandLineArgs as any) = jest.fn(() => null);
expect(setChromeFlags()).toBeUndefined();
});
it('should return `undefined` when `chromeFlagsArgs` is incorrect', () => {
(getCommandLineArgs as any) = jest.fn(() => 'testing');
expect(setChromeFlags()).toBeUndefined();
});
it('should return `undefined` when `flags[key]` is incorrect', () => {
(getCommandLineArgs as any) = jest.fn(() => '--chrome-flags=,');
expect(setChromeFlags()).toBeUndefined();
});
});
});