|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails oncall+draft_js |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +jest.disableAutomock(); |
| 14 | + |
| 15 | +const ContentBlock = require('ContentBlock'); |
| 16 | +const ContentState = require('ContentState'); |
| 17 | +const EditorState = require('EditorState'); |
| 18 | + |
| 19 | +const onBlur = require('editOnBlur'); |
| 20 | + |
| 21 | +const getEditorState = (text: string = 'Arsenal') => { |
| 22 | + return EditorState.createWithContent( |
| 23 | + ContentState.createFromBlockArray([ |
| 24 | + new ContentBlock({ |
| 25 | + key: 'a', |
| 26 | + text, |
| 27 | + }), |
| 28 | + ]), |
| 29 | + ); |
| 30 | +}; |
| 31 | + |
| 32 | +const getBlurEvent = data => ({ |
| 33 | + data, |
| 34 | +}); |
| 35 | + |
| 36 | +function withGlobalGetSelectionAs(getSelectionValue = {}, callback) { |
| 37 | + const oldGetSelection = global.getSelection; |
| 38 | + try { |
| 39 | + global.getSelection = () => { |
| 40 | + return getSelectionValue; |
| 41 | + }; |
| 42 | + callback(); |
| 43 | + } finally { |
| 44 | + global.getSelection = oldGetSelection; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +test('editor removes selection on blur (default behaviour)', () => { |
| 49 | + const anchorNodeText = 'react draftjs'; |
| 50 | + const anchorNode = document.createTextNode(anchorNodeText); |
| 51 | + const globalSelection = { |
| 52 | + anchorNode, |
| 53 | + focusNode: anchorNode, |
| 54 | + removeAllRanges: jest.fn(), |
| 55 | + rangeCount: 1, |
| 56 | + }; |
| 57 | + |
| 58 | + const editorNode = document.createElement('div'); |
| 59 | + editorNode.appendChild(anchorNode); |
| 60 | + |
| 61 | + withGlobalGetSelectionAs(globalSelection, () => { |
| 62 | + const editorState = getEditorState(anchorNodeText); |
| 63 | + const editor = { |
| 64 | + _latestEditorState: editorState, |
| 65 | + props: { |
| 66 | + preserveSelectionOnBlur: false, |
| 67 | + }, |
| 68 | + editor: editorNode, |
| 69 | + }; |
| 70 | + |
| 71 | + onBlur(editor, getBlurEvent()); |
| 72 | + |
| 73 | + expect(globalSelection.removeAllRanges).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +test('editor preserves selection on blur', () => { |
| 78 | + const anchorNodeText = 'react draftjs'; |
| 79 | + const anchorNode = document.createTextNode(anchorNodeText); |
| 80 | + const globalSelection = { |
| 81 | + anchorNode, |
| 82 | + focusNode: anchorNode, |
| 83 | + removeAllRanges: jest.fn(), |
| 84 | + rangeCount: 1, |
| 85 | + }; |
| 86 | + |
| 87 | + const editorNode = document.createElement('div'); |
| 88 | + editorNode.appendChild(anchorNode); |
| 89 | + |
| 90 | + withGlobalGetSelectionAs(globalSelection, () => { |
| 91 | + const editorState = getEditorState(anchorNodeText); |
| 92 | + const editor = { |
| 93 | + _latestEditorState: editorState, |
| 94 | + props: { |
| 95 | + preserveSelectionOnBlur: true, |
| 96 | + }, |
| 97 | + editor: editorNode, |
| 98 | + }; |
| 99 | + |
| 100 | + onBlur(editor, getBlurEvent()); |
| 101 | + |
| 102 | + expect(globalSelection.removeAllRanges).toHaveBeenCalledTimes(0); |
| 103 | + }); |
| 104 | +}); |
0 commit comments