From 8149e125f0ee1ddd41f605db236b69f07f2a86ec Mon Sep 17 00:00:00 2001 From: Sumedha Pramod Date: Tue, 16 Oct 2018 12:28:32 -0700 Subject: [PATCH] Fix: Tests --- src/Annotator.js | 4 +- src/__tests__/Annotator-test.js | 11 +----- src/controllers/AnnotationModeController.js | 4 +- .../AnnotationModeController-test.js | 37 ++++++------------- 4 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/Annotator.js b/src/Annotator.js index 60c3d9690..5d59bb0df 100644 --- a/src/Annotator.js +++ b/src/Annotator.js @@ -14,7 +14,7 @@ import { ANNOTATOR_EVENT, CONTROLLER_EVENT, CLASS_ANNOTATIONS_LOADED, - SELECTOR_BOX_PREVIEW_HEADER + SELECTOR_BOX_PREVIEW_BASE_HEADER } from './constants'; import FileVersionAPI from './api/FileVersionAPI'; @@ -114,7 +114,7 @@ class Annotator extends EventEmitter { // If using box content preview header and no external header element was specified, // fallback to the container element if (this.options.header !== 'none' && !this.headerElement) { - this.headerElement = this.container.querySelector(SELECTOR_BOX_PREVIEW_HEADER); + this.headerElement = this.container.querySelector(SELECTOR_BOX_PREVIEW_BASE_HEADER); } if (!this.container) { diff --git a/src/__tests__/Annotator-test.js b/src/__tests__/Annotator-test.js index 126f6d0f5..38b55786e 100644 --- a/src/__tests__/Annotator-test.js +++ b/src/__tests__/Annotator-test.js @@ -7,14 +7,13 @@ import { ANNOTATOR_EVENT, THREAD_EVENT, CONTROLLER_EVENT, - SELECTOR_ANNOTATED_ELEMENT, - SELECTOR_BOX_PREVIEW_HEADER_CONTAINER + SELECTOR_ANNOTATED_ELEMENT } from '../constants'; let annotator; let controller; let thread; -const html = `
+const html = `
`; @@ -118,12 +117,6 @@ describe('Annotator', () => { expect(annotator.loadAnnotations).toBeCalled(); }); - it('should set the headerElement to the container as a fallback', () => { - annotator.options.header = 'light'; - annotator.init(5); - expect(annotator.headerElement).toEqual(document.querySelector(SELECTOR_BOX_PREVIEW_HEADER_CONTAINER)); - }); - it('should setup mobile dialog for mobile browsers', () => { annotator.isMobile = true; annotator.init(); diff --git a/src/controllers/AnnotationModeController.js b/src/controllers/AnnotationModeController.js index 086097d86..6e9147fe8 100644 --- a/src/controllers/AnnotationModeController.js +++ b/src/controllers/AnnotationModeController.js @@ -157,7 +157,7 @@ class AnnotationModeController extends EventEmitter { * @return {void} */ showButton(): void { - if (!this.permissions.canAnnotate || !this.modeButton) { + if (!this.permissions.can_annotate || !this.modeButton) { return; } @@ -177,7 +177,7 @@ class AnnotationModeController extends EventEmitter { * @return {void} */ hideButton() { - if (!this.permissions.canAnnotate || !this.modeButton) { + if (!this.permissions.can_annotate || !this.modeButton) { return; } diff --git a/src/controllers/__tests__/AnnotationModeController-test.js b/src/controllers/__tests__/AnnotationModeController-test.js index 91aebab52..f06295ee8 100644 --- a/src/controllers/__tests__/AnnotationModeController-test.js +++ b/src/controllers/__tests__/AnnotationModeController-test.js @@ -59,6 +59,9 @@ describe('controllers/AnnotationModeController', () => { }; controller.getLocation = jest.fn(); controller.annotatedElement = rootElement; + controller.permissions = { + can_annotate: true + }; }); afterEach(() => { @@ -111,31 +114,25 @@ describe('controllers/AnnotationModeController', () => { it('should hide the button if modeButton exists', () => { controller.modeButton = {}; + controller.hideButton = jest.fn(); controller.destroy(); expect(controller.hideButton).toBeCalled(); }); it('should not hide the button if modeButton does not exist', () => { controller.modeButton = undefined; + controller.hideButton = jest.fn(); controller.destroy(); expect(controller.hideButton).not.toBeCalled(); }); }); - describe('getButton', () => { + describe('getButton()', () => { it('should return the annotation mode button', () => { const buttonEl = document.createElement('button'); buttonEl.classList.add('class'); controller.headerElement = document.createElement('div'); controller.headerElement.appendChild(buttonEl); - - it('should remove listener from button', () => { - controller.buttonEl = { - removeEventListener: jest.fn() - }; - controller.destroy(); - expect(controller.buttonEl.removeEventListener).toBeCalled(); - }); }); it('should return null if no headerElement', () => { @@ -163,12 +160,6 @@ describe('controllers/AnnotationModeController', () => { controller.getButton = jest.fn().mockReturnValue(buttonEl); }); - it('should do nothing if user cannot annotate', () => { - controller.permissions.can_annotate = false; - controller.showButton(); - expect(buttonEl.classList).toContain(CLASS_HIDDEN); - }); - it('should do nothing if the button is not in the container', () => { controller.getButton = jest.fn(); controller.showButton(); @@ -180,13 +171,6 @@ describe('controllers/AnnotationModeController', () => { expect(buttonEl.classList).not.toContain(CLASS_HIDDEN); expect(buttonEl.addEventListener).toBeCalledWith('click', controller.toggleMode); }); - - it('should do nothing if no modeButton', () => { - controller.modeButton = undefined; - controller.permissions.canAnnotate = false; - controller.showButton(); - expect(buttonEl.classList).toContain(CLASS_HIDDEN); - }); }); describe('hideButton()', () => { @@ -205,12 +189,12 @@ describe('controllers/AnnotationModeController', () => { buttonEl.classList.add('selector'); buttonEl.addEventListener = jest.fn(); - controller.permissions = { canAnnotate: true }; + controller.permissions = { can_annotate: true }; controller.getButton = jest.fn().mockReturnValue(buttonEl); }); it('should do nothing if user cannot annotate', () => { - controller.permissions.canAnnotate = false; + controller.permissions.can_annotate = false; controller.hideButton(); expect(buttonEl.classList).not.toContain(CLASS_HIDDEN); }); @@ -228,7 +212,7 @@ describe('controllers/AnnotationModeController', () => { it('should do nothing if no modeButton', () => { controller.modeButton = undefined; - controller.permissions.canAnnotate = false; + controller.permissions.can_annotate = false; controller.hideButton(); expect(buttonEl.classList).not.toContain(CLASS_HIDDEN); }); @@ -262,6 +246,7 @@ describe('controllers/AnnotationModeController', () => { // Set up annotation mode controller.annotatedElement.classList.add(CLASS_ANNOTATION_MODE); controller.annotatedElement.classList.add(CLASS_ANNNOTATION_MODE_BACKGROUND); + controller.headerElement = document.createElement('div'); controller.buttonEl = document.createElement('button'); controller.buttonEl.classList.add(CLASS_ACTIVE); @@ -270,7 +255,7 @@ describe('controllers/AnnotationModeController', () => { expect(controller.emit).toBeCalledWith(CONTROLLER_EVENT.exit, expect.any(Object)); expect(controller.unbindListeners).toBeCalled(); expect(controller.emit).toBeCalledWith('binddomlisteners'); - expect(util.replaceHeader).toBeCalledWith(controller.container, SELECTOR_BOX_PREVIEW_BASE_HEADER); + expect(util.replaceHeader).toBeCalledWith(controller.headerElement, SELECTOR_BOX_PREVIEW_BASE_HEADER); }); });