| author | Alexandre Segura <mex.zktk@gmail.com> |
| Thu, 29 Jun 2017 12:06:48 +0200 | |
| changeset 106 | fffefefed507 |
| parent 14 | df6780e48eb5 |
| child 168 | ea92f4fe783d |
| permissions | -rw-r--r-- |
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
import React from 'react'; |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
import { shallow, mount } from 'enzyme'; |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
import NoteInput from '../NoteInput'; |
| 14 | 4 |
import { Plain } from 'slate' |
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
|
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
const setup = (propOverrides, doMount=false) => { |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
const props = Object.assign({ |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
addNote: jest.fn() |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
}, propOverrides); |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
|
| 14 | 11 |
const renderFn = doMount?mount:shallow; |
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
const wrapper = renderFn(<NoteInput {...props} />); |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
|
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
return { |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
props, |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
wrapper, |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
} |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
}; |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
|
| 14 | 20 |
// Element.focus() doesn't work |
21 |
// @see https://stackoverflow.com/questions/42213522/mocking-document-createrange-for-jest |
|
| 8 | 22 |
beforeAll(() => { |
23 |
window.getSelection = () => { |
|
24 |
return { |
|
| 14 | 25 |
removeAllRanges: () => {}, |
26 |
addRange: () => {} |
|
27 |
}; |
|
28 |
}; |
|
29 |
||
30 |
window.Range = function Range() {}; |
|
31 |
||
32 |
const createContextualFragment = (html) => { |
|
33 |
const div = document.createElement('div'); |
|
34 |
div.innerHTML = html; |
|
35 |
return div.children[0]; // so hokey it's not even funny |
|
36 |
}; |
|
37 |
||
38 |
Range.prototype.createContextualFragment = (html) => createContextualFragment(html); |
|
39 |
||
40 |
// HACK: Polyfil that allows codemirror to render in a JSDOM env. |
|
41 |
window.document.createRange = function createRange() { |
|
42 |
return { |
|
43 |
setEnd: () => {}, |
|
44 |
setStart: () => {}, |
|
45 |
getBoundingClientRect: () => { |
|
46 |
return { right: 0 }; |
|
47 |
}, |
|
48 |
compareBoundaryPoints: () => { |
|
49 |
return 0; |
|
50 |
}, |
|
51 |
getClientRects: () => [], |
|
52 |
createContextualFragment, |
|
| 8 | 53 |
}; |
54 |
}; |
|
55 |
}); |
|
56 |
||
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
describe('Notes container Component', () => { |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
test('render', () => { |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
const { wrapper } = setup(); |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
expect(wrapper.exists()).toBe(true) |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
}); |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
|
| 14 | 63 |
test('button is disabled when there is no text', () => { |
64 |
const { props, wrapper } = setup({}, true); |
|
65 |
const button = wrapper.find('button'); |
|
66 |
||
67 |
expect(button.prop('disabled')).toBe(true); |
|
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
}); |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
|
| 14 | 70 |
test('button is not disabled when there is text', () => { |
71 |
const { props, wrapper } = setup({}, true); |
|
72 |
const button = wrapper.find('button'); |
|
73 |
const editor = wrapper.find('SlateEditor').find('Editor').node; |
|
74 |
||
75 |
// FIXME simulate('change') doesn't work |
|
76 |
editor.onChange(Plain.deserialize('Hello world')); |
|
77 |
||
78 |
expect(button.prop('disabled')).toBe(false); |
|
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
}); |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
|
| 14 | 81 |
// test('click button', () => { |
82 |
// const { props, wrapper } = setup({}, true); |
|
83 |
// wrapper.find('button').simulate('click'); |
|
84 |
// expect(props.addNote.mock.calls.length).toBe(1); |
|
85 |
// }); |
|
86 |
||
87 |
// test('note value on clickbutton', () => { |
|
88 |
// const { props, wrapper } = setup({}, true); |
|
89 |
// // This does nothing... must find a way to make it work |
|
90 |
// wrapper.find('SlateEditor').simulate('change', {target: {value: 'note text'}}); |
|
91 |
// wrapper.find('button').simulate('click'); |
|
92 |
// expect(props.addNote.mock.calls.length).toBe(1); |
|
93 |
// // cf. previous comment |
|
94 |
// //expect(props.addNote.mock.calls[0]).toEqual(['note text']); |
|
95 |
// }); |
|
96 |
||
|
3
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
}); |
|
3b5d37d84cfe
Some code rename and reorg + basic tests
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |