|
1 | 1 | import React, { useRef } from 'react'; |
2 | | -import { render, screen } from '@testing-library/react'; |
| 2 | +import { render, screen,fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
3 | 4 |
|
4 | 5 | import MessageInput from "../index"; |
5 | 6 |
|
@@ -45,28 +46,75 @@ describe('ui/MessageInput', () => { |
45 | 46 | ).toBe(0); |
46 | 47 | }); |
47 | 48 |
|
48 | | - it.skip('should render send icon if text is present', () => { |
49 | | - const component = shallow( |
50 | | - <MessageInput |
51 | | - onSendMessage={noop} |
52 | | - message={{ |
53 | | - message: 'hello', |
54 | | - mentionedMessageTempalte: 'hello' |
55 | | - }} |
56 | | - /> |
57 | | - ); |
| 49 | + it('should call sendMessage with valid string', async () => { |
| 50 | + const onSendMessage = jest.fn(); |
| 51 | + const textRef = { current: { innerText: null } }; |
| 52 | + const mockText = 'Test Value'; |
| 53 | + |
| 54 | + render(<MessageInput onSendMessage={onSendMessage} ref={textRef} />); |
| 55 | + |
| 56 | + const input = screen.getByRole('textbox'); |
| 57 | + await userEvent.clear(input); |
| 58 | + await userEvent.type(input, mockText); |
| 59 | + expect(input.textContent).toBe(mockText); |
| 60 | + |
| 61 | + fireEvent.keyDown(input, { key: 'Enter' }); |
| 62 | + expect(onSendMessage).toHaveBeenCalledWith({ mentionTemplate: mockText, message: mockText }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should call sendMessage with valid string; new lines included', async () => { |
| 66 | + const onSendMessage = jest.fn(); |
| 67 | + const textRef = { current: { innerText: null } }; |
| 68 | + const mockText = ' \nTest Value \n'; |
| 69 | + |
| 70 | + render(<MessageInput onSendMessage={onSendMessage} ref={textRef} />); |
| 71 | + |
| 72 | + const input = screen.getByRole('textbox'); |
| 73 | + await userEvent.type(input, mockText); |
| 74 | + expect(input.textContent).toBe(mockText); |
| 75 | + |
| 76 | + fireEvent.keyDown(input, { key: 'Enter' }); |
| 77 | + expect(onSendMessage).toHaveBeenCalledWith({ mentionTemplate: mockText, message: mockText }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should not call sendMessage with invalid string; only white spaces', async() => { |
| 81 | + const onSendMessage = jest.fn(); |
| 82 | + const textRef = { current: { innerText: null } }; |
| 83 | + const mockText = ' '; |
| 84 | + |
| 85 | + render(<MessageInput onSendMessage={onSendMessage} ref={textRef} />); |
| 86 | + |
| 87 | + const input = screen.getByRole('textbox'); |
| 88 | + await userEvent.type(input, mockText); |
| 89 | + expect(input.textContent).toBe(mockText); |
| 90 | + |
| 91 | + fireEvent.keyDown(input, { key: 'Enter' }); |
| 92 | + expect(onSendMessage).not.toHaveBeenCalledWith({ mentionTemplate: mockText, message: mockText }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should render send icon if text is present', async() => { |
| 96 | + const onSendMessage = jest.fn(); |
| 97 | + const textRef = { current: { innerText: null } }; |
| 98 | + const mockText = 'hello'; |
| 99 | + |
| 100 | + const { container } = render(<MessageInput onSendMessage={onSendMessage} ref={textRef} />); |
| 101 | + |
| 102 | + const input = screen.getByRole('textbox'); |
| 103 | + await userEvent.type(input, mockText); |
| 104 | + expect(input.textContent).toBe(mockText); |
| 105 | + |
58 | 106 | expect( |
59 | | - component.find('#sendbird-message-input-text-field').exists() |
60 | | - ).toBe(true); |
| 107 | + container.getElementsByClassName('sendbird-message-input-text-field').length |
| 108 | + ).toBe(1); |
61 | 109 | expect( |
62 | | - component.find('.sendbird-message-input--send').exists() |
63 | | - ).toBe(true); |
| 110 | + container.getElementsByClassName('sendbird-message-input--send').length |
| 111 | + ).toBe(1); |
64 | 112 | expect( |
65 | | - component.find('.sendbird-message-input--attach').exists() |
66 | | - ).toBe(false); |
| 113 | + container.getElementsByClassName('sendbird-message-input--attach').length |
| 114 | + ).toBe(0); |
67 | 115 | expect( |
68 | | - component.find('.sendbird-message-input--edit-action').exists() |
69 | | - ).toBe(false); |
| 116 | + container.getElementsByClassName('sendbird-message-input--edit-action').length |
| 117 | + ).toBe(0); |
70 | 118 | }); |
71 | 119 |
|
72 | 120 | it('should display save/cancel button on edit mode', () => { |
|
0 commit comments