Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 0601090

Browse files
Frank Thompsonfacebook-github-bot
authored andcommitted
add flow declaration in editOnBeforeInput-test.js
Summary: This adds an `flow` declaration to `editOnBeforeInput-test.js`. I took the easy way out and used `any` to cast plain objects to `DraftEditor` and `SyntheticInputEvent<>`, which is why I did not use `flow strict-local`. Reviewed By: claudiopro Differential Revision: D18433927 fbshipit-source-id: 5cfa39482be75ec526fd47c32496c29f55fe2cdb
1 parent 177db5e commit 0601090

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

src/component/handlers/edit/__tests__/editOnBeforeInput-test.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
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

1314
jest.disableAutomock();
1415

16+
import type DraftEditor from 'DraftEditor.react';
17+
1518
const CompositeDraftDecorator = require('CompositeDraftDecorator');
1619
const ContentBlock = require('ContentBlock');
1720
const 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

5864
test('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

159168
function 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

Comments
 (0)