aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/__tests__/MessageInput-test.js
blob: 143a2491038781eff4d62f6357aa043bb865cfa1 (plain) (blame)
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
import React from 'react';
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 () { }
    };
};

it('Gives immediate focus on to textarea on load', () => {
    expect(document.activeElement.type).toEqual(undefined);
    var wrapper = null;
    act(() => {
        wrapper = create(<MessageInput data={testMessage} onSend={() => { }} />);
    });
    console.log(document.activeElement);
    expect(document.activeElement.type).toEqual('textarea', 'textarea was not focused');
});

it('Submits on ctrl-enter', () => {
    const onSend = jest.fn();
    var messageInput = null;
    act(() => {
        messageInput = create(<MessageInput data={testMessage} onSend={onSend} />);
    });

    let textarea = messageInput.root.findByType('textarea');
    textarea.props.onKeyPress({
        charCode: 13,
        which: 13,
        keyCode: 13,
        ctrlKey: false
    });
    expect(onSend).toHaveBeenCalledTimes(0);
    textarea.props.onKeyPress({
        charCode: 13,
        which: 13,
        keyCode: 13,
        ctrlKey: true
    });
    expect(onSend).toHaveBeenCalledTimes(1);
});

it('Clears text on submit', () => {
    var messageInput = null;
    act(() => {
        messageInput = create(<MessageInput data={testMessage} onSend={() => { }} />);
    });
    let textarea = messageInput.root.findByType('textarea');
    act(() => {
        textarea.value = ' ';
        textarea.props.onChange({ target: { value: ' ', validity: {} } });
    });
    expect(textarea.value).toEqual(' ');
    act(() => {
        messageInput.root.findByType('form').props.onSubmit({ event: {} });
    });
    expect(textarea.value).toEqual('');
});