aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/__tests__/MessageInput-test.js
blob: 77a71d77fb6024736d07e045b366f2d9e6d73963 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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', () => {
    let focused = false;
    act(() => {
        create(<MessageInput data={testMessage} onSend={() => { }} />, {
            createNodeMock: (element) => {
                if (element.type === 'textarea') {
                    // mock a focus function
                    return {
                        focus: () => {
                            focused = true;
                        },
                        style: {}
                    };
                }
                return null;
            }
        });
    });
    expect(focused).toEqual(true, 'textarea was not focused');
});


it('Submits on ctrl-enter', () => {
    const onSend = jest.fn();
    var messageInput = null;
    act(() => {
        messageInput = create(<MessageInput data={testMessage} onSend={onSend} />, {
            createNodeMock: (element) => {
                if (element.type === 'textarea') {
                    return {
                        focus: () => { },
                        style: {}
                    };
                }
                return null;
            }
        });
    });
    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);
    expect(textarea.props.value).toEqual('');
    act(() => {
        textarea.props.onChange({
            target: {
                value: ' ',
                validity: {}
            }
        });
    });
    expect(textarea.props.value).toEqual(' ');
    /*
        TODO: waiting for react-use-form-state reset
    act(() => {
        messageInput.root.findByType('form').props.onSubmit({ event: {} });
    });
    expect(textarea.props.value).toEqual('', 'Value should be cleared after submit');
    */
});