Skip to content

Commit

Permalink
Fix: Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Oct 16, 2018
1 parent 97b8393 commit d6840c4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/Annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 2 additions & 9 deletions src/__tests__/Annotator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<div class="bp-header-container"></div>
const html = `<div class="bp-header"></div>
<button class="bp-btn-annotate"></button>
<div class="annotated-element"></div>`;

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/AnnotationModeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ class AnnotationModeController extends EventEmitter {
* @return {void}
*/
showButton(): void {
if (!this.permissions.can_annotate || !this.modeButton) {
return;
}

this.buttonEl = this.getButton(this.modeButton.selector);
if (this.buttonEl) {
this.buttonEl.title = this.modeButton.title;
Expand Down
31 changes: 8 additions & 23 deletions src/controllers/__tests__/AnnotationModeController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ describe('controllers/AnnotationModeController', () => {
};
controller.getLocation = jest.fn();
controller.annotatedElement = rootElement;
controller.permissions = {
can_annotate: true
};
});

afterEach(() => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
Expand All @@ -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.can_annotate = false;
controller.showButton();
expect(buttonEl.classList).toContain(CLASS_HIDDEN);
});
});

describe('hideButton()', () => {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
});

Expand Down

0 comments on commit d6840c4

Please sign in to comment.