Skip to content

Commit bf420f3

Browse files
authored
Merge pull request #13 from SamyPesse/tests
First tests
2 parents dcb9f93 + 211c1c5 commit bf420f3

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

lib/__tests__/getLineAnchorForOffset.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var expect = require('expect');
21
var getLineAnchorForOffset = require('../getLineAnchorForOffset');
32

43
describe('getLineAnchorForOffset', function() {
@@ -28,4 +27,3 @@ describe('getLineAnchorForOffset', function() {
2827
});
2928

3029
});
31-

lib/__tests__/handle-tab.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { EditorState, ContentState, SelectionState } = require('draft-js');
2+
const handleTab = require('../handleTab');
3+
4+
const toPlainText = (editorState) => editorState.getCurrentContent().getPlainText();
5+
const createWithText = (text) => {
6+
const contentState = ContentState.createFromText(text);
7+
return EditorState.createWithContent(contentState);
8+
}
9+
10+
const tabs = (times) => " ".repeat(times || 1);
11+
12+
it('should insert a tab', () => {
13+
const evt = { preventDefault: jest.fn() };
14+
const initialText = "";
15+
const before = createWithText(initialText);
16+
const after = handleTab(evt, before);
17+
18+
expect(toPlainText(before)).toEqual(initialText);
19+
expect(toPlainText(after)).toEqual(tabs(1));
20+
});
21+
22+
it('should prevent the default event behavior', () => {
23+
const preventDefault = jest.fn();
24+
const evt = { preventDefault };
25+
const before = EditorState.createEmpty();
26+
27+
handleTab(evt, before);
28+
expect(preventDefault).toHaveBeenCalled();
29+
})
30+
31+
it('should add a tab to an existing tab', () => {
32+
const evt = { preventDefault: jest.fn() };
33+
const initialText = tabs(1);
34+
const before = createWithText(initialText);
35+
const after = handleTab(evt, before);
36+
37+
expect(toPlainText(before)).toEqual(initialText);
38+
expect(toPlainText(after)).toEqual(initialText + tabs(1));
39+
})
40+
41+
it('should replace existing content with the tab', () => {
42+
const evt = { preventDefault: jest.fn() };
43+
const initialText = "hello";
44+
45+
const currentContent = ContentState.createFromText(initialText);
46+
const selectInitialtext = SelectionState.createEmpty(
47+
currentContent.getBlockMap().first().getKey()
48+
);
49+
const before = EditorState.create({
50+
allowUndo: true,
51+
currentContent,
52+
// Focus the entire initial word
53+
selection: selectInitialtext.set('focusOffset', initialText.length),
54+
});
55+
56+
const after = handleTab(evt, before);
57+
expect(toPlainText(before)).toEqual(initialText);
58+
expect(toPlainText(after)).toEqual(tabs(1));
59+
})

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Collection of utilities to make code blocks edition easy in DraftJS",
55
"main": "./lib/index.js",
66
"scripts": {
7-
"test": "./node_modules/.bin/mocha \"./lib/__tests__/*.js\" --bail --reporter=list",
7+
"test": "jest",
88
"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",
99
"deploy": "npm run build; gh-pages -d ./demo",
1010
"watch": "watch 'npm run build' ./lib",
@@ -39,13 +39,13 @@
3939
"draft-js": "0.x",
4040
"draft-js-prism": "^1.0.2",
4141
"eslint": "^2.11.1",
42-
"expect": "^1.20.1",
4342
"gh-pages": "^0.11.0",
4443
"http-server": "^0.9.0",
45-
"mocha": "^2.5.3",
44+
"jest": "^21.1.0",
4645
"parallelshell": "2.0.0",
4746
"react": "^15.1.0",
4847
"react-dom": "^15.1.0",
48+
"prismjs": "^1.8.1",
4949
"watch": "^0.18.0"
5050
},
5151
"peerDependencies": {

0 commit comments

Comments
 (0)