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

Commit

Permalink
add flow declaration in editOnBeforeInput-test.js
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Frank Thompson authored and facebook-github-bot committed Nov 11, 2019
1 parent 177db5e commit 0601090
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions src/component/handlers/edit/__tests__/editOnBeforeInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+draft_js
* @flow
* @format
*/

'use strict';

jest.disableAutomock();

import type DraftEditor from 'DraftEditor.react';

const CompositeDraftDecorator = require('CompositeDraftDecorator');
const ContentBlock = require('ContentBlock');
const ContentState = require('ContentState');
Expand Down Expand Up @@ -50,22 +53,25 @@ const getEditorState = (text: string = 'Arsenal') => {
);
};

const getInputEvent = data => ({
data,
preventDefault: jest.fn(),
});
const getDraftEditor = (obj): DraftEditor => (obj: any);

const getInputEvent = (data): SyntheticInputEvent<> =>
({
data,
preventDefault: jest.fn(),
}: any);

test('editor is not updated if no character data is provided', () => {
const editorState = EditorState.acceptSelection(
getEditorState(),
rangedSelection,
);

const editor = {
const editor = getDraftEditor({
_latestEditorState: editorState,
props: {},
update: jest.fn(),
};
});

onBeforeInput(editor, getInputEvent());

Expand All @@ -78,13 +84,13 @@ test('editor is not updated if handled by handleBeforeInput', () => {
rangedSelection,
);

const editor = {
const editor = getDraftEditor({
_latestEditorState: editorState,
props: {
handleBeforeInput: () => true,
},
update: jest.fn(),
};
});

onBeforeInput(editor, getInputEvent('O'));

Expand All @@ -97,17 +103,18 @@ test('editor is updated with new text if it does not match current selection', (
rangedSelection,
);

const editor = {
const update = jest.fn();
const editor = getDraftEditor({
_latestEditorState: editorState,
props: {},
update: jest.fn(),
};
update,
});

onBeforeInput(editor, getInputEvent('O'));

expect(editor.update).toHaveBeenCalledTimes(1);
expect(update).toHaveBeenCalledTimes(1);

const newEditorState = editor.update.mock.calls[0][0];
const newEditorState = update.mock.calls[0][0];
expect(newEditorState.getCurrentContent()).toMatchSnapshot();
});

Expand All @@ -117,17 +124,18 @@ test('editor selectionstate is updated if new text matches current selection', (
rangedSelection,
);

const editor = {
const update = jest.fn();
const editor = getDraftEditor({
_latestEditorState: editorState,
props: {},
update: jest.fn(),
};
update,
});

onBeforeInput(editor, getInputEvent('A'));

expect(editor.update).toHaveBeenCalledTimes(1);
expect(update).toHaveBeenCalledTimes(1);

const newEditorState = editor.update.mock.calls[0][0];
const newEditorState = update.mock.calls[0][0];
expect(newEditorState.getSelection()).toMatchSnapshot();
});

Expand All @@ -137,17 +145,18 @@ test('editor selectionstate is updated if new text matches current selection and
rangedSelectionBackwards,
);

const editor = {
const update = jest.fn();
const editor = getDraftEditor({
_latestEditorState: editorState,
props: {},
update: jest.fn(),
};
update,
});

onBeforeInput(editor, getInputEvent('A'));

expect(editor.update).toHaveBeenCalledTimes(1);
expect(update).toHaveBeenCalledTimes(1);

const newEditorState = editor.update.mock.calls[0][0];
const newEditorState = update.mock.calls[0][0];
expect(newEditorState.getSelection()).toMatchSnapshot();
});

Expand All @@ -158,10 +167,10 @@ function hashtagStrategy(contentBlock, callback, contentState) {

function findWithRegex(regex, contentBlock, callback) {
const text = contentBlock.getText();
let matchArr, start;
while ((matchArr = regex.exec(text)) !== null) {
start = matchArr.index;
callback(start, start + matchArr[0].length);
let matchArr = regex.exec(text);
while (matchArr !== null) {
callback(matchArr.index, matchArr.index + matchArr[0].length);
matchArr = regex.exec(text);
}
}

Expand All @@ -187,12 +196,12 @@ function testDecoratorFingerprint(
}),
);

const editor = {
const editor = getDraftEditor({
_latestEditorState: editorState,
_latestCommittedEditorState: editorState,
props: {},
update: jest.fn(),
};
});

const ev = getInputEvent(charToInsert);
onBeforeInput(editor, ev);
Expand Down

0 comments on commit 0601090

Please sign in to comment.