This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding 'preserveSelectionOnBlur' prop (#2128)
Summary: **Summary** This adds a `preserveSelectionOnBlur` prop as per #2123 **Test Plan** Tests were added for the two possible scenarios. I'm not sure how much to mock in this test because it is just testing a specific branch of code, so I've faked certain details like the rangeCount. What do you think mrkev claudiopro ? Pull Request resolved: #2128 Reviewed By: claudiopro Differential Revision: D16270879 Pulled By: mrkev fbshipit-source-id: 304af92c1211b8ff95741bff434b4fe3c4b6dd7d
- Loading branch information
1 parent
4064cae
commit db792ef
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/component/handlers/edit/__tests__/editOnBlur-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails oncall+draft_js | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
jest.disableAutomock(); | ||
|
||
const ContentBlock = require('ContentBlock'); | ||
const ContentState = require('ContentState'); | ||
const EditorState = require('EditorState'); | ||
|
||
const onBlur = require('editOnBlur'); | ||
|
||
const getEditorState = (text: string = 'Arsenal') => { | ||
return EditorState.createWithContent( | ||
ContentState.createFromBlockArray([ | ||
new ContentBlock({ | ||
key: 'a', | ||
text, | ||
}), | ||
]), | ||
); | ||
}; | ||
|
||
const getBlurEvent = data => ({ | ||
data, | ||
}); | ||
|
||
function withGlobalGetSelectionAs(getSelectionValue = {}, callback) { | ||
const oldGetSelection = global.getSelection; | ||
try { | ||
global.getSelection = () => { | ||
return getSelectionValue; | ||
}; | ||
callback(); | ||
} finally { | ||
global.getSelection = oldGetSelection; | ||
} | ||
} | ||
|
||
test('editor removes selection on blur (default behaviour)', () => { | ||
const anchorNodeText = 'react draftjs'; | ||
const anchorNode = document.createTextNode(anchorNodeText); | ||
const globalSelection = { | ||
anchorNode, | ||
focusNode: anchorNode, | ||
removeAllRanges: jest.fn(), | ||
rangeCount: 1, | ||
}; | ||
|
||
const editorNode = document.createElement('div'); | ||
editorNode.appendChild(anchorNode); | ||
|
||
withGlobalGetSelectionAs(globalSelection, () => { | ||
const editorState = getEditorState(anchorNodeText); | ||
const editor = { | ||
_latestEditorState: editorState, | ||
props: { | ||
preserveSelectionOnBlur: false, | ||
}, | ||
editor: editorNode, | ||
}; | ||
|
||
onBlur(editor, getBlurEvent()); | ||
|
||
expect(globalSelection.removeAllRanges).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
test('editor preserves selection on blur', () => { | ||
const anchorNodeText = 'react draftjs'; | ||
const anchorNode = document.createTextNode(anchorNodeText); | ||
const globalSelection = { | ||
anchorNode, | ||
focusNode: anchorNode, | ||
removeAllRanges: jest.fn(), | ||
rangeCount: 1, | ||
}; | ||
|
||
const editorNode = document.createElement('div'); | ||
editorNode.appendChild(anchorNode); | ||
|
||
withGlobalGetSelectionAs(globalSelection, () => { | ||
const editorState = getEditorState(anchorNodeText); | ||
const editor = { | ||
_latestEditorState: editorState, | ||
props: { | ||
preserveSelectionOnBlur: true, | ||
}, | ||
editor: editorNode, | ||
}; | ||
|
||
onBlur(editor, getBlurEvent()); | ||
|
||
expect(globalSelection.removeAllRanges).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters