Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test handleTab
  • Loading branch information
mxstbr committed Sep 23, 2017
commit fea57a843320e170ae1104c41196b6672b8cc76e
59 changes: 59 additions & 0 deletions lib/__tests__/handle-tab.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { EditorState, ContentState, SelectionState } = require('draft-js');
const handleTab = require('../handleTab');

const toPlainText = (editorState) => editorState.getCurrentContent().getPlainText();
const createWithText = (text) => {
const contentState = ContentState.createFromText(text);
return EditorState.createWithContent(contentState);
}

const tabs = (times) => " ".repeat(times || 1);

it('should insert a tab', () => {
const evt = { preventDefault: jest.fn() };
const initialText = "";
const before = createWithText(initialText);
const after = handleTab(evt, before);

expect(toPlainText(before)).toEqual(initialText);
expect(toPlainText(after)).toEqual(tabs(1));
});

it('should prevent the default event behavior', () => {
const preventDefault = jest.fn();
const evt = { preventDefault };
const before = EditorState.createEmpty();

handleTab(evt, before);
expect(preventDefault).toHaveBeenCalled();
})

it('should add a tab to an existing tab', () => {
const evt = { preventDefault: jest.fn() };
const initialText = tabs(1);
const before = createWithText(initialText);
const after = handleTab(evt, before);

expect(toPlainText(before)).toEqual(initialText);
expect(toPlainText(after)).toEqual(initialText + tabs(1));
})

it('should replace existing content with the tab', () => {
const evt = { preventDefault: jest.fn() };
const initialText = "hello";

const currentContent = ContentState.createFromText(initialText);
const selectInitialtext = SelectionState.createEmpty(
currentContent.getBlockMap().first().getKey()
);
const before = EditorState.create({
allowUndo: true,
currentContent,
// Focus the entire initial word
selection: selectInitialtext.set('focusOffset', initialText.length),
});

const after = handleTab(evt, before);
expect(toPlainText(before)).toEqual(initialText);
expect(toPlainText(after)).toEqual(tabs(1));
})