aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/__tests__/MessageInput-test.js
blob: ef7a81e6ff711495ffff87f6a2d182341ec4840a (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 { mount } from 'enzyme';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

import MessageInput from '../MessageInput';

// Remove when it will be fixed: https://github.com/airbnb/enzyme/issues/1875
jest.mock('react', () => {
    const r = jest.requireActual('react');
    return { ...r, memo: (x) => x };
});

configure({ adapter: new Adapter() });

const testMessage = {
    mid: 1,
    user: { uid: 1, uname: 'ugnich' },
    body: 'test message',
    timestamp: new Date().toISOString()
};

window.matchMedia = window.matchMedia || function() {
    return {
        matches: true,
        addListener: function() { },
        removeListener: function() { }
    };
};

it('Gives immediate focus on to textarea on load', () => {
    const wrapper = mount(<MessageInput data={testMessage} onSend={() => { }} />);
    const textareaRef = wrapper.instance().textarea;

    jest.spyOn(textareaRef.current, 'focus');

    wrapper.instance().componentDidMount();
    expect(textareaRef.current.focus).toHaveBeenCalledTimes(1);
});

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

    let textarea = messageInput.find('textarea');
    textarea.simulate('keypress', {
        charCode: 13,
        which: 13,
        keyCode: 13,
        ctrlKey: false
    });
    expect(onSend).toHaveBeenCalledTimes(0);
    textarea.simulate('keypress', {
        charCode: 13,
        which: 13,
        keyCode: 13,
        ctrlKey: true
    });
    expect(onSend).toHaveBeenCalledTimes(1);
});

it('Clears template on submit', () => {
    const messageInput = mount(<MessageInput data={testMessage} onSend={() => { }} />);
    messageInput.find('textarea').simulate('change', {
        target: {
            value: ' '
        }
    });
    expect(messageInput.state().body).toEqual(' ');
    messageInput.find('Button').simulate('click');
    expect(messageInput.state().body).toEqual('');
});