@@ -23,6 +23,8 @@ jest.mock('box-ui-elements/es/components/form-elements/draft-js-mention-selector
2323 getFormattedCommentText : jest . fn ( ( ) => ( { hasMention : false , text : 'test' } ) ) ,
2424} ) ) ;
2525
26+ jest . mock ( 'lodash/debounce' , ( ) => ( func : Function ) => func ) ;
27+
2628describe ( 'components/Popups/ReplyField' , ( ) => {
2729 const defaults : Props = {
2830 className : 'ba-Popup-field' ,
@@ -32,6 +34,7 @@ describe('components/Popups/ReplyField', () => {
3234 ] ,
3335 cursorPosition : 0 ,
3436 editorState : mockEditorState ,
37+ fetchCollaborators : jest . fn ( ) ,
3538 isDisabled : false ,
3639 onChange : jest . fn ( ) ,
3740 setCursorPosition : jest . fn ( ) ,
@@ -66,80 +69,36 @@ describe('components/Popups/ReplyField', () => {
6669 test ( 'should handle the editor change event' , ( ) => {
6770 const wrapper = getWrapper ( ) ;
6871 const editor = wrapper . find ( Editor ) ;
72+ const instance = wrapper . instance ( ) ;
73+
74+ const fetchCollaboratorsSpy = jest . spyOn ( instance , 'fetchCollaborators' ) ;
6975
7076 editor . simulate ( 'change' , mockEditorState ) ;
7177
7278 expect ( defaults . onChange ) . toBeCalledWith ( mockEditorState ) ;
79+ expect ( fetchCollaboratorsSpy ) . toHaveBeenCalled ( ) ;
7380 } ) ;
7481 } ) ;
7582
76- describe ( 'getCollaborators ()' , ( ) => {
77- test ( 'should return empty list if no activeMention' , ( ) => {
83+ describe ( 'fetchCollaborators ()' , ( ) => {
84+ test ( 'should not call fetchCollaborators if no activeMention or empty query ' , ( ) => {
7885 const wrapper = getWrapper ( ) ;
7986 const instance = wrapper . instance ( ) ;
8087
8188 getActiveMentionForEditorState . mockReturnValueOnce ( null ) ;
89+ instance . fetchCollaborators ( mockEditorState ) ;
8290
83- expect ( instance . getCollaborators ( ) ) . toHaveLength ( 0 ) ;
84- } ) ;
85-
86- test ( 'should return full collaborators list if mentionString length is less than 2' , ( ) => {
87- const wrapper = getWrapper ( ) ;
88- const instance = wrapper . instance ( ) ;
89-
90- const mockMentionShort = {
91- ...mockMention ,
92- mentionString : '' ,
93- } ;
94-
95- getActiveMentionForEditorState . mockReturnValueOnce ( mockMentionShort ) ;
96-
97- expect ( instance . getCollaborators ( ) ) . toMatchObject ( defaults . collaborators ) ;
98- } ) ;
99-
100- test ( 'should filter invalid items in collaborators' , ( ) => {
101- const wrapper = getWrapper ( ) ;
102- const instance = wrapper . instance ( ) ;
103-
104- // mockMention and defaults.collaborators don't match
105-
106- expect ( instance . getCollaborators ( ) ) . toHaveLength ( 0 ) ;
107- } ) ;
108-
109- test ( 'should filter items based on item name' , ( ) => {
110- const mockMentionTest2 = {
111- ...mockMention ,
112- mentionString : 'test2' ,
113- } ;
114-
115- const wrapper = getWrapper ( ) ;
116- const instance = wrapper . instance ( ) ;
91+ expect ( defaults . fetchCollaborators ) . not . toHaveBeenCalled ( ) ;
11792
118- getActiveMentionForEditorState . mockReturnValueOnce ( mockMentionTest2 ) ;
119-
120- expect ( instance . getCollaborators ( ) ) . toMatchObject ( [ defaults . collaborators [ 1 ] ] ) ;
121- } ) ;
93+ getActiveMentionForEditorState . mockReturnValueOnce ( { mentionString : '' } ) ;
94+ instance . fetchCollaborators ( mockEditorState ) ;
12295
123- test ( 'should filter items based on item email' , ( ) => {
124- const mockCollabs = [
125- {
126- id : 'testid3' ,
127- name : 'test3' ,
128- item : { id : 'testid3' , name : 'test3' , type : 'group' , email : 'test3@box.com' } ,
129- } ,
130- ...defaults . collaborators ,
131- ] ;
132- const mockMentionEmail = {
133- ...mockMention ,
134- mentionString : 'box.com' ,
135- } ;
136-
137- const wrapper = getWrapper ( { collaborators : mockCollabs } ) ;
138- const instance = wrapper . instance ( ) ;
96+ expect ( defaults . fetchCollaborators ) . not . toHaveBeenCalled ( ) ;
13997
140- getActiveMentionForEditorState . mockReturnValueOnce ( mockMentionEmail ) ;
98+ getActiveMentionForEditorState . mockReturnValueOnce ( { mentionString : 'test' } ) ;
99+ instance . fetchCollaborators ( mockEditorState ) ;
141100
142- expect ( instance . getCollaborators ( ) ) . toMatchObject ( [ mockCollabs [ 0 ] ] ) ;
101+ expect ( defaults . fetchCollaborators ) . toHaveBeenCalledWith ( 'test' ) ;
143102 } ) ;
144103 } ) ;
145104
@@ -266,7 +225,6 @@ describe('components/Popups/ReplyField', () => {
266225 let mockKeyboardEvent : React . KeyboardEvent < HTMLDivElement > ;
267226 let wrapper : ShallowWrapper < Props , State , ReplyField > ;
268227 let instance : ReplyField ;
269- let getCollaboratorsSpy : jest . SpyInstance ;
270228 let stopDefaultEventSpy : jest . SpyInstance ;
271229 let setActiveItemSpy : jest . SpyInstance ;
272230
@@ -280,10 +238,6 @@ describe('components/Popups/ReplyField', () => {
280238 wrapper . setState ( { activeItemIndex : 0 , popupReference : ( 'popupReference' as unknown ) as VirtualElement } ) ;
281239 instance = wrapper . instance ( ) ;
282240
283- getCollaboratorsSpy = jest . spyOn ( instance , 'getCollaborators' ) . mockReturnValue ( [
284- { id : 'testid1' , name : 'test1' , item : { id : 'testid1' , name : 'test1' , type : 'user' } } ,
285- { id : 'testid2' , name : 'test2' , item : { id : 'testid2' , name : 'test2' , type : 'group' } } ,
286- ] ) ;
287241 stopDefaultEventSpy = jest . spyOn ( instance , 'stopDefaultEvent' ) ;
288242 setActiveItemSpy = jest . spyOn ( instance , 'setPopupListActiveItem' ) ;
289243 } ) ;
@@ -302,7 +256,7 @@ describe('components/Popups/ReplyField', () => {
302256 } ) ;
303257
304258 test ( 'should do nothing if collaborators length is 0' , ( ) => {
305- getCollaboratorsSpy . mockReturnValueOnce ( [ ] ) ;
259+ wrapper . setProps ( { collaborators : [ ] } ) ;
306260 instance . handleArrow ( mockKeyboardEvent ) ;
307261
308262 expect ( stopDefaultEventSpy ) . not . toBeCalled ( ) ;
0 commit comments