@@ -18,6 +18,7 @@ describe('src/elements/content-sidebar/activity-feed/useAnnotationThread', () =>
1818 const mockUseAnnotatorEventsResult = {
1919 emitAddAnnotationEndEvent : jest . fn ( ) ,
2020 emitAddAnnotationStartEvent : jest . fn ( ) ,
21+ emitAnnotationActiveChangeEvent : jest . fn ( ) ,
2122 emitDeleteAnnotationEndEvent : jest . fn ( ) ,
2223 emitDeleteAnnotationStartEvent : jest . fn ( ) ,
2324 emitUpdateAnnotationEndEvent : jest . fn ( ) ,
@@ -40,16 +41,19 @@ describe('src/elements/content-sidebar/activity-feed/useAnnotationThread', () =>
4041 const filePermissions = { can_annotate : true , can_view_annotations : true } ;
4142 const errorCallback = jest . fn ( ) ;
4243
44+ const getFileProps = props => ( {
45+ id : 'fileId' ,
46+ file_version : { id : '123' } ,
47+ permissions : filePermissions ,
48+ ...props ,
49+ } ) ;
50+
4351 const getHook = props =>
4452 renderHook ( ( ) =>
4553 useAnnotationThread ( {
4654 api : { } ,
4755 currentUser : { } ,
48- file : {
49- id : 'fileId' ,
50- file_version : { id : '123' } ,
51- permissions : filePermissions ,
52- } ,
56+ file : getFileProps ( ) ,
5357 annotationId : annotation . id ,
5458 errorCallback,
5559 eventEmitter : { } ,
@@ -77,20 +81,85 @@ describe('src/elements/content-sidebar/activity-feed/useAnnotationThread', () =>
7781 expect ( result . current . replies ) . toEqual ( [ ] ) ;
7882 } ) ;
7983
80- test ( 'should return correct values after fetch' , ( ) => {
84+ test ( 'should return correct values after fetch and call emitAnnotationActiveChangeEvent ' , ( ) => {
8185 const { replies, ...normalizedAnnotation } = annotation ;
8286 const mockHandleFetch = jest . fn ( ) . mockImplementation ( ( { successCallback } ) => successCallback ( annotation ) ) ;
8387 useAnnotationAPI . mockImplementation ( ( ) => ( {
8488 ...mockUseAnnotationAPIResult ,
8589 handleFetch : mockHandleFetch ,
8690 } ) ) ;
91+ const fileVersionId = '456' ;
92+ const file = getFileProps ( { file_version : { id : fileVersionId } } ) ;
8793
88- const { result } = getHook ( ) ;
94+ const { result } = getHook ( { file } ) ;
8995
9096 expect ( result . current . annotation ) . toEqual ( normalizedAnnotation ) ;
9197 expect ( result . current . isLoading ) . toEqual ( false ) ;
9298 expect ( result . current . error ) . toEqual ( undefined ) ;
9399 expect ( result . current . replies ) . toEqual ( replies ) ;
100+ expect ( mockUseAnnotatorEventsResult . emitAnnotationActiveChangeEvent ) . toBeCalledWith (
101+ annotation . id ,
102+ fileVersionId ,
103+ ) ;
104+ } ) ;
105+
106+ describe ( 'handleAnnotationEdit' , ( ) => {
107+ test ( 'should call handleEdit from useAnnotationAPI and call emitUpdateAnnotationStartEvent + emitUpdateAnnotationEndEvent' , ( ) => {
108+ const updatedText = 'new text' ;
109+ const updatedAnnotation = {
110+ id : annotation . id ,
111+ description : { message : updatedText } ,
112+ } ;
113+ const mockHandleEdit = jest . fn ( ) . mockImplementation ( ( { successCallback } ) => {
114+ successCallback ( updatedAnnotation ) ;
115+ } ) ;
116+ useAnnotationAPI . mockImplementation ( ( ) => ( {
117+ ...mockUseAnnotationAPIResult ,
118+ handleEdit : mockHandleEdit ,
119+ } ) ) ;
120+
121+ const { result } = getHook ( ) ;
122+ act ( ( ) => {
123+ result . current . annotationActions . handleAnnotationEdit ( annotation . id , updatedText , { can_edit : true } ) ;
124+ } ) ;
125+
126+ expect ( mockHandleEdit ) . toBeCalledWith ( {
127+ id : annotation . id ,
128+ permissions : { can_edit : true } ,
129+ text : updatedText ,
130+ successCallback : expect . any ( Function ) ,
131+ } ) ;
132+ expect ( mockUseAnnotatorEventsResult . emitUpdateAnnotationStartEvent ) . toBeCalledWith ( updatedAnnotation ) ;
133+ expect ( mockUseAnnotatorEventsResult . emitUpdateAnnotationEndEvent ) . toBeCalledWith ( updatedAnnotation ) ;
134+ } ) ;
135+ } ) ;
136+
137+ describe ( 'handleAnnotationDelete' , ( ) => {
138+ test ( 'should call handleDelete from useAnnotationAPI and call emitDeleteAnnotationStartEvent + emitDeleteAnnotationEndEvent' , ( ) => {
139+ const mockHandleDelete = jest . fn ( ) . mockImplementation ( ( { successCallback } ) => {
140+ successCallback ( ) ;
141+ } ) ;
142+ useAnnotationAPI . mockImplementation ( ( ) => ( {
143+ ...mockUseAnnotationAPIResult ,
144+ handleDelete : mockHandleDelete ,
145+ } ) ) ;
146+
147+ const { result } = getHook ( ) ;
148+ act ( ( ) => {
149+ result . current . annotationActions . handleAnnotationDelete ( {
150+ id : annotation . id ,
151+ permissions : { can_delete : true } ,
152+ } ) ;
153+ } ) ;
154+
155+ expect ( mockHandleDelete ) . toBeCalledWith ( {
156+ id : annotation . id ,
157+ permissions : { can_delete : true } ,
158+ successCallback : expect . any ( Function ) ,
159+ } ) ;
160+ expect ( mockUseAnnotatorEventsResult . emitDeleteAnnotationStartEvent ) . toBeCalledWith ( annotation . id ) ;
161+ expect ( mockUseAnnotatorEventsResult . emitDeleteAnnotationEndEvent ) . toBeCalledWith ( annotation . id ) ;
162+ } ) ;
94163 } ) ;
95164
96165 describe ( 'useAnnotationAPI' , ( ) => {
@@ -143,48 +212,6 @@ describe('src/elements/content-sidebar/activity-feed/useAnnotationThread', () =>
143212 expect ( mockOnAnnotationCreate ) . toBeCalledWith ( createdAnnotation ) ;
144213 } ) ;
145214
146- test ( 'should call handleAnnotationEdit with correct params' , ( ) => {
147- const mockHandleEdit = jest . fn ( ) ;
148- useAnnotationAPI . mockImplementation ( ( ) => ( {
149- ...mockUseAnnotationAPIResult ,
150- handleEdit : mockHandleEdit ,
151- } ) ) ;
152-
153- const { result } = getHook ( ) ;
154- act ( ( ) => {
155- result . current . annotationActions . handleAnnotationEdit ( annotation . id , 'new text' , { can_edit : true } ) ;
156- } ) ;
157-
158- expect ( mockHandleEdit ) . toBeCalledWith ( {
159- id : annotation . id ,
160- permissions : { can_edit : true } ,
161- text : 'new text' ,
162- successCallback : expect . any ( Function ) ,
163- } ) ;
164- } ) ;
165-
166- test ( 'should call handleAnnotationDelete with correct params' , ( ) => {
167- const mockHandleDelete = jest . fn ( ) ;
168- useAnnotationAPI . mockImplementation ( ( ) => ( {
169- ...mockUseAnnotationAPIResult ,
170- handleDelete : mockHandleDelete ,
171- } ) ) ;
172-
173- const { result } = getHook ( ) ;
174- act ( ( ) => {
175- result . current . annotationActions . handleAnnotationDelete ( {
176- id : annotation . id ,
177- permissions : { can_delete : true } ,
178- } ) ;
179- } ) ;
180-
181- expect ( mockHandleDelete ) . toBeCalledWith ( {
182- id : annotation . id ,
183- permissions : { can_delete : true } ,
184- successCallback : expect . any ( Function ) ,
185- } ) ;
186- } ) ;
187-
188215 test ( 'should call handleAnnotationStatusChange with correct params and set annotation state to pending' , ( ) => {
189216 const mockHandleStatusChange = jest . fn ( ) ;
190217 useAnnotationAPI . mockImplementation ( ( ) => ( {
0 commit comments