Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions lib/__tests__/getLineAnchorForOffset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var expect = require('expect');
var getLineAnchorForOffset = require('../getLineAnchorForOffset');

describe('getLineAnchorForOffset', function() {
Expand Down Expand Up @@ -28,4 +27,3 @@ describe('getLineAnchorForOffset', function() {
});

});

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));
})
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Collection of utilities to make code blocks edition easy in DraftJS",
"main": "./lib/index.js",
"scripts": {
"test": "./node_modules/.bin/mocha \"./lib/__tests__/*.js\" --bail --reporter=list",
"test": "jest",
"build": "browserify -t [ babelify --presets [ es2015 react ] ] ./demo/main.js > ./demo/dist.js; cp ./node_modules/prismjs/themes/prism.css ./demo/prism.css; cp ./node_modules/draft-js/dist/Draft.css ./demo/draft.css",
"deploy": "npm run build; gh-pages -d ./demo",
"watch": "watch 'npm run build' ./lib",
Expand Down Expand Up @@ -39,13 +39,13 @@
"draft-js": "0.x",
"draft-js-prism": "^1.0.2",
"eslint": "^2.11.1",
"expect": "^1.20.1",
"gh-pages": "^0.11.0",
"http-server": "^0.9.0",
"mocha": "^2.5.3",
"jest": "^21.1.0",
"parallelshell": "2.0.0",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"prismjs": "^1.8.1",
"watch": "^0.18.0"
},
"peerDependencies": {
Expand Down