11import React from 'react' ;
22import { mount , shallow } from 'enzyme' ;
3- import { ContentState , EditorState } from 'draft-js' ;
3+ import { EditorState } from 'draft-js' ;
44import sinon from 'sinon' ;
55
6+ import * as utils from '../utils' ;
67import DraftJSEditor from '../../../draft-js-editor' ;
78import DraftJSMentionSelector from '../DraftJSMentionSelectorCore' ;
89
910const sandbox = sinon . sandbox . create ( ) ;
1011
11- const noMentionEditorState = EditorState . createWithContent ( ContentState . createFromText ( 'No mention here' ) ) ;
12- const oneMentionEditorState = EditorState . createWithContent ( ContentState . createFromText ( 'Hey @foo' ) ) ;
13- const twoMentionEditorState = EditorState . createWithContent ( ContentState . createFromText ( 'Hi @foo, meet @bar' ) ) ;
14-
15- const oneMentionSelectionState = oneMentionEditorState . getSelection ( ) . merge ( {
16- anchorOffset : 8 ,
17- focusOffset : 8 ,
18- } ) ;
19-
20- const twoMentionSelectionState = twoMentionEditorState . getSelection ( ) . merge ( {
21- anchorOffset : 18 ,
22- focusOffset : 18 ,
23- } ) ;
24-
25- const twoMentionSelectionStateCursorInside = twoMentionEditorState . getSelection ( ) . merge ( {
26- anchorOffset : 17 ,
27- focusOffset : 17 ,
28- } ) ;
29-
30- const oneMentionExpectedMention = {
31- mentionString : 'foo' ,
32- mentionTrigger : '@' ,
33- start : 4 ,
34- end : 8 ,
35- } ;
36-
37- const twoMentionExpectedMention = {
38- mentionString : 'bar' ,
39- mentionTrigger : '@' ,
40- start : 14 ,
41- end : 18 ,
42- } ;
43-
44- const twoMentionCursorInsideExpectedMention = {
45- mentionString : 'ba' ,
46- mentionTrigger : '@' ,
47- start : 14 ,
48- end : 17 ,
49- } ;
50-
5112describe ( 'components/form-elements/draft-js-mention-selector/DraftJSMentionSelector' , ( ) => {
5213 afterEach ( ( ) => {
5314 sandbox . verifyAndRestore ( ) ;
@@ -161,71 +122,16 @@ describe('components/form-elements/draft-js-mention-selector/DraftJSMentionSelec
161122 } ) ;
162123
163124 describe ( 'getActiveMentionForEditorState()' , ( ) => {
164- const wrapper = shallow ( < DraftJSMentionSelector { ...requiredProps } /> ) ;
165-
166- const instance = wrapper . instance ( ) ;
167-
168- // TESTS
169- [
170- // empty input
171- {
172- editorState : EditorState . createEmpty ( ) ,
173- } ,
174- // input has ne mention
175- {
176- editorState : noMentionEditorState ,
177- } ,
178- ] . forEach ( ( { editorState } ) => {
179- test ( 'should return null' , ( ) => {
180- expect ( instance . getActiveMentionForEditorState ( editorState ) ) . toBeNull ( ) ;
181- } ) ;
182- } ) ;
183-
184- [
185- // one string beginning with "@"
186- {
187- editorState : oneMentionEditorState ,
188- selectionState : oneMentionSelectionState ,
189- expected : oneMentionExpectedMention ,
190- } ,
191- // two strings beginning with "@"
192- {
193- editorState : twoMentionEditorState ,
194- selectionState : twoMentionSelectionState ,
195- expected : twoMentionExpectedMention ,
196- } ,
197- // two strings beginning "@", cursor inside one
198- {
199- editorState : twoMentionEditorState ,
200- selectionState : twoMentionSelectionStateCursorInside ,
201- expected : twoMentionCursorInsideExpectedMention ,
202- } ,
203- ] . forEach ( ( { editorState, selectionState, expected } ) => {
204- test ( 'should return null when cursor is not over a mention' , ( ) => {
205- const selectionStateAtBeginning = editorState . getSelection ( ) . merge ( {
206- anchorOffset : 0 ,
207- focusOffset : 0 ,
208- } ) ;
209-
210- const editorStateWithForcedSelection = EditorState . acceptSelection (
211- editorState ,
212- selectionStateAtBeginning ,
213- ) ;
214-
215- const result = instance . getActiveMentionForEditorState ( editorStateWithForcedSelection ) ;
125+ test ( 'should call getActiveMentionForEditorState from utils' , ( ) => {
126+ const wrapper = shallow ( < DraftJSMentionSelector { ...requiredProps } /> ) ;
216127
217- expect ( result ) . toBeNull ( ) ;
218- } ) ;
128+ wrapper . setState ( { mentionPattern : 'testPattern' } ) ;
219129
220- test ( 'should return the selected mention when it is selected' , ( ) => {
221- const editorStateWithForcedSelection = EditorState . acceptSelection ( editorState , selectionState ) ;
130+ const getMentionStub = sandbox . stub ( utils , 'getActiveMentionForEditorState' ) ;
222131
223- const result = instance . getActiveMentionForEditorState ( editorStateWithForcedSelection ) ;
132+ wrapper . instance ( ) . getActiveMentionForEditorState ( 'testState' ) ;
224133
225- Object . keys ( expected ) . forEach ( key => {
226- expect ( result [ key ] ) . toEqual ( expected [ key ] ) ;
227- } ) ;
228- } ) ;
134+ expect ( getMentionStub . calledWith ( 'testState' , 'testPattern' ) ) . toBe ( true ) ;
229135 } ) ;
230136 } ) ;
231137
@@ -368,24 +274,22 @@ describe('components/form-elements/draft-js-mention-selector/DraftJSMentionSelec
368274 } ) ;
369275
370276 describe ( 'addMention()' , ( ) => {
371- test ( 'should null out activeMention and call handleChange with updated string (plus space) when called' , ( ) => {
372- const mention = { id : 1 , name : 'Fool Name' } ;
373-
374- const wrapper = shallow ( < DraftJSMentionSelector { ...requiredProps } editorState = { oneMentionEditorState } /> ) ;
277+ test ( 'should call addMention from utils' , ( ) => {
278+ const wrapper = shallow ( < DraftJSMentionSelector { ...requiredProps } editorState = "testState" /> ) ;
375279
376280 wrapper . setState ( {
377- activeMention : oneMentionExpectedMention ,
281+ activeMention : 'testActiveMention' ,
378282 } ) ;
379283
380284 const instance = wrapper . instance ( ) ;
381- const handleChangeMock = sandbox . mock ( instance ) . expects ( 'handleChange ') ;
285+ const addMentionStub = sandbox . stub ( utils , 'addMention' ) . returns ( 'testReturn ') ;
382286 const setStateSpy = sandbox . spy ( instance , 'setState' ) ;
287+ const handleChangeStub = sandbox . stub ( instance , 'handleChange' ) ;
383288
384- instance . addMention ( mention ) ;
289+ instance . addMention ( 'testMention' ) ;
290+ expect ( addMentionStub . calledWith ( 'testState' , 'testActiveMention' , 'testMention' ) ) . toBe ( true ) ;
385291 expect ( setStateSpy . calledWith ( { activeMention : null } ) ) . toBe ( true ) ;
386-
387- const editorStateCall = handleChangeMock . firstCall . args [ 0 ] ;
388- expect ( editorStateCall . getCurrentContent ( ) . getPlainText ( ) ) . toEqual ( 'Hey @Fool Name ' ) ;
292+ expect ( handleChangeStub . calledWith ( 'testReturn' ) ) . toBe ( true ) ;
389293 } ) ;
390294 } ) ;
391295
@@ -400,7 +304,7 @@ describe('components/form-elements/draft-js-mention-selector/DraftJSMentionSelec
400304 const wrapper = shallow ( < DraftJSMentionSelector { ...requiredProps } contacts = { [ contact ] } /> ) ;
401305
402306 wrapper . setState ( {
403- activeMention : oneMentionExpectedMention ,
307+ activeMention : { } ,
404308 } ) ;
405309
406310 wrapper . setProps ( { contacts : [ ] } ) ;
0 commit comments