aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/__tests__/MessageInput.test.js
blob: 0bfe25691f3b9cef95e0c14f858b63cf9eb65076 (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
import { render, screen, fireEvent, waitFor } from '@testing-library/react';

import MessageInput from '../MessageInput';

test('Gives immediate focus on to textarea on load', async () => {
    let draft = 'draft';
    render(<MessageInput text={draft} onSend={async () => { return true; }} />);
    expect(screen.getByText(draft)).toHaveFocus();
});


test('Submits on ctrl-enter and pass validation', async () => {
    let result = false;
    const onSend = jest.fn(async ({ body }) => { result = body === 'YO'; return result; });
    let draft = 'draft';
    render(<MessageInput onSend={onSend} text={draft} />);
    let textarea = screen.getByText(draft);
    fireEvent.change(textarea, { target: { value: 'HI' } });
    // this event should not submit
    fireEvent.keyPress(textarea, {
        charCode: 13,
        which: 13,
        keyCode: 13
    });
    // this event should submit
    fireEvent.keyPress(textarea, {
        charCode: 13,
        which: 13,
        keyCode: 13,
        ctrlKey: true
    });
    expect(onSend).toHaveBeenCalledTimes(1);
    expect(result).toBe(false);
    fireEvent.change(textarea, { target: { value: 'YO' } });
    fireEvent.keyPress(textarea, {
        charCode: 13,
        which: 13,
        keyCode: 13,
        ctrlKey: true
    });
    expect(onSend).toHaveBeenCalledTimes(2);
    expect(result).toBe(true);
    await waitFor(() => expect(textarea).toHaveTextContent(''));
    textarea.focus();
    expect(textarea).toHaveFocus();
});