|
1 | 1 | import React from 'react'; |
2 | 2 |
|
3 | | -import { act, fireEvent, render, waitFor } from '@testing-library/react-native'; |
| 3 | +import { |
| 4 | + act, |
| 5 | + cleanup, |
| 6 | + fireEvent, |
| 7 | + render, |
| 8 | + screen, |
| 9 | + userEvent, |
| 10 | + waitFor, |
| 11 | +} from '@testing-library/react-native'; |
4 | 12 |
|
5 | | -import { getOrCreateChannelApi } from '../../../mock-builders/api/getOrCreateChannel'; |
6 | | -import { useMockedApis } from '../../../mock-builders/api/useMockedApis'; |
7 | | -import { generateChannelResponse } from '../../../mock-builders/generator/channel'; |
8 | | -import { generateUser } from '../../../mock-builders/generator/user'; |
9 | | -import { getTestClientWithUser } from '../../../mock-builders/mock'; |
| 13 | +import { initiateClientWithChannels } from '../../../mock-builders/api/initiateClientWithChannels'; |
10 | 14 | import { Channel } from '../../Channel/Channel'; |
11 | 15 | import { Chat } from '../../Chat/Chat'; |
12 | 16 | import { AutoCompleteInput } from '../AutoCompleteInput'; |
13 | | -import { AutoCompleteSuggestionList } from '../AutoCompleteSuggestionList'; |
| 17 | + |
| 18 | +const renderComponent = ({ channelProps, client, props }) => { |
| 19 | + return render( |
| 20 | + <Chat client={client}> |
| 21 | + <Channel {...channelProps}> |
| 22 | + <AutoCompleteInput {...props} /> |
| 23 | + </Channel> |
| 24 | + </Chat>, |
| 25 | + ); |
| 26 | +}; |
14 | 27 |
|
15 | 28 | describe('AutoCompleteInput', () => { |
16 | | - const clientUser = generateUser(); |
17 | | - let chatClient; |
| 29 | + let client; |
18 | 30 | let channel; |
19 | 31 |
|
20 | | - const getAutoCompleteComponent = () => ( |
21 | | - <Chat client={chatClient}> |
22 | | - <Channel channel={channel}> |
23 | | - <AutoCompleteInput /> |
24 | | - <AutoCompleteSuggestionList /> |
25 | | - </Channel> |
26 | | - </Chat> |
27 | | - ); |
| 32 | + beforeAll(async () => { |
| 33 | + const { client: chatClient, channels } = await initiateClientWithChannels(); |
| 34 | + client = chatClient; |
| 35 | + channel = channels[0]; |
| 36 | + }); |
28 | 37 |
|
29 | | - const initializeChannel = async (c) => { |
30 | | - useMockedApis(chatClient, [getOrCreateChannelApi(c)]); |
| 38 | + afterEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + cleanup(); |
| 41 | + }); |
31 | 42 |
|
32 | | - channel = chatClient.channel('messaging'); |
| 43 | + it('should render AutoCompleteInput', async () => { |
| 44 | + const channelProps = { channel }; |
| 45 | + const props = {}; |
33 | 46 |
|
34 | | - await channel.watch(); |
35 | | - }; |
| 47 | + renderComponent({ channelProps, client, props }); |
36 | 48 |
|
37 | | - beforeEach(async () => { |
38 | | - chatClient = await getTestClientWithUser(clientUser); |
39 | | - await initializeChannel(generateChannelResponse()); |
40 | | - }); |
| 49 | + const { queryByTestId } = screen; |
41 | 50 |
|
42 | | - afterEach(() => { |
43 | | - channel = null; |
| 51 | + const input = queryByTestId('auto-complete-text-input'); |
| 52 | + |
| 53 | + await waitFor(() => { |
| 54 | + expect(input).toBeTruthy(); |
| 55 | + }); |
44 | 56 | }); |
45 | 57 |
|
46 | | - it('should render AutoCompleteInput and trigger open/close suggestions with / commands', async () => { |
47 | | - const { queryByTestId } = render(getAutoCompleteComponent()); |
| 58 | + it('should have the editable prop as false when the message composer config is set', async () => { |
| 59 | + const channelProps = { channel }; |
| 60 | + const props = {}; |
| 61 | + |
| 62 | + channel.messageComposer.updateConfig({ text: { enabled: false } }); |
| 63 | + |
| 64 | + renderComponent({ channelProps, client, props }); |
| 65 | + |
| 66 | + const { queryByTestId } = screen; |
48 | 67 |
|
49 | 68 | const input = queryByTestId('auto-complete-text-input'); |
50 | 69 |
|
51 | | - const onSelectionChange = input.props.onSelectionChange; |
| 70 | + await waitFor(() => { |
| 71 | + expect(input.props.editable).toBeFalsy(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should have the maxLength same as the one on the config of channel', async () => { |
| 76 | + jest.spyOn(channel, 'getConfig').mockReturnValue({ |
| 77 | + max_message_length: 10, |
| 78 | + }); |
| 79 | + const channelProps = { channel }; |
| 80 | + const props = {}; |
| 81 | + |
| 82 | + renderComponent({ channelProps, client, props }); |
| 83 | + |
| 84 | + const { queryByTestId } = screen; |
| 85 | + |
| 86 | + const input = queryByTestId('auto-complete-text-input'); |
52 | 87 |
|
53 | 88 | await waitFor(() => { |
54 | | - expect(input).toBeTruthy(); |
| 89 | + expect(input.props.maxLength).toBe(10); |
55 | 90 | }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should call the textComposer handleChange when the onChangeText is triggered', () => { |
| 94 | + const { textComposer } = channel.messageComposer; |
| 95 | + |
| 96 | + const spyHandleChange = jest.spyOn(textComposer, 'handleChange'); |
| 97 | + |
| 98 | + const channelProps = { channel }; |
| 99 | + const props = {}; |
| 100 | + |
| 101 | + renderComponent({ channelProps, client, props }); |
56 | 102 |
|
57 | | - await act(async () => { |
58 | | - await onSelectionChange({ |
| 103 | + const { queryByTestId } = screen; |
| 104 | + |
| 105 | + const input = queryByTestId('auto-complete-text-input'); |
| 106 | + |
| 107 | + act(() => { |
| 108 | + userEvent.type(input, 'hello'); |
| 109 | + }); |
| 110 | + |
| 111 | + waitFor(() => { |
| 112 | + expect(spyHandleChange).toHaveBeenCalled(); |
| 113 | + expect(spyHandleChange).toHaveBeenCalledWith({ |
| 114 | + selection: { end: 5, start: 5 }, |
| 115 | + text: 'hello', |
| 116 | + }); |
| 117 | + expect(input.props.value).toBe('hello'); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should style the text input with maxHeight that is set by the layout', () => { |
| 122 | + const channelProps = { channel }; |
| 123 | + const props = { numberOfLines: 10 }; |
| 124 | + |
| 125 | + renderComponent({ channelProps, client, props }); |
| 126 | + |
| 127 | + const { queryByTestId } = screen; |
| 128 | + |
| 129 | + const input = queryByTestId('auto-complete-text-input'); |
| 130 | + |
| 131 | + act(() => { |
| 132 | + fireEvent(input, 'contentSizeChange', { |
| 133 | + nativeEvent: { |
| 134 | + contentSize: { height: 100 }, |
| 135 | + }, |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + waitFor(() => { |
| 140 | + expect(input.props.style[1].maxHeight).toBe(1000); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should call the textComposer setSelection when the onSelectionChange is triggered', () => { |
| 145 | + const { textComposer } = channel.messageComposer; |
| 146 | + |
| 147 | + const spySetSelection = jest.spyOn(textComposer, 'setSelection'); |
| 148 | + |
| 149 | + const channelProps = { channel }; |
| 150 | + const props = {}; |
| 151 | + |
| 152 | + renderComponent({ channelProps, client, props }); |
| 153 | + |
| 154 | + const { queryByTestId } = screen; |
| 155 | + |
| 156 | + const input = queryByTestId('auto-complete-text-input'); |
| 157 | + |
| 158 | + act(() => { |
| 159 | + fireEvent(input, 'selectionChange', { |
59 | 160 | nativeEvent: { |
60 | | - selection: { |
61 | | - end: 1, |
62 | | - start: 1, |
63 | | - }, |
| 161 | + selection: { end: 5, start: 5 }, |
64 | 162 | }, |
65 | 163 | }); |
66 | | - await fireEvent.changeText(input, '/'); |
| 164 | + }); |
| 165 | + |
| 166 | + waitFor(() => { |
| 167 | + expect(spySetSelection).toHaveBeenCalled(); |
| 168 | + expect(spySetSelection).toHaveBeenCalledWith({ end: 5, start: 5 }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + // TODO: Add a test for command |
| 173 | + it.each([ |
| 174 | + { cooldownActive: false, result: 'Send a message' }, |
| 175 | + { cooldownActive: true, result: 'Slow mode ON' }, |
| 176 | + ])('should have the placeholderText as Slow mode ON when cooldown is active', async (data) => { |
| 177 | + const channelProps = { channel }; |
| 178 | + const props = { |
| 179 | + cooldownActive: data.cooldownActive, |
| 180 | + }; |
| 181 | + |
| 182 | + renderComponent({ channelProps, client, props }); |
| 183 | + |
| 184 | + const { queryByTestId } = screen; |
| 185 | + |
| 186 | + const input = queryByTestId('auto-complete-text-input'); |
| 187 | + |
| 188 | + await waitFor(() => { |
| 189 | + expect(input.props.placeholder).toBe(data.result); |
67 | 190 | }); |
68 | 191 | }); |
69 | 192 | }); |
0 commit comments