Skip to content

Commit 6cf681f

Browse files
committed
Improve branch coverage
1 parent bbe4489 commit 6cf681f

File tree

7 files changed

+73
-55
lines changed

7 files changed

+73
-55
lines changed

.nycrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"exclude": [
3+
"src/**/__test__/*.js"
4+
],
25
"reporter": [
36
"lcov",
47
"text-summary",

src/modifiers/__test__/changeCurrentBlockType-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@ describe('changeCurrentBlockType', () => {
4040
rawContentState('Hello world', 'header-one', { foo: 'bar' })
4141
);
4242
});
43+
it('changes block type even if data is null', () => {
44+
const editorState = createEditorState('Yo', 'unstyled');
45+
const newEditorState = changeCurrentBlockType(
46+
editorState, 'header-one', 'Hello world', null);
47+
expect(newEditorState).not.to.equal(editorState);
48+
expect(
49+
Draft.convertToRaw(newEditorState.getCurrentContent())
50+
).to.deep.equal(
51+
rawContentState('Hello world', 'header-one', {})
52+
);
53+
});
4354
});

src/modifiers/__test__/handleBlockType-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('handleBlockType', () => {
88
entityMap: {},
99
blocks: [{
1010
key: 'item1',
11-
text: 'Test',
11+
text: '[ ]',
1212
type: 'unstyled',
1313
depth: 0,
1414
inlineStyleRanges: [],
@@ -19,9 +19,9 @@ describe('handleBlockType', () => {
1919
const contentState = Draft.convertFromRaw(rawContentState);
2020
const selection = new SelectionState({
2121
anchorKey: 'item1',
22-
anchorOffset: 6,
22+
anchorOffset: 3,
2323
focusKey: 'item1',
24-
focusOffset: 6,
24+
focusOffset: 3,
2525
isBackward: false,
2626
hasFocus: true
2727
});

src/modifiers/__test__/insertEmptyBlock-test.js

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,60 @@ describe('insertEmptyBlock', () => {
1010
after(() => {
1111
Draft.genKey.restore();
1212
});
13-
const firstBlock = {
14-
key: 'item1',
15-
text: 'asdf',
16-
type: 'unstyled',
17-
depth: 0,
18-
inlineStyleRanges: [{
19-
length: 2,
20-
offset: 1,
21-
style: 'ITALIC'
22-
}],
23-
entityRanges: [],
24-
data: { foo: 'bar' }
25-
};
26-
const beforeRawContentState = {
27-
entityMap: {},
28-
blocks: [firstBlock]
29-
};
30-
const afterRawContentState = {
31-
entityMap: {},
32-
blocks: [firstBlock, {
33-
key: 'item2',
34-
text: '',
35-
type: 'header-one',
13+
const testInsertEmptyBlock = (...args) => () => {
14+
const [type = 'unstyled', data = {}] = args;
15+
const firstBlock = {
16+
key: 'item1',
17+
text: 'asdf',
18+
type: 'unstyled',
3619
depth: 0,
37-
inlineStyleRanges: [],
20+
inlineStyleRanges: [{
21+
length: 2,
22+
offset: 1,
23+
style: 'ITALIC'
24+
}],
3825
entityRanges: [],
39-
data: { bar: 'baz' }
40-
}]
26+
data: { foo: 'bar' }
27+
};
28+
const beforeRawContentState = {
29+
entityMap: {},
30+
blocks: [firstBlock]
31+
};
32+
const afterRawContentState = {
33+
entityMap: {},
34+
blocks: [firstBlock, {
35+
key: 'item2',
36+
text: '',
37+
type,
38+
depth: 0,
39+
inlineStyleRanges: [],
40+
entityRanges: [],
41+
data
42+
}]
43+
};
44+
const contentState = Draft.convertFromRaw(beforeRawContentState);
45+
const selection = new SelectionState({
46+
anchorKey: 'item1',
47+
anchorOffset: 4,
48+
focusKey: 'item1',
49+
focusOffset: 4,
50+
isBackward: false,
51+
hasFocus: true
52+
});
53+
const editorState = EditorState.forceSelection(
54+
EditorState.createWithContent(contentState), selection);
55+
56+
it('creates new code block', () => {
57+
const newEditorState = insertEmptyBlock(editorState, ...args);
58+
expect(newEditorState).not.to.equal(editorState);
59+
expect(
60+
Draft.convertToRaw(newEditorState.getCurrentContent())
61+
).to.deep.equal(
62+
afterRawContentState
63+
);
64+
});
4165
};
42-
const contentState = Draft.convertFromRaw(beforeRawContentState);
43-
const selection = new SelectionState({
44-
anchorKey: 'item1',
45-
anchorOffset: 4,
46-
focusKey: 'item1',
47-
focusOffset: 4,
48-
isBackward: false,
49-
hasFocus: true
50-
});
51-
const editorState = EditorState.forceSelection(
52-
EditorState.createWithContent(contentState), selection);
53-
it('creates new code block', () => {
54-
const newEditorState = insertEmptyBlock(editorState, 'header-one', { bar: 'baz' });
55-
expect(newEditorState).not.to.equal(editorState);
56-
expect(
57-
Draft.convertToRaw(newEditorState.getCurrentContent())
58-
).to.deep.equal(
59-
afterRawContentState
60-
);
61-
});
66+
67+
describe('with arguments', testInsertEmptyBlock('header-one', { bar: 'baz' }));
68+
describe('without arguments', testInsertEmptyBlock());
6269
});

src/modifiers/changeCurrentBlockType.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ const changeCurrentBlockType = (editorState, type, text, blockMetadata = {}) =>
66
const key = selection.getStartKey();
77
const blockMap = currentContent.getBlockMap();
88
const block = blockMap.get(key);
9-
let data = block.getData();
10-
if (data && data.merge) {
11-
data = data.merge(blockMetadata);
12-
}
9+
const data = block.getData().merge(blockMetadata);
1310
const newBlock = block.merge({ type, data, text: text || '' });
1411
const newSelection = selection.merge({
1512
anchorOffset: 0,

src/modifiers/handleBlockType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const handleBlockType = (editorState, character) => {
4545
return changeCurrentBlockType(editorState, 'blockquote', matchArr[1]);
4646
}
4747
matchArr = line.match(/^\[([x ])] (.*)$/i);
48-
if (blockType === 'unordered-list-item' && matchArr) {
48+
if (matchArr && blockType === 'unordered-list-item') {
4949
return changeCurrentBlockType(editorState, CHECKABLE_LIST_ITEM, matchArr[2], { checked: matchArr[1] !== ' ' });
5050
}
5151
return editorState;

src/modifiers/handleInlineStyle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const handleInlineStyle = (editorState, character) => {
3535
});
3636
return newEditorState !== editorState;
3737
});
38-
return newEditorState || editorState;
38+
return newEditorState;
3939
};
4040

4141
export default handleInlineStyle;

0 commit comments

Comments
 (0)