|
1 | 1 | import { expect } from 'chai'; |
2 | | -import Draft, { EditorState } from 'draft-js'; |
3 | | -import { addText, addEmptyBlock } from '../utils'; |
| 2 | +import Draft, { EditorState, SelectionState } from 'draft-js'; |
| 3 | +import { addText, replaceText, addEmptyBlock } from '../utils'; |
4 | 4 |
|
5 | 5 | describe('utils test', () => { |
6 | 6 | it('is loaded', () => { |
7 | 7 | expect(addText).to.be.a('function'); |
| 8 | + expect(replaceText).to.be.a('function'); |
8 | 9 | expect(addEmptyBlock).to.be.a('function'); |
9 | 10 | }); |
10 | 11 |
|
@@ -45,4 +46,26 @@ describe('utils test', () => { |
45 | 46 | const lastBlock = currentContent.getLastBlock(); |
46 | 47 | expect(lastBlock.getText()).to.equal(randomText); |
47 | 48 | }); |
| 49 | + |
| 50 | + it('should replaceText', () => { |
| 51 | + let newEditorState = EditorState.createWithContent(Draft.convertFromRaw(newRawContentState)); |
| 52 | + const randomText = Date.now().toString(32); |
| 53 | + let currentContent = newEditorState.getCurrentContent(); |
| 54 | + let lastBlock = currentContent.getLastBlock(); |
| 55 | + const newSelection = new SelectionState({ |
| 56 | + anchorKey: lastBlock.getKey(), |
| 57 | + anchorOffset: 0, |
| 58 | + focusKey: lastBlock.getKey(), |
| 59 | + focusOffset: lastBlock.getText().length |
| 60 | + }); |
| 61 | + newEditorState = EditorState.forceSelection(newEditorState, newSelection); |
| 62 | + |
| 63 | + newEditorState = replaceText(newEditorState, randomText); |
| 64 | + currentContent = newEditorState.getCurrentContent(); |
| 65 | + expect(currentContent.hasText()).to.equal(true); |
| 66 | + lastBlock = currentContent.getLastBlock(); |
| 67 | + expect(lastBlock.getText()).to.equal(randomText); |
| 68 | + const firstBlock = currentContent.getFirstBlock(); |
| 69 | + expect(firstBlock.getText()).to.equal(randomText); |
| 70 | + }); |
48 | 71 | }); |
0 commit comments