|
| 1 | +import { ContextMenus } from '../../../../add-on/src/lib/context-menus/ContextMenus' |
| 2 | +import {expect} from 'chai' |
| 3 | +import { before, describe, it } from 'mocha' |
| 4 | +import browserMock from 'sinon-chrome' |
| 5 | +import sinon from 'sinon' |
| 6 | + |
| 7 | +describe('lib/context-menus/ContextMenus', () => { |
| 8 | + let sinonSandbox |
| 9 | + |
| 10 | + before(function () { |
| 11 | + browserMock.runtime.id = 'testid' |
| 12 | + sinonSandbox = sinon.createSandbox() |
| 13 | + }) |
| 14 | + |
| 15 | + beforeEach(function () { |
| 16 | + sinonSandbox.restore() |
| 17 | + browserMock.contextMenus.onClicked.addListener.resetHistory() |
| 18 | + }) |
| 19 | + |
| 20 | + it('initializes and registers global listener', () => { |
| 21 | + const contextMenus = new ContextMenus() |
| 22 | + expect(contextMenus).to.be.an.instanceOf(ContextMenus) |
| 23 | + expect(contextMenus).to.have.property('contextMenuListeners') |
| 24 | + expect(contextMenus).to.have.property('log') |
| 25 | + expect(contextMenus).to.have.property('init') |
| 26 | + expect(contextMenus).to.have.property('queueListener') |
| 27 | + expect(contextMenus).to.have.property('create') |
| 28 | + expect(browserMock.contextMenus.onClicked.addListener.called).to.be.true |
| 29 | + }) |
| 30 | + |
| 31 | + it('queues listener and calls that when event is fired.', () => { |
| 32 | + const contextMenus = new ContextMenus() |
| 33 | + const globalListener = browserMock.contextMenus.onClicked.addListener.firstCall.args[0] |
| 34 | + const listenerSpy = sinonSandbox.spy() |
| 35 | + contextMenus.queueListener('testIdOne', listenerSpy) |
| 36 | + // emulate event |
| 37 | + globalListener({ menuItemId: 'testIdOne' }) |
| 38 | + expect(listenerSpy.called).to.be.true |
| 39 | + }) |
| 40 | + |
| 41 | + it('should allow adding listener to existing menuItemId', () => { |
| 42 | + const contextMenus = new ContextMenus() |
| 43 | + const globalListener = browserMock.contextMenus.onClicked.addListener.firstCall.args[0] |
| 44 | + const listenerSpyOne = sinonSandbox.spy() |
| 45 | + const listenerSpyTwo = sinonSandbox.spy() |
| 46 | + contextMenus.queueListener('testIdOne', listenerSpyOne) |
| 47 | + contextMenus.queueListener('testIdOne', listenerSpyTwo) |
| 48 | + // emulate event |
| 49 | + globalListener({ menuItemId: 'testIdOne' }) |
| 50 | + expect(listenerSpyOne.called).to.be.true |
| 51 | + expect(listenerSpyTwo.called).to.be.true |
| 52 | + }) |
| 53 | + |
| 54 | + it('should allow adding listener on the fly', () => { |
| 55 | + const contextMenus = new ContextMenus() |
| 56 | + const globalListener = browserMock.contextMenus.onClicked.addListener.firstCall.args[0] |
| 57 | + const listenerSpyOne = sinonSandbox.spy() |
| 58 | + const listenerSpyTwo = sinonSandbox.spy() |
| 59 | + contextMenus.queueListener('testIdOne', listenerSpyOne) |
| 60 | + contextMenus.queueListener('testIdTwo', listenerSpyTwo) |
| 61 | + // emulate event |
| 62 | + globalListener({ menuItemId: 'testIdOne' }) |
| 63 | + expect(listenerSpyOne.called).to.be.true |
| 64 | + globalListener({ menuItemId: 'testIdTwo' }) |
| 65 | + expect(listenerSpyTwo.called).to.be.true |
| 66 | + }) |
| 67 | + |
| 68 | + it('should create and queue listener', () => { |
| 69 | + const contextMenus = new ContextMenus() |
| 70 | + const globalListener = browserMock.contextMenus.onClicked.addListener.firstCall.args[0] |
| 71 | + const listenerSpy = sinonSandbox.spy() |
| 72 | + contextMenus.create({ id: 'testIdOne' }, listenerSpy) |
| 73 | + // emulate event |
| 74 | + globalListener({ menuItemId: 'testIdOne' }) |
| 75 | + expect(listenerSpy.called).to.be.true |
| 76 | + }) |
| 77 | + |
| 78 | + it('should not create multiple context menu items for the same menuItemId', () => { |
| 79 | + const contextMenus = new ContextMenus() |
| 80 | + const globalListener = browserMock.contextMenus.onClicked.addListener.firstCall.args[0] |
| 81 | + const listenerSpyOne = sinonSandbox.spy() |
| 82 | + const listenerSpyTwo = sinonSandbox.spy() |
| 83 | + contextMenus.create({ id: 'testIdOne' }, listenerSpyOne) |
| 84 | + contextMenus.create({ id: 'testIdOne' }, listenerSpyTwo) |
| 85 | + // emulate event |
| 86 | + globalListener({ menuItemId: 'testIdOne' }) |
| 87 | + expect(listenerSpyOne.called).to.be.true |
| 88 | + expect(listenerSpyTwo.called).to.be.true |
| 89 | + }) |
| 90 | + |
| 91 | + it('should create and queue listener for multiple items', () => { |
| 92 | + const contextMenus = new ContextMenus() |
| 93 | + const globalListener = browserMock.contextMenus.onClicked.addListener.firstCall.args[0] |
| 94 | + const listenerSpyOne = sinonSandbox.spy() |
| 95 | + const listenerSpyTwo = sinonSandbox.spy() |
| 96 | + contextMenus.create({ id: 'testIdOne' }, listenerSpyOne) |
| 97 | + contextMenus.create({ id: 'testIdTwo' }, listenerSpyTwo) |
| 98 | + // emulate event |
| 99 | + globalListener({ menuItemId: 'testIdOne' }) |
| 100 | + expect(listenerSpyOne.called).to.be.true |
| 101 | + globalListener({ menuItemId: 'testIdTwo' }) |
| 102 | + expect(listenerSpyTwo.called).to.be.true |
| 103 | + }) |
| 104 | + |
| 105 | + it('should throw error if id is not provided and callback is', () => { |
| 106 | + const contextMenus = new ContextMenus() |
| 107 | + expect(() => contextMenus.create({ }, () => {})).to.throw() |
| 108 | + }) |
| 109 | +}) |
0 commit comments