55 * LICENSE file in the root directory of this source tree.
66 *
77 * @emails oncall+draft_js
8+ * @flow
89 * @format
910 */
1011
1112'use strict' ;
1213
1314jest . disableAutomock ( ) ;
1415
16+ import type DraftEditor from 'DraftEditor.react' ;
17+
1518const CompositeDraftDecorator = require ( 'CompositeDraftDecorator' ) ;
1619const ContentBlock = require ( 'ContentBlock' ) ;
1720const ContentState = require ( 'ContentState' ) ;
@@ -50,22 +53,25 @@ const getEditorState = (text: string = 'Arsenal') => {
5053 ) ;
5154} ;
5255
53- const getInputEvent = data => ( {
54- data,
55- preventDefault : jest . fn ( ) ,
56- } ) ;
56+ const getDraftEditor = ( obj ) : DraftEditor => ( obj : any ) ;
57+
58+ const getInputEvent = ( data ) : SyntheticInputEvent < > =>
59+ ( {
60+ data,
61+ preventDefault : jest . fn ( ) ,
62+ } : any ) ;
5763
5864test ( 'editor is not updated if no character data is provided' , ( ) => {
5965 const editorState = EditorState . acceptSelection (
6066 getEditorState ( ) ,
6167 rangedSelection ,
6268 ) ;
6369
64- const editor = {
70+ const editor = getDraftEditor ( {
6571 _latestEditorState : editorState ,
6672 props : { } ,
6773 update : jest . fn ( ) ,
68- } ;
74+ } ) ;
6975
7076 onBeforeInput ( editor , getInputEvent ( ) ) ;
7177
@@ -78,13 +84,13 @@ test('editor is not updated if handled by handleBeforeInput', () => {
7884 rangedSelection ,
7985 ) ;
8086
81- const editor = {
87+ const editor = getDraftEditor ( {
8288 _latestEditorState : editorState ,
8389 props : {
8490 handleBeforeInput : ( ) => true ,
8591 } ,
8692 update : jest . fn ( ) ,
87- } ;
93+ } ) ;
8894
8995 onBeforeInput ( editor , getInputEvent ( 'O' ) ) ;
9096
@@ -97,17 +103,18 @@ test('editor is updated with new text if it does not match current selection', (
97103 rangedSelection ,
98104 ) ;
99105
100- const editor = {
106+ const update = jest . fn ( ) ;
107+ const editor = getDraftEditor ( {
101108 _latestEditorState : editorState ,
102109 props : { } ,
103- update : jest . fn ( ) ,
104- } ;
110+ update,
111+ } ) ;
105112
106113 onBeforeInput ( editor , getInputEvent ( 'O' ) ) ;
107114
108- expect ( editor . update ) . toHaveBeenCalledTimes ( 1 ) ;
115+ expect ( update ) . toHaveBeenCalledTimes ( 1 ) ;
109116
110- const newEditorState = editor . update . mock . calls [ 0 ] [ 0 ] ;
117+ const newEditorState = update . mock . calls [ 0 ] [ 0 ] ;
111118 expect ( newEditorState . getCurrentContent ( ) ) . toMatchSnapshot ( ) ;
112119} ) ;
113120
@@ -117,17 +124,18 @@ test('editor selectionstate is updated if new text matches current selection', (
117124 rangedSelection ,
118125 ) ;
119126
120- const editor = {
127+ const update = jest . fn ( ) ;
128+ const editor = getDraftEditor ( {
121129 _latestEditorState : editorState ,
122130 props : { } ,
123- update : jest . fn ( ) ,
124- } ;
131+ update,
132+ } ) ;
125133
126134 onBeforeInput ( editor , getInputEvent ( 'A' ) ) ;
127135
128- expect ( editor . update ) . toHaveBeenCalledTimes ( 1 ) ;
136+ expect ( update ) . toHaveBeenCalledTimes ( 1 ) ;
129137
130- const newEditorState = editor . update . mock . calls [ 0 ] [ 0 ] ;
138+ const newEditorState = update . mock . calls [ 0 ] [ 0 ] ;
131139 expect ( newEditorState . getSelection ( ) ) . toMatchSnapshot ( ) ;
132140} ) ;
133141
@@ -137,17 +145,18 @@ test('editor selectionstate is updated if new text matches current selection and
137145 rangedSelectionBackwards ,
138146 ) ;
139147
140- const editor = {
148+ const update = jest . fn ( ) ;
149+ const editor = getDraftEditor ( {
141150 _latestEditorState : editorState ,
142151 props : { } ,
143- update : jest . fn ( ) ,
144- } ;
152+ update,
153+ } ) ;
145154
146155 onBeforeInput ( editor , getInputEvent ( 'A' ) ) ;
147156
148- expect ( editor . update ) . toHaveBeenCalledTimes ( 1 ) ;
157+ expect ( update ) . toHaveBeenCalledTimes ( 1 ) ;
149158
150- const newEditorState = editor . update . mock . calls [ 0 ] [ 0 ] ;
159+ const newEditorState = update . mock . calls [ 0 ] [ 0 ] ;
151160 expect ( newEditorState . getSelection ( ) ) . toMatchSnapshot ( ) ;
152161} ) ;
153162
@@ -158,10 +167,10 @@ function hashtagStrategy(contentBlock, callback, contentState) {
158167
159168function findWithRegex ( regex , contentBlock , callback ) {
160169 const text = contentBlock . getText ( ) ;
161- let matchArr , start ;
162- while ( ( matchArr = regex . exec ( text ) ) !== null ) {
163- start = matchArr . index ;
164- callback ( start , start + matchArr [ 0 ] . length ) ;
170+ let matchArr = regex . exec ( text ) ;
171+ while ( matchArr !== null ) {
172+ callback ( matchArr . index , matchArr . index + matchArr [ 0 ] . length ) ;
173+ matchArr = regex . exec ( text ) ;
165174 }
166175}
167176
@@ -187,12 +196,12 @@ function testDecoratorFingerprint(
187196 } ) ,
188197 ) ;
189198
190- const editor = {
199+ const editor = getDraftEditor ( {
191200 _latestEditorState : editorState ,
192201 _latestCommittedEditorState : editorState ,
193202 props : { } ,
194203 update : jest . fn ( ) ,
195- } ;
204+ } ) ;
196205
197206 const ev = getInputEvent ( charToInsert ) ;
198207 onBeforeInput ( editor , ev ) ;
0 commit comments