From 3a9370064c468c658064b9aa166546768ff37d32 Mon Sep 17 00:00:00 2001 From: VICTOR RAPHAEL BRAGA DE SALES MASCARENHAS Date: Fri, 1 Mar 2019 10:39:49 -0300 Subject: [PATCH] protocol handle unit test (#576) --- spec/protocolHandler.spec.ts | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 spec/protocolHandler.spec.ts diff --git a/spec/protocolHandler.spec.ts b/spec/protocolHandler.spec.ts new file mode 100644 index 000000000..2ada635ff --- /dev/null +++ b/spec/protocolHandler.spec.ts @@ -0,0 +1,75 @@ +jest.mock('../src/app/window-actions', () => { + return { + activate: jest.fn(), + }; +}); + +jest.mock('../src/common/utils', () => { + return { + getCommandLineArgs: jest.fn(() => 'symphony://?userId=22222'), + }; +}); + +jest.mock('../src/common/env', () => { + return { + isWindowsOS: false, + }; +}); + +describe('protocol handler', () => { + let protocolHandlerInstance; + + beforeEach(() => { + jest.resetModules(); + const { protocolHandler } = require('../src/app/protocol-handler'); + protocolHandlerInstance = protocolHandler; + }); + + it('protocol uri should be null by default', () => { + expect(protocolHandlerInstance.protocolUri).toBeNull(); + }); + + it('protocol action should be called when uri is correct', () => { + protocolHandlerInstance.preloadWebContents = { send: jest.fn() }; + + const spy: jest.SpyInstance = jest.spyOn(protocolHandlerInstance.preloadWebContents, 'send'); + const uri: string = 'symphony://?userId=123456'; + const protocolAction: string = 'protocol-action'; + + protocolHandlerInstance.sendProtocol(uri); + + expect(spy).toBeCalledWith(protocolAction, 'symphony://?userId=123456'); + }); + + it('protocol action not should be called when uri is incorrect', () => { + protocolHandlerInstance.preloadWebContents = { send: jest.fn() }; + + const spy: jest.SpyInstance = jest.spyOn(protocolHandlerInstance.preloadWebContents, 'send'); + const uri: string = 'symphony---://?userId=123456'; + const protocolAction: string = 'protocol-action'; + + protocolHandlerInstance.sendProtocol(uri); + + expect(spy).not.toBeCalledWith(protocolAction, 'symphony://?userId=123456'); + }); + + it('protocol should get uri from `processArgv` when `getCommandLineArgs` is called', () => { + const { getCommandLineArgs } = require('../src/common/utils'); + + protocolHandlerInstance.processArgv(''); + + expect(getCommandLineArgs).toBeCalled(); + }); + + it('should be called `sendProtocol` when is windowsOS on `processArgs`', () => { + const env = require('../src/common/env'); + env.isWindowsOS = true; + + const spy: jest.SpyInstance = jest.spyOn(protocolHandlerInstance, 'sendProtocol'); + + protocolHandlerInstance.processArgv(''); + + expect(spy).toBeCalled(); + }); + +});