diff options
author | 2019-05-04 21:13:12 +0300 | |
---|---|---|
committer | 2023-01-13 10:37:54 +0300 | |
commit | f470636a70943a8ecad8bddc791a1c2dddd28e1e (patch) | |
tree | c43d109d88adbde9a696084070cdd92c6b9a004b /vnext/src/ui/__tests__/MessageInput-test.js | |
parent | 3d7d213e3ddc5bf4f71d536f31677b768aa3b7c0 (diff) |
Components -> UI
Diffstat (limited to 'vnext/src/ui/__tests__/MessageInput-test.js')
-rw-r--r-- | vnext/src/ui/__tests__/MessageInput-test.js | 95 |
1 files changed, 95 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..7ac69ed0 --- /dev/null +++ b/vnext/src/ui/__tests__/MessageInput-test.js @@ -0,0 +1,95 @@ +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'); + act(() => { + + textarea.props.onKeyPress({ + charCode: 13, + which: 13, + keyCode: 13, + ctrlKey: false + }); + }); + expect(onSend).toHaveBeenCalledTimes(0); + act(() => { + 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(' '); + act(() => { + messageInput.root.findByType('form').props.onSubmit({ event: {} }); + }); + expect(textarea.props.value).toEqual('', 'Value should be cleared after submit'); +}); |