1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
import { create, act } from 'react-test-renderer';
import MessageInput from '../MessageInput';
const testMessage = {
mid: 1,
rid: 0,
body: 'test message',
timestamp: new Date().toISOString(),
attach: '',
to: {}
};
window.matchMedia = window.matchMedia || function() {
return {
matches: true,
addListener: function() { },
removeListener: function() { }
};
};
function createMessageInput(data, onFocus, onSend, draft) {
return create(<MessageInput data={data} onSend={onSend} text={draft} />, {
createNodeMock: (element) => {
if (element.type === 'textarea') {
// mock a focus function
return {
focus: onFocus,
style: {}
};
}
return null;
}
});
}
it('Gives immediate focus on to textarea on load', () => {
let focused = false;
act(() => {
createMessageInput(testMessage, () => {
focused = true;
}, () => { });
});
expect(focused).toEqual(true, 'textarea was not focused');
});
it('Submits on ctrl-enter', () => {
const onSend = jest.fn();
var messageInput = null;
act(() => {
messageInput = createMessageInput(testMessage, () => {}, onSend);
});
let textarea = messageInput.root.findByType('textarea');
act(() => {
textarea.props.onKeyPress({
charCode: 13,
which: 13,
keyCode: 13,
ctrlKey: false
});
});
expect(onSend).toHaveBeenCalledTimes(0);
act(() => {
textarea.props.onKeyPress({
charCode: 13,
which: 13,
keyCode: 13,
ctrlKey: true
});
});
expect(onSend).toHaveBeenCalledTimes(1);
expect(textarea.props.value).toEqual('');
act(() => {
textarea.props.onChange({
target: {
value: ' ',
validity: {}
}
});
});
expect(textarea.props.value).toEqual(' ');
act(() => {
messageInput.root.findByType('form').props.onSubmit({ event: {} });
});
expect(textarea.props.value).toEqual('', 'Value should be cleared after submit');
});
it('Show draft text', () => {
var messageInput;
act(() => {
messageInput = createMessageInput(testMessage, () => {}, () => {}, 'yo');
});
let textarea = messageInput.root.findByType('textarea');
expect(textarea.props.value).toEqual('yo', 'Value should match draft');
});
|