11import BaseManager from '../../common/BaseManager' ;
22import DocumentAnnotator from '../DocumentAnnotator' ;
3+ import HighlightListener from '../../highlight/HighlightListener' ;
34import RegionManager from '../../region/RegionManager' ;
45import { Annotation , Event } from '../../@types' ;
56import { annotations as regions } from '../../region/__mocks__/data' ;
6- import { fetchAnnotationsAction , setSelectionAction } from '../../store' ;
7+ import { fetchAnnotationsAction } from '../../store' ;
78import { HighlightManager } from '../../highlight' ;
8- import { mockRange } from '../../store/selection/__mocks__/range' ;
99import { scrollToLocation } from '../../utils/scroll' ;
1010
11- jest . mock ( 'lodash/debounce' , ( ) => ( func : Function ) => func ) ;
1211jest . mock ( '../../highlight/HighlightManager' ) ;
13- jest . mock ( '../docUtil' , ( ) => ( {
14- getSelection : ( ) => ( { location : 1 , range : mockRange } ) ,
15- } ) ) ;
1612jest . mock ( '../../region/RegionManager' ) ;
1713jest . mock ( '../../utils/scroll' ) ;
1814
19- jest . useFakeTimers ( ) ;
20-
2115describe ( 'DocumentAnnotator' , ( ) => {
2216 const container = document . createElement ( 'div' ) ;
2317 const defaults = {
@@ -75,32 +69,27 @@ describe('DocumentAnnotator', () => {
7569 } ) ;
7670
7771 describe ( 'constructor()' , ( ) => {
78- test ( 'should add selectionchange event listener' , ( ) => {
79- jest . spyOn ( document , 'addEventListener' ) ;
80-
81- annotator = getAnnotator ( ) ;
72+ test ( 'should create HighlightListener if feature is enabled' , ( ) => {
73+ annotator = getAnnotator ( { features : { highlightText : true } } ) ;
8274
83- expect ( document . addEventListener ) . toHaveBeenCalledWith ( 'selectionchange' , annotator . handleSelectionChange ) ;
75+ expect ( annotator . highlightListener ) . toBeInstanceOf ( HighlightListener ) ;
8476 } ) ;
8577 } ) ;
8678
8779 describe ( 'destroy()' , ( ) => {
88- test ( 'should clear timeout and remove event handlers' , ( ) => {
89- annotator . selectionChangeTimer = 1 ;
80+ test ( 'should remove event handler ' , ( ) => {
81+ annotator = getAnnotator ( { features : { highlightText : true } } ) ;
82+
9083 jest . spyOn ( annotator , 'removeListener' ) ;
91- jest . spyOn ( document , 'removeEventListener ' ) ;
84+ jest . spyOn ( annotator . highlightListener as HighlightListener , 'destroy ' ) ;
9285
9386 annotator . destroy ( ) ;
9487
95- expect ( clearTimeout ) . toHaveBeenCalledWith ( 1 ) ;
9688 expect ( annotator . removeListener ) . toHaveBeenCalledWith (
9789 'annotations_mode_change' ,
9890 annotator . handleChangeMode ,
9991 ) ;
100- expect ( document . removeEventListener ) . toHaveBeenCalledWith (
101- 'selectionchange' ,
102- annotator . handleSelectionChange ,
103- ) ;
92+ expect ( annotator . highlightListener ?. destroy ) . toHaveBeenCalled ( ) ;
10493 } ) ;
10594 } ) ;
10695
@@ -205,24 +194,6 @@ describe('DocumentAnnotator', () => {
205194 } ) ;
206195 } ) ;
207196
208- describe ( 'handleSelectionChange()' , ( ) => {
209- test ( 'should clear selection and dispatch new selection' , ( ) => {
210- jest . spyOn ( annotator . store , 'dispatch' ) ;
211-
212- annotator . handleSelectionChange ( ) ;
213-
214- expect ( annotator . store . dispatch ) . toHaveBeenLastCalledWith ( setSelectionAction ( null ) ) ;
215- expect ( annotator . selectionChangeTimer ) . not . toBeUndefined ( ) ;
216- expect ( clearTimeout ) . toHaveBeenCalled ( ) ;
217-
218- jest . runAllTimers ( ) ;
219-
220- expect ( annotator . store . dispatch ) . toHaveBeenLastCalledWith (
221- setSelectionAction ( { location : 1 , range : mockRange } ) ,
222- ) ;
223- } ) ;
224- } ) ;
225-
226197 describe ( 'init()' , ( ) => {
227198 test ( 'should set the root and annotated element based on class name' , ( ) => {
228199 annotator . init ( 5 ) ;
@@ -241,12 +212,14 @@ describe('DocumentAnnotator', () => {
241212 expect ( annotator . renderPage ) . toHaveBeenCalledTimes ( 3 ) ;
242213 } ) ;
243214
244- test ( 'should clear previous selection' , ( ) => {
245- jest . spyOn ( annotator . store , 'dispatch' ) ;
215+ test ( 'should init HighlightListener' , ( ) => {
216+ annotator = getAnnotator ( { features : { highlightText : true } } ) ;
217+ annotator . annotatedEl = document . createElement ( 'div' ) ;
218+ jest . spyOn ( annotator . highlightListener as HighlightListener , 'init' ) ;
246219
247220 annotator . render ( ) ;
248221
249- expect ( annotator . store . dispatch ) . toHaveBeenCalledWith ( setSelectionAction ( null ) ) ;
222+ expect ( annotator . highlightListener ?. init ) . toHaveBeenCalledWith ( annotator . annotatedEl ) ;
250223 } ) ;
251224 } ) ;
252225
0 commit comments