diff options
author | Vitaly Takmazov | 2022-10-28 15:52:57 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-13 10:37:58 +0300 |
commit | 58f504e34fe5c37056c4e74ed8204bd0d10cb994 (patch) | |
tree | 59c9daf1be270793c294c78a079d3bc73b4d45e7 /vnext/src/ui/__tests__/MessageInput.test.js | |
parent | d1ae7c0766c1e3319b4a75164123ffd364fd2389 (diff) |
Merge Jest configuration from Next version
Diffstat (limited to 'vnext/src/ui/__tests__/MessageInput.test.js')
-rw-r--r-- | vnext/src/ui/__tests__/MessageInput.test.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/vnext/src/ui/__tests__/MessageInput.test.js b/vnext/src/ui/__tests__/MessageInput.test.js new file mode 100644 index 00000000..1b5b0442 --- /dev/null +++ b/vnext/src/ui/__tests__/MessageInput.test.js @@ -0,0 +1,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.textContent).toEqual('')); + textarea.focus(); + expect(textarea).toHaveFocus(); +}); |