SymphonyElectron/spec/protocolHandler.spec.ts

171 lines
4.5 KiB
TypeScript
Raw Normal View History

2020-09-25 04:46:33 -05:00
jest.mock('electron-log');
2019-03-01 07:39:49 -06:00
jest.mock('../src/app/window-actions', () => {
2021-01-29 00:25:40 -06:00
return {
activate: jest.fn(),
};
2019-03-01 07:39:49 -06:00
});
jest.mock('../src/common/utils', () => {
2021-01-29 00:25:40 -06:00
return {
getCommandLineArgs: jest.fn(() => 'symphony://?userId=22222'),
};
2019-03-01 07:39:49 -06:00
});
jest.mock('../src/app/window-handler', () => {
return {
windowHandler: {
url: '',
},
};
});
2019-03-01 07:39:49 -06:00
jest.mock('../src/common/env', () => {
2021-01-29 00:25:40 -06:00
return {
isWindowsOS: false,
isLinux: false,
isMac: true,
};
2019-03-01 07:39:49 -06:00
});
2020-09-25 04:46:33 -05:00
jest.mock('../src/common/logger', () => {
2021-01-29 00:25:40 -06:00
return {
logger: {
setLoggerWindow: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
verbose: jest.fn(),
debug: jest.fn(),
silly: jest.fn(),
},
};
2020-09-25 04:46:33 -05:00
});
jest.mock('../src/app/config-handler', () => {
return {
config: {
getUserConfigFields: jest.fn(() => ''),
},
};
});
2019-03-01 07:39:49 -06:00
describe('protocol handler', () => {
2021-01-29 00:25:40 -06:00
let protocolHandlerInstance;
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
beforeEach(() => {
jest.resetModules();
const { protocolHandler } = require('../src/app/protocol-handler');
protocolHandlerInstance = protocolHandler;
});
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
it('protocol uri should be null by default', () => {
expect(protocolHandlerInstance.protocolUri).toBe(
'symphony://?userId=22222',
);
});
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
it('protocol action should be called when uri is correct', () => {
protocolHandlerInstance.preloadWebContents = { send: jest.fn() };
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
const spy: jest.SpyInstance = jest.spyOn(
protocolHandlerInstance.preloadWebContents,
'send',
);
const uri: string = 'symphony://?userId=123456';
const protocolAction: string = 'protocol-action';
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.sendProtocol(uri);
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
expect(spy).toBeCalledWith(protocolAction, 'symphony://?userId=123456');
});
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
it('protocol activate should be called when uri is correct on macOS', () => {
const { activate } = require('../src/app/window-actions');
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.preloadWebContents = { send: jest.fn() };
const uri: string = 'symphony://?userId=123456';
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.sendProtocol(uri);
2021-01-29 00:25:40 -06:00
expect(activate).toBeCalledWith('main');
});
2021-01-29 00:25:40 -06:00
it('protocol activate should not be called when uri is correct on non macOS', () => {
const env = require('../src/common/env');
env.isMac = false;
2021-01-29 00:25:40 -06:00
const { activate } = require('../src/app/window-actions');
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.preloadWebContents = { send: jest.fn() };
const uri: string = 'symphony://?userId=123456';
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.sendProtocol(uri);
2021-01-29 00:25:40 -06:00
expect(activate).not.toBeCalled();
});
2021-01-29 00:25:40 -06:00
it('protocol action not should be called when uri is incorrect', () => {
protocolHandlerInstance.preloadWebContents = { send: jest.fn() };
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
const spy: jest.SpyInstance = jest.spyOn(
protocolHandlerInstance.preloadWebContents,
'send',
);
const uri: string = 'symphony---://?userId=123456';
const protocolAction: string = 'protocol-action';
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.sendProtocol(uri);
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
expect(spy).not.toBeCalledWith(protocolAction, 'symphony://?userId=123456');
});
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
it('protocol should get uri from `processArgv` when `getCommandLineArgs` is called', () => {
const { getCommandLineArgs } = require('../src/common/utils');
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.processArgv('');
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
expect(getCommandLineArgs).toBeCalled();
});
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
it('should be called `sendProtocol` when is windowsOS on `processArgs`', () => {
const env = require('../src/common/env');
env.isWindowsOS = true;
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
const spy: jest.SpyInstance = jest.spyOn(
protocolHandlerInstance,
'sendProtocol',
);
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.processArgv('');
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
expect(spy).toBeCalled();
});
2019-03-01 07:39:49 -06:00
2021-01-29 00:25:40 -06:00
it('should invoke `sendProtocol` when `setPreloadWebContents` is called and protocolUri is valid', () => {
protocolHandlerInstance.preloadWebContents = { send: jest.fn() };
protocolHandlerInstance.protocolUri = 'symphony://?userId=123456';
2021-01-29 00:25:40 -06:00
const spy: jest.SpyInstance = jest.spyOn(
protocolHandlerInstance,
'sendProtocol',
);
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.setPreloadWebContents({ send: jest.fn() });
expect(spy).toBeCalledWith('symphony://?userId=123456');
});
2021-01-29 00:25:40 -06:00
it('should not invoke `sendProtocol` when `setPreloadWebContents` is called and protocolUri is invalid', () => {
protocolHandlerInstance.preloadWebContents = { send: jest.fn() };
protocolHandlerInstance.protocolUri = null;
2021-01-29 00:25:40 -06:00
const spy: jest.SpyInstance = jest.spyOn(
protocolHandlerInstance,
'sendProtocol',
);
2021-01-29 00:25:40 -06:00
protocolHandlerInstance.setPreloadWebContents({ send: jest.fn() });
expect(spy).not.toBeCalled();
});
2019-03-01 07:39:49 -06:00
});