From cdc30af80893c6d3306686a158c2fdeb1894e8f2 Mon Sep 17 00:00:00 2001 From: Sumedha Pramod Date: Thu, 1 Nov 2018 18:31:42 -0700 Subject: [PATCH] Fix: Tests --- src/AnnotationThread.js | 4 --- src/__tests__/AnnotationThread-test.js | 9 ----- src/__tests__/Annotator-test.js | 2 +- src/controllers/AnnotationModeController.js | 2 +- src/controllers/DrawingModeController.js | 1 - .../AnnotationModeController-test.js | 15 +++----- .../__tests__/DrawingModeController-test.js | 1 - src/doc/DocHighlightThread.js | 35 ++++++++++--------- src/doc/__tests__/DocHighlightThread-test.js | 10 +----- src/drawing/DrawingThread.js | 10 +++--- src/drawing/__tests__/DrawingThread-test.js | 1 - 11 files changed, 29 insertions(+), 61 deletions(-) diff --git a/src/AnnotationThread.js b/src/AnnotationThread.js index 1c5c6275b..decf9144b 100644 --- a/src/AnnotationThread.js +++ b/src/AnnotationThread.js @@ -120,10 +120,6 @@ class AnnotationThread extends EventEmitter { this.element = null; } - - // $FlowFixMe - const { page } = this.location; - this.emit(THREAD_EVENT.render, { page }); } /** diff --git a/src/__tests__/AnnotationThread-test.js b/src/__tests__/AnnotationThread-test.js index d8ffa27e7..b1a85c0da 100644 --- a/src/__tests__/AnnotationThread-test.js +++ b/src/__tests__/AnnotationThread-test.js @@ -57,15 +57,6 @@ describe('AnnotationThread', () => { it('should unbind listeners and remove thread element and broadcast that the thread was deleted', () => { thread.destroy(); expect(thread.unbindDOMListeners).toBeCalled(); - expect(thread.emit).not.toBeCalledWith(THREAD_EVENT.threadDelete); - expect(thread.unmountPopover).toBeCalled(); - }); - - it('should emit annotationthreaddeleted only if thread is not in a pending state', () => { - thread.state = STATES.inactive; - thread.destroy(); - expect(thread.unbindDOMListeners).toBeCalled(); - expect(thread.emit).toBeCalledWith(THREAD_EVENT.threadDelete); expect(thread.unmountPopover).toBeCalled(); }); }); diff --git a/src/__tests__/Annotator-test.js b/src/__tests__/Annotator-test.js index 5a6053366..ebab43e23 100644 --- a/src/__tests__/Annotator-test.js +++ b/src/__tests__/Annotator-test.js @@ -486,7 +486,7 @@ describe('Annotator', () => { expect(thread.state).toEqual(STATES.active); expect(thread.save).toBeCalledWith(TYPES.point, 'text'); - expect(annotator.emit).toBeCalledWith(THREAD_EVENT.threadSave, expect.any(Object)); + expect(annotator.emit).toBeCalledWith(THREAD_EVENT.save, expect.any(Object)); expect(result).not.toBeNull(); expect(thread.renderAnnotationPopover).toBeCalled(); }); diff --git a/src/controllers/AnnotationModeController.js b/src/controllers/AnnotationModeController.js index 25daabfc4..de692882a 100644 --- a/src/controllers/AnnotationModeController.js +++ b/src/controllers/AnnotationModeController.js @@ -512,7 +512,7 @@ class AnnotationModeController extends EventEmitter { this.visibleThreadID = null; break; case THREAD_EVENT.render: - if (eventData.page) { + if (eventData && eventData.page) { this.renderPage(eventData.page); } else { this.render(); diff --git a/src/controllers/DrawingModeController.js b/src/controllers/DrawingModeController.js index 61f7f2865..ecff6d792 100644 --- a/src/controllers/DrawingModeController.js +++ b/src/controllers/DrawingModeController.js @@ -296,7 +296,6 @@ class DrawingModeController extends AnnotationModeController { this.currentThread = undefined; this.selectedThread = thread; - // this.registerThread(thread); this.unbindListeners(); // Clear existing canvases diff --git a/src/controllers/__tests__/AnnotationModeController-test.js b/src/controllers/__tests__/AnnotationModeController-test.js index 2847e730d..9fca01504 100644 --- a/src/controllers/__tests__/AnnotationModeController-test.js +++ b/src/controllers/__tests__/AnnotationModeController-test.js @@ -379,10 +379,9 @@ describe('controllers/AnnotationModeController', () => { it('should internally keep track of the registered thread', () => { // eslint-disable-next-line new-cap - const pageThreads = { - all: jest.fn().mockReturnValue([thread]), - remove: jest.fn() - }; + const pageThreads = new rbush(); + pageThreads.all = jest.fn().mockReturnValue([thread]); + pageThreads.remove = jest.fn(); controller.annotations = { 1: pageThreads }; @@ -468,19 +467,13 @@ describe('controllers/AnnotationModeController', () => { }); it('should re-render the annotations on render', () => { - controller.handleThreadEvents(thread, { event: THREAD_EVENT.render, eventData: 1 }); + controller.handleThreadEvents(thread, { event: THREAD_EVENT.render, eventData: { page: 1 } }); expect(controller.renderPage).toBeCalled(); controller.handleThreadEvents(thread, { event: THREAD_EVENT.render }); expect(controller.render).toBeCalled(); }); - it('should unregister thread on threadDelete', () => { - controller.handleThreadEvents(thread, { event: THREAD_EVENT.threadDelete, data: {} }); - expect(controller.unregisterThread).toBeCalled(); - expect(controller.emit).toBeCalledWith(THREAD_EVENT.threadDelete, expect.any(Object)); - }); - it('should unregister thread on deleteError', () => { controller.handleThreadEvents(thread, { event: THREAD_EVENT.deleteError, data: {} }); expect(controller.emit).toBeCalledWith(CONTROLLER_EVENT.error, controller.localized.deleteError); diff --git a/src/controllers/__tests__/DrawingModeController-test.js b/src/controllers/__tests__/DrawingModeController-test.js index 8233b26d1..07d986a29 100644 --- a/src/controllers/__tests__/DrawingModeController-test.js +++ b/src/controllers/__tests__/DrawingModeController-test.js @@ -287,7 +287,6 @@ describe('controllers/DrawingModeController', () => { }); expect(controller.unbindListeners).toBeCalled(); expect(controller.bindListeners).toBeCalled(); - expect(controller.registerThread).toBeCalled(); expect(controller.currentThread).toBeUndefined(); expect(thread.handleStart).not.toBeCalled(); expect(thread.removeListener).toBeCalledWith('threadevent', expect.any(Function)); diff --git a/src/doc/DocHighlightThread.js b/src/doc/DocHighlightThread.js index 2a923cd21..b405cc3a8 100644 --- a/src/doc/DocHighlightThread.js +++ b/src/doc/DocHighlightThread.js @@ -36,6 +36,24 @@ class DocHighlightThread extends AnnotationThread { this.canComment = canComment; } + /** + * [destructor] + * + * @return {void} + */ + destroy() { + this.threadID = null; + + // $FlowFixMe + const { page } = this.location; + this.emit(THREAD_EVENT.render, { page }); + super.destroy(); + + if (this.state === STATES.pending) { + window.getSelection().removeAllRanges(); + } + } + /** * Cancels the first comment on the thread * @@ -64,23 +82,6 @@ class DocHighlightThread extends AnnotationThread { this.cancelFirstComment(); }; - /** - * [destructor] - * - * @return {void} - */ - destroy() { - this.threadID = null; - - // $FlowFixMe - this.emit(THREAD_EVENT.render, this.location.page); - super.destroy(); - - if (this.state === STATES.pending) { - window.getSelection().removeAllRanges(); - } - } - /** * Hides the highlight by cutting out the annotation from context. Note * that if there are any overlapping highlights, this will cut out diff --git a/src/doc/__tests__/DocHighlightThread-test.js b/src/doc/__tests__/DocHighlightThread-test.js index 46336026d..5e1cf2bae 100644 --- a/src/doc/__tests__/DocHighlightThread-test.js +++ b/src/doc/__tests__/DocHighlightThread-test.js @@ -61,18 +61,10 @@ describe('doc/DocHighlightThread', () => { describe('destroy()', () => { it('should destroy the thread', () => { thread.emit = jest.fn(); - thread.state = STATES.pending; - - // This stubs out a parent method by forcing the method we care about - // in the prototype of the prototype of DocHighlightThread (ie - // AnnotationThread's prototype) to be a stub - Object.defineProperty(Object.getPrototypeOf(DocHighlightThread.prototype), 'destroy', { - value: jest.fn() - }); thread.destroy(); expect(thread.element).toBeUndefined(); - expect(thread.emit).toBeCalledWith(THREAD_EVENT.render, 1); + expect(thread.emit).toBeCalledWith(THREAD_EVENT.render, { page: thread.location.page }); }); }); diff --git a/src/drawing/DrawingThread.js b/src/drawing/DrawingThread.js index c0e65eeb7..8b04f5be8 100644 --- a/src/drawing/DrawingThread.js +++ b/src/drawing/DrawingThread.js @@ -96,15 +96,13 @@ class DrawingThread extends AnnotationThread { window.cancelAnimationFrame(this.lastAnimationRequestId); } - if (this.state !== STATES.pending) { - // $FlowFixMe - const { page } = this.location; - this.emit(THREAD_EVENT.render, { page }); - } - this.unmountPopover(); this.reset(); super.destroy(); + + // $FlowFixMe + const { page } = this.location; + this.emit(THREAD_EVENT.render, { page }); } /** diff --git a/src/drawing/__tests__/DrawingThread-test.js b/src/drawing/__tests__/DrawingThread-test.js index 26043c0d7..9aca08cec 100644 --- a/src/drawing/__tests__/DrawingThread-test.js +++ b/src/drawing/__tests__/DrawingThread-test.js @@ -82,7 +82,6 @@ describe('drawing/DrawingThread', () => { expect(thread.getBrowserRectangularBoundary).toBeCalled(); expect(thread.concreteContext.clearRect).toBeCalled(); expect(thread.clearBoundary).toBeCalled(); - expect(thread.delete).toBeCalledWith({ id: '123abc' }); expect(thread.pathContainer).toEqual(null); }); });