11import * as store from '../../store' ;
22import APIFactory from '../../api' ;
3- import BaseAnnotator from '../BaseAnnotator' ;
3+ import BaseAnnotator , { CSS_CONTAINER_CLASS , CSS_LOADED_CLASS } from '../BaseAnnotator' ;
44import { ANNOTATOR_EVENT } from '../../constants' ;
55import { Event , LegacyEvent } from '../../@types' ;
66import { Mode } from '../../store/common' ;
7+ import { setIsInitialized } from '../../store' ;
78
89jest . mock ( '../../api' ) ;
9- jest . mock ( '../../store' , ( ) => ( {
10- createStore : jest . fn ( ( ) => ( { dispatch : jest . fn ( ) } ) ) ,
11- removeAnnotationAction : jest . fn ( ) ,
12- fetchAnnotationsAction : jest . fn ( ) ,
13- fetchCollaboratorsAction : jest . fn ( ) ,
14- setActiveAnnotationIdAction : jest . fn ( ) ,
15- setVisibilityAction : jest . fn ( ) ,
16- toggleAnnotationModeAction : jest . fn ( ) ,
17- } ) ) ;
10+ jest . mock ( '../../store/createStore' ) ;
11+
12+ class MockAnnotator extends BaseAnnotator {
13+ protected getAnnotatedElement ( ) : HTMLElement | null | undefined {
14+ return this . containerEl ?. querySelector ( '.inner' ) ;
15+ }
16+ }
1817
1918describe ( 'BaseAnnotator' , ( ) => {
19+ const container = document . createElement ( 'div' ) ;
20+ container . innerHTML = `<div class="inner" />` ;
21+
2022 const defaults = {
2123 apiHost : 'https://api.box.com' ,
22- container : document . createElement ( 'div' ) ,
24+ container,
2325 file : {
2426 id : '12345' ,
2527 file_version : { id : '98765' } ,
@@ -34,7 +36,7 @@ describe('BaseAnnotator', () => {
3436 locale : 'en-US' ,
3537 token : '1234567890' ,
3638 } ;
37- const getAnnotator = ( options = { } ) : BaseAnnotator => new BaseAnnotator ( { ...defaults , ...options } ) ;
39+ const getAnnotator = ( options = { } ) : MockAnnotator => new MockAnnotator ( { ...defaults , ...options } ) ;
3840 let annotator = getAnnotator ( ) ;
3941
4042 beforeEach ( ( ) => {
@@ -107,13 +109,13 @@ describe('BaseAnnotator', () => {
107109
108110 describe ( 'destroy()' , ( ) => {
109111 test ( 'should remove the base class name from the root element' , ( ) => {
110- const rootEl = document . createElement ( 'div' ) ;
111- rootEl . classList . add ( 'ba' ) ;
112+ const containerEl = document . createElement ( 'div' ) ;
113+ containerEl . classList . add ( CSS_CONTAINER_CLASS ) ;
112114
113- annotator . rootEl = rootEl ;
115+ annotator . containerEl = containerEl ;
114116 annotator . destroy ( ) ;
115117
116- expect ( annotator . rootEl . classList ) . not . toContain ( 'ba' ) ;
118+ expect ( annotator . containerEl . classList ) . not . toContain ( CSS_CONTAINER_CLASS ) ;
117119 } ) ;
118120
119121 test ( 'should remove proper event handlers' , ( ) => {
@@ -129,11 +131,20 @@ describe('BaseAnnotator', () => {
129131 } ) ;
130132
131133 describe ( 'init()' , ( ) => {
132- test ( 'should set the root element based on class selector' , ( ) => {
134+ test ( 'should set its reference elements based on class selector' , ( ) => {
133135 annotator . init ( 5 ) ;
134136
135- expect ( annotator . rootEl ) . toBe ( defaults . container ) ;
136- expect ( annotator . rootEl && annotator . rootEl . classList ) . toContain ( 'ba' ) ;
137+ expect ( annotator . containerEl ) . toBeDefined ( ) ;
138+ expect ( annotator . containerEl ?. classList ) . toContain ( CSS_CONTAINER_CLASS ) ;
139+ expect ( annotator . annotatedEl ?. classList ) . toContain ( CSS_LOADED_CLASS ) ;
140+ } ) ;
141+
142+ test ( 'should dispatch all necessary actions' , ( ) => {
143+ annotator . init ( 1 , 180 ) ;
144+
145+ expect ( annotator . store . dispatch ) . toHaveBeenCalledWith ( store . setRotationAction ( 180 ) ) ;
146+ expect ( annotator . store . dispatch ) . toHaveBeenCalledWith ( store . setScaleAction ( 1 ) ) ;
147+ expect ( annotator . store . dispatch ) . toHaveBeenCalledWith ( setIsInitialized ( ) ) ;
137148 } ) ;
138149
139150 test ( 'should emit error if no root element exists' , ( ) => {
@@ -142,7 +153,7 @@ describe('BaseAnnotator', () => {
142153 annotator . init ( 5 ) ;
143154
144155 expect ( annotator . emit ) . toBeCalledWith ( ANNOTATOR_EVENT . error , expect . any ( String ) ) ;
145- expect ( annotator . rootEl ) . toBeNull ( ) ;
156+ expect ( annotator . containerEl ) . toBeNull ( ) ;
146157 } ) ;
147158 } ) ;
148159
@@ -155,8 +166,8 @@ describe('BaseAnnotator', () => {
155166 } ) ;
156167
157168 test ( 'should call their underlying methods' , ( ) => {
158- annotator . emit ( LegacyEvent . SCALE , { scale : 1 } ) ;
159- expect ( annotator . init ) . toHaveBeenCalledWith ( 1 ) ;
169+ annotator . emit ( LegacyEvent . SCALE , { rotationAngle : 0 , scale : 1 } ) ;
170+ expect ( annotator . init ) . toHaveBeenCalledWith ( 1 , 0 ) ;
160171
161172 annotator . emit ( Event . ACTIVE_SET , 12345 ) ;
162173 expect ( annotator . setActiveId ) . toHaveBeenCalledWith ( 12345 ) ;
@@ -177,36 +188,37 @@ describe('BaseAnnotator', () => {
177188
178189 describe ( 'setVisibility()' , ( ) => {
179190 test . each ( [ true , false ] ) ( 'should hide/show annotations if visibility is %p' , visibility => {
180- annotator . rootEl = defaults . container ;
191+ annotator . init ( 1 ) ;
181192 annotator . setVisibility ( visibility ) ;
182- expect ( annotator . rootEl . classList . contains ( 'is-hidden' ) ) . toEqual ( ! visibility ) ;
193+ expect ( annotator . containerEl ?. classList . contains ( 'is-hidden' ) ) . toEqual ( ! visibility ) ;
194+ } ) ;
195+
196+ test ( 'should do nothing if the root element is not defined' , ( ) => {
197+ annotator . containerEl = document . querySelector ( 'nonsense' ) as HTMLElement ;
198+ annotator . setVisibility ( true ) ;
199+ expect ( annotator . containerEl ?. classList ) . toBeFalsy ( ) ;
183200 } ) ;
184201 } ) ;
185202
186203 describe ( 'setActiveAnnotationId()' , ( ) => {
187204 test . each ( [ null , '12345' ] ) ( 'should dispatch setActiveAnnotationIdAction with id %s' , id => {
188205 annotator . setActiveId ( id ) ;
189- expect ( annotator . store . dispatch ) . toBeCalled ( ) ;
190- expect ( store . setActiveAnnotationIdAction ) . toBeCalledWith ( id ) ;
206+ expect ( annotator . store . dispatch ) . toBeCalledWith ( store . setActiveAnnotationIdAction ( id ) ) ;
191207 } ) ;
192208 } ) ;
193209
194210 describe ( 'removeAnnotation' , ( ) => {
195- test ( 'should dispatch deleteActiveAnnotationAction ' , ( ) => {
211+ test ( 'should dispatch removeActiveAnnotationAction with the specified id ' , ( ) => {
196212 const id = '123' ;
197213 annotator . removeAnnotation ( id ) ;
198-
199- expect ( annotator . store . dispatch ) . toBeCalled ( ) ;
200- expect ( store . removeAnnotationAction ) . toBeCalledWith ( id ) ;
214+ expect ( annotator . store . dispatch ) . toBeCalledWith ( store . removeAnnotationAction ( id ) ) ;
201215 } ) ;
202216 } ) ;
203217
204218 describe ( 'toggleAnnotationMode()' , ( ) => {
205219 test ( 'should dispatch toggleAnnotationModeAction with specified mode' , ( ) => {
206220 annotator . toggleAnnotationMode ( 'region' as Mode ) ;
207-
208- expect ( annotator . store . dispatch ) . toBeCalled ( ) ;
209- expect ( store . toggleAnnotationModeAction ) . toBeCalledWith ( 'region' ) ;
221+ expect ( annotator . store . dispatch ) . toBeCalledWith ( store . toggleAnnotationModeAction ( 'region' as Mode ) ) ;
210222 } ) ;
211223 } ) ;
212224} ) ;
0 commit comments