Skip to content

Commit 168c41c

Browse files
committed
fix: tests
1 parent 1cf9027 commit 168c41c

File tree

4 files changed

+26
-306
lines changed

4 files changed

+26
-306
lines changed
Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { render, waitFor } from '@testing-library/react-native';
3+
import { act, fireEvent, render, waitFor } from '@testing-library/react-native';
44

55
import { getOrCreateChannelApi } from '../../../mock-builders/api/getOrCreateChannel';
66
import { useMockedApis } from '../../../mock-builders/api/useMockedApis';
@@ -10,23 +10,18 @@ import { getTestClientWithUser } from '../../../mock-builders/mock';
1010
import { Channel } from '../../Channel/Channel';
1111
import { Chat } from '../../Chat/Chat';
1212
import { AutoCompleteInput } from '../AutoCompleteInput';
13+
import { AutoCompleteSuggestionList } from '../AutoCompleteSuggestionList';
1314

1415
describe('AutoCompleteInput', () => {
1516
const clientUser = generateUser();
1617
let chatClient;
1718
let channel;
1819

19-
const getComponent = (props = {}) => (
20+
const getAutoCompleteComponent = () => (
2021
<Chat client={chatClient}>
21-
<Channel
22-
channel={channel}
23-
triggerSettings={ACITriggerSettings({
24-
channel,
25-
onMentionSelectItem: jest.fn(),
26-
t: jest.fn(),
27-
})}
28-
>
29-
<AutoCompleteInput giphyEnabled onChange={jest.fn} text={props.text} />
22+
<Channel channel={channel}>
23+
<AutoCompleteInput />
24+
<AutoCompleteSuggestionList />
3025
</Channel>
3126
</Chat>
3227
);
@@ -41,41 +36,34 @@ describe('AutoCompleteInput', () => {
4136

4237
beforeEach(async () => {
4338
chatClient = await getTestClientWithUser(clientUser);
39+
await initializeChannel(generateChannelResponse());
4440
});
4541

4642
afterEach(() => {
4743
channel = null;
4844
});
4945

5046
it('should render AutoCompleteInput and trigger open/close suggestions with / commands', async () => {
51-
const props = {
52-
closeSuggestions: jest.fn(),
53-
openSuggestions: jest.fn(),
54-
};
47+
const { queryByTestId } = render(getAutoCompleteComponent());
5548

56-
await initializeChannel(generateChannelResponse());
49+
const input = queryByTestId('auto-complete-text-input');
5750

58-
const { queryByTestId, rerender } = render(getComponent(props));
51+
const onSelectionChange = input.props.onSelectionChange;
5952

6053
await waitFor(() => {
61-
expect(queryByTestId('auto-complete-text-input')).toBeTruthy();
62-
expect(props.closeSuggestions).toHaveBeenCalledTimes(0);
63-
expect(props.openSuggestions).toHaveBeenCalledTimes(0);
54+
expect(input).toBeTruthy();
6455
});
6556

66-
rerender(getComponent({ ...props, text: '/' }));
67-
68-
await waitFor(() => {
69-
expect(queryByTestId('auto-complete-text-input')).toBeTruthy();
70-
});
71-
72-
rerender(getComponent({ ...props, text: '' }));
73-
rerender(getComponent(props));
74-
75-
await waitFor(() => {
76-
expect(queryByTestId('auto-complete-text-input')).toBeTruthy();
57+
await act(async () => {
58+
await onSelectionChange({
59+
nativeEvent: {
60+
selection: {
61+
end: 1,
62+
start: 1,
63+
},
64+
},
65+
});
66+
await fireEvent.changeText(input, '/');
7767
});
7868
});
79-
80-
// TODO: figure out how to make tests work for @ mentions with needing to update function state values
8169
});

package/src/components/MessageInput/__tests__/MessageInput.test.js

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ describe('MessageInput', () => {
8888

8989
beforeEach(async () => {
9090
chatClient = await getTestClientWithUser(clientUser);
91+
await initializeChannel(generateChannelResponse());
9192
});
9293

9394
afterEach(() => {
9495
channel = null;
9596
cleanup();
97+
jest.clearAllMocks();
9698
});
9799

98100
it('should render MessageInput', async () => {
99-
await initializeChannel(generateChannelResponse());
100-
101101
const openPicker = jest.fn();
102102
const closePicker = jest.fn();
103103
const attachmentValue = { closePicker, openPicker };
@@ -116,17 +116,7 @@ describe('MessageInput', () => {
116116
});
117117

118118
it('trigger file size threshold limit alert when images size above the limit', async () => {
119-
await initializeChannel(generateChannelResponse());
120-
121-
render(
122-
<OverlayProvider>
123-
<Chat client={chatClient}>
124-
<Channel channel={channel}>
125-
<MessageInput />
126-
</Channel>
127-
</Chat>
128-
</OverlayProvider>,
129-
);
119+
render(getComponent());
130120

131121
// Both for files and for images triggered in one test itself.
132122
await waitFor(() => {
@@ -135,9 +125,6 @@ describe('MessageInput', () => {
135125
});
136126

137127
it('should start the audio recorder on long press and cleanup on unmount', async () => {
138-
jest.clearAllMocks();
139-
140-
await initializeChannel(generateChannelResponse());
141128
const userBot = userEvent.setup();
142129

143130
const { queryByTestId, unmount } = render(
@@ -167,9 +154,6 @@ describe('MessageInput', () => {
167154
});
168155

169156
it('should trigger an alert if a normal press happened on audio recording', async () => {
170-
jest.clearAllMocks();
171-
172-
await initializeChannel(generateChannelResponse());
173157
const userBot = userEvent.setup();
174158

175159
const { queryByTestId } = render(
@@ -193,8 +177,6 @@ describe('MessageInput', () => {
193177
});
194178

195179
it('should render the SendMessageDisallowedIndicator if the send-message capability is not present', async () => {
196-
await initializeChannel(generateChannelResponse());
197-
198180
const { queryByTestId } = render(
199181
<Chat client={chatClient}>
200182
<Channel audioRecordingEnabled channel={channel}>
@@ -223,31 +205,15 @@ describe('MessageInput', () => {
223205
});
224206

225207
it('should not render the SendMessageDisallowedIndicator if the channel is frozen and the send-message capability is present', async () => {
226-
await initializeChannel(generateChannelResponse({ channel: { frozen: true } }));
227-
228-
const { queryByTestId } = render(
229-
<Chat client={chatClient}>
230-
<Channel audioRecordingEnabled channel={channel}>
231-
<MessageInput />
232-
</Channel>
233-
</Chat>,
234-
);
208+
const { queryByTestId } = render(getComponent());
235209

236210
await waitFor(() => {
237211
expect(queryByTestId('send-message-disallowed-indicator')).toBeNull();
238212
});
239213
});
240214

241215
it('should render the SendMessageDisallowedIndicator in a frozen channel only if the send-message capability is not present', async () => {
242-
await initializeChannel(generateChannelResponse({ channel: { frozen: true } }));
243-
244-
const { queryByTestId } = render(
245-
<Chat client={chatClient}>
246-
<Channel audioRecordingEnabled channel={channel}>
247-
<MessageInput />
248-
</Channel>
249-
</Chat>,
250-
);
216+
const { queryByTestId } = render(getComponent());
251217

252218
act(() => {
253219
chatClient.dispatchEvent({
@@ -276,8 +242,6 @@ describe('MessageInput', () => {
276242
};
277243

278244
it('should not render the SendMessageDisallowedIndicator if we are editing a message, regardless of capabilities', async () => {
279-
await initializeChannel(generateChannelResponse());
280-
281245
const { queryByTestId } = render(
282246
<Chat client={chatClient}>
283247
<Channel audioRecordingEnabled channel={channel}>

package/src/contexts/__tests__/index.test.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
useOverlayContext,
1414
useOwnCapabilitiesContext,
1515
usePaginatedMessageListContext,
16-
useSuggestionsContext,
1716
useTheme,
1817
useThreadContext,
1918
useTypingContext,
@@ -45,10 +44,6 @@ describe('contexts hooks in a component throws an error with message when not wr
4544
useOwnCapabilitiesContext,
4645
'The useOwnCapabilitiesContext hook was called outside the Channel Component. Make sure you have configured Channel component correctly - https://getstream.io/chat/docs/sdk/reactnative/basics/hello_stream_chat/#channel',
4746
],
48-
[
49-
useSuggestionsContext,
50-
'The useSuggestionsContext hook was called outside of the SuggestionsContext provider. Make sure you have configured Channel component correctly - https://getstream.io/chat/docs/sdk/reactnative/basics/hello_stream_chat/#channel',
51-
],
5247
[
5348
useTypingContext,
5449
'The useTypingContext hook was called outside of the TypingContext provider. Make sure you have configured Channel component correctly - https://getstream.io/chat/docs/sdk/reactnative/basics/hello_stream_chat/#channel',

0 commit comments

Comments
 (0)