diff --git a/src/app/shared/context-menu/context-menu.component.ts b/src/app/shared/context-menu/context-menu.component.ts index 1a37405..b1b0d41 100644 --- a/src/app/shared/context-menu/context-menu.component.ts +++ b/src/app/shared/context-menu/context-menu.component.ts @@ -25,12 +25,7 @@ export class ContextMenu { let parentElement = el.nativeElement.parentElement; - parentElement.oncontextmenu = (event: MouseEvent) => { - event.preventDefault(); - event.stopPropagation(); - - this.onParentContextMenu(event); - }; + parentElement.oncontextmenu = this.eventHandler; } getText(item: ContextMenuItem): SafeHtml { @@ -43,6 +38,13 @@ export class ContextMenu { } } + private eventHandler(event: MouseEvent) { + event.preventDefault(); + event.stopPropagation(); + + this.onParentContextMenu(event); + } + private onParentContextMenu(event: MouseEvent) { this.menuService.closeAllMenus(); this.isOpen = true; diff --git a/test/app/mocks.js b/test/app/mocks.js index 95c21d7..2e30ea4 100644 --- a/test/app/mocks.js +++ b/test/app/mocks.js @@ -86,6 +86,23 @@ global.window.Reflect = Reflect; global.RxJs = require('rxjs/Rx'); global.expect = chai.expect; +global.ElementRefMock = { + nativeElement: { + parentElement: { + oncontextmenu: () => {} + }, + firstElementChild: { + style: {}, + getBoundingClientRect: () => { + return { + width: 10, + height: 10 + }; + } + } + } +}; + global.Chartist = { Pie(id, data, opts) { this.sum = function() { return 100; }; @@ -197,6 +214,23 @@ global.ModalServiceMock = function() { }; }; +global.ContextMenuServiceMock = function() { + var register = new RxJs.Subject(), + closeAll = new RxJs.Subject(); + + return { + registerCalled: register.asObservable(), + closeAllCalled: closeAll.asObservable(), + + registerMenu: () => { + register.next(true); + }, + closeAllMenus: () => { + closeAll.next(true); + } + }; +}; + global.StringsServiceMock = { stringsChanged: { subscribe(callback) { diff --git a/test/app/settings/auto-actions/auto-actions.component.spec.js b/test/app/settings/auto-actions/auto-actions.component.spec.js index 7dc3d5e..bb866b6 100644 --- a/test/app/settings/auto-actions/auto-actions.component.spec.js +++ b/test/app/settings/auto-actions/auto-actions.component.spec.js @@ -6,7 +6,7 @@ var dirs = '../../../../', describe('AutoActions', () => { var autoActions, - modalService; + imodalService; beforeEach(() => { modalService = new ModalServiceMock(); diff --git a/test/app/shared/context-menu/context-menu.component.spec.js b/test/app/shared/context-menu/context-menu.component.spec.js new file mode 100644 index 0000000..7ba9912 --- /dev/null +++ b/test/app/shared/context-menu/context-menu.component.spec.js @@ -0,0 +1,39 @@ +/* global expect ElementRefMock ContextMenuServiceMock SanitizerMock */ +var path = '../../../../build/shared/context-menu/', + ContextMenu = require(path + 'context-menu.component.js').ContextMenu; + +describe('ContextMenu', () => { + var contextMenu, + contextMenuService; + + beforeEach(() => { + contextMenuService = new ContextMenuServiceMock(); + contextMenu = new ContextMenu(ElementRefMock, contextMenuService, SanitizerMock); + }); + + it('has a function to get menu item text', () => { + expect(contextMenu.getText({ text: 'testing' })).to.equal('testing'); + }); + + it('has a function to call a callback', (done) => { + var callback = () => { + done(); + }; + + contextMenu.callAction(callback); + }); + + it('captures the parent oncontextmenu event', (done) => { + contextMenuService.closeAllCalled.subscribe( called => { + expect(called).to.equal(true); + done(); + }); + + contextMenu.eventHandler({ + pageX: 10, pageY: 10, + preventDefault: () => {}, + stopPropagation: () => {} + }); + }); +}); + diff --git a/test/app/shared/context-menu/context-menu.service.spec.js b/test/app/shared/context-menu/context-menu.service.spec.js new file mode 100644 index 0000000..0010cf0 --- /dev/null +++ b/test/app/shared/context-menu/context-menu.service.spec.js @@ -0,0 +1,30 @@ +/* global expect */ +var path = '../../../../build/shared/context-menu/', + ContextMenuItem = require(path + 'context-menu-item.model.js').ContextMenuItem, + ContextMenuService = require(path + 'context-menu.service.js').ContextMenuService; + +describe('ContextMenu', () => { + var contextMenuService; + + beforeEach(() => { + contextMenuService = new ContextMenuService(); + }); + + it('has a menus array', () => { + expect(contextMenuService.menus).to.be.an('array'); + }); + + it('has a registerMenu function', () => { + contextMenuService.registerMenu(new ContextMenuItem()); + + expect(contextMenuService.menus.length).to.equal(1); + }); + + it('has a closeAllMenus function', () => { + contextMenuService.registerMenu({ isOpen: true }); + contextMenuService.closeAllMenus({}); + + expect(contextMenuService.menus[0].isOpen).to.equal(false); + }); +}); +