Skip to content

Commit f887ee1

Browse files
committed
tests: add more tests
1 parent 0d547fa commit f887ee1

File tree

3 files changed

+478
-2
lines changed

3 files changed

+478
-2
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import React from 'react';
2+
3+
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react-native';
4+
5+
import { OverlayProvider } from '../../../contexts';
6+
import { initiateClientWithChannels } from '../../../mock-builders/api/initiateClientWithChannels';
7+
import {
8+
generateAudioAttachment,
9+
generateFileAttachment,
10+
generateVideoAttachment,
11+
} from '../../../mock-builders/attachments';
12+
13+
import { FileState } from '../../../utils/utils';
14+
import { Channel } from '../../Channel/Channel';
15+
import { Chat } from '../../Chat/Chat';
16+
import { FileUploadPreview } from '../FileUploadPreview';
17+
18+
jest.mock('../../../native.ts', () => {
19+
const View = require('react-native/Libraries/Components/View/View');
20+
21+
return {
22+
isAudioRecorderAvailable: jest.fn(() => true),
23+
isDocumentPickerAvailable: jest.fn(() => true),
24+
isImageMediaLibraryAvailable: jest.fn(() => true),
25+
isImagePickerAvailable: jest.fn(() => true),
26+
isSoundPackageAvailable: jest.fn(() => true),
27+
NativeHandlers: {
28+
Sound: {
29+
Player: View,
30+
},
31+
},
32+
};
33+
});
34+
35+
const renderComponent = ({ client, channel, props }) => {
36+
return render(
37+
<OverlayProvider>
38+
<Chat client={client}>
39+
<Channel channel={channel}>
40+
<FileUploadPreview {...props} />
41+
</Channel>
42+
</Chat>
43+
</OverlayProvider>,
44+
);
45+
};
46+
47+
describe('AudioAttachmentUploadPreview render', () => {
48+
let client;
49+
let channel;
50+
51+
beforeAll(async () => {
52+
const { client: chatClient, channels } = await initiateClientWithChannels();
53+
client = chatClient;
54+
channel = channels[0];
55+
});
56+
57+
afterEach(() => {
58+
act(() => {
59+
channel.messageComposer.attachmentManager.initState();
60+
});
61+
});
62+
63+
it('should render AudioAttachmentUploadPreview with all uploading files', async () => {
64+
const attachments = [
65+
generateAudioAttachment({
66+
localMetadata: {
67+
file: {
68+
uri: 'file://audio-attachment.mp3',
69+
},
70+
id: 'audio-attachment',
71+
uploadState: FileState.UPLOADING,
72+
},
73+
}),
74+
];
75+
const props = {};
76+
77+
await act(() => {
78+
channel.messageComposer.attachmentManager.upsertAttachments(attachments);
79+
});
80+
81+
renderComponent({ channel, client, props });
82+
83+
const { getByLabelText, getAllByTestId, queryAllByTestId } = screen;
84+
85+
await waitFor(() => {
86+
expect(queryAllByTestId('audio-attachment-upload-preview')).toHaveLength(1);
87+
expect(getByLabelText('audio-attachment-preview')).toBeDefined();
88+
expect(queryAllByTestId('active-upload-progress-indicator')).toHaveLength(1);
89+
expect(queryAllByTestId('upload-progress-indicator')).toHaveLength(1);
90+
});
91+
92+
await act(() => {
93+
fireEvent.press(getAllByTestId('remove-upload-preview')[0]);
94+
});
95+
96+
await waitFor(() => {
97+
expect(channel.messageComposer.attachmentManager.attachments).toHaveLength(0);
98+
});
99+
});
100+
101+
it('should render AudioAttachmentUploadPreview with all uploaded files', async () => {
102+
const attachments = [
103+
generateAudioAttachment({
104+
localMetadata: {
105+
file: {
106+
uri: 'file://audio-attachment.mp3',
107+
},
108+
id: 'audio-attachment',
109+
uploadState: FileState.FINISHED,
110+
},
111+
}),
112+
];
113+
const props = {};
114+
115+
await act(() => {
116+
channel.messageComposer.attachmentManager.upsertAttachments(attachments);
117+
});
118+
119+
renderComponent({ channel, client, props });
120+
121+
const { getByLabelText, queryAllByTestId } = screen;
122+
123+
await waitFor(() => {
124+
expect(queryAllByTestId('audio-attachment-upload-preview')).toHaveLength(1);
125+
expect(getByLabelText('audio-attachment-preview')).toBeDefined();
126+
expect(queryAllByTestId('inactive-upload-progress-indicator')).toHaveLength(1);
127+
});
128+
});
129+
130+
it('should render AudioAttachmentUploadPreview with all failed files', async () => {
131+
const uploadAttachmentSpy = jest.fn();
132+
channel.messageComposer.attachmentManager.uploadAttachment = uploadAttachmentSpy;
133+
const attachments = [
134+
generateAudioAttachment({
135+
localMetadata: {
136+
file: {
137+
uri: 'file://audio-attachment.mp3',
138+
},
139+
id: 'audio-attachment',
140+
uploadState: FileState.FAILED,
141+
},
142+
}),
143+
];
144+
const props = {};
145+
146+
await act(() => {
147+
channel.messageComposer.attachmentManager.upsertAttachments(attachments);
148+
});
149+
150+
renderComponent({ channel, client, props });
151+
152+
const { getAllByTestId, getByLabelText, queryAllByTestId } = screen;
153+
154+
await waitFor(() => {
155+
expect(queryAllByTestId('audio-attachment-upload-preview')).toHaveLength(1);
156+
expect(getByLabelText('audio-attachment-preview')).toBeDefined();
157+
expect(queryAllByTestId('retry-upload-progress-indicator')).toHaveLength(1);
158+
});
159+
160+
await act(() => {
161+
fireEvent.press(getAllByTestId('retry-upload-progress-indicator')[0]);
162+
});
163+
164+
await waitFor(() => {
165+
expect(queryAllByTestId('audio-attachment-upload-preview')).toHaveLength(1);
166+
expect(getByLabelText('audio-attachment-preview')).toBeDefined();
167+
expect(channel.messageComposer.attachmentManager.attachments).toHaveLength(1);
168+
expect(uploadAttachmentSpy).toHaveBeenCalled();
169+
});
170+
});
171+
172+
it('should render AudioAttachmentUploadPreview with all unsupported', async () => {
173+
const attachments = [
174+
generateAudioAttachment({
175+
localMetadata: {
176+
file: {
177+
uri: 'file://audio-attachment.mp3',
178+
},
179+
id: 'audio-attachment',
180+
uploadState: FileState.BLOCKED,
181+
},
182+
}),
183+
];
184+
const props = {};
185+
186+
await act(() => {
187+
channel.messageComposer.attachmentManager.upsertAttachments(attachments);
188+
});
189+
190+
renderComponent({ channel, client, props });
191+
192+
const { getByLabelText, queryAllByTestId, queryAllByText } = screen;
193+
194+
await waitFor(() => {
195+
expect(queryAllByTestId('audio-attachment-upload-preview')).toHaveLength(1);
196+
expect(getByLabelText('audio-attachment-preview')).toBeDefined();
197+
expect(queryAllByText('Not supported')).toHaveLength(1);
198+
});
199+
});
200+
});

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

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,92 @@ import { OverlayProvider } from '../../../contexts';
77
import { initiateClientWithChannels } from '../../../mock-builders/api/initiateClientWithChannels';
88
import { Channel } from '../../Channel/Channel';
99
import { Chat } from '../../Chat/Chat';
10-
import { SendButton } from '../SendButton';
10+
import { CommandsButton } from '../CommandsButton';
1111

1212
const renderComponent = ({ client, channel, props }) => {
1313
return render(
1414
<OverlayProvider>
1515
<Chat client={client}>
1616
<Channel channel={channel}>
17-
<SendButton {...props} />
17+
<CommandsButton {...props} />
1818
</Channel>
1919
</Chat>
2020
</OverlayProvider>,
2121
);
2222
};
23+
24+
describe('CommandsButton', () => {
25+
let client;
26+
let channel;
27+
28+
beforeAll(async () => {
29+
const { client: chatClient, channels } = await initiateClientWithChannels();
30+
client = chatClient;
31+
channel = channels[0];
32+
});
33+
34+
it('should not render component when hasText is true', async () => {
35+
const props = { hasText: true };
36+
renderComponent({ channel, client, props });
37+
38+
const { queryByTestId } = screen;
39+
40+
await waitFor(() => {
41+
expect(queryByTestId('commands-button')).toBeFalsy();
42+
});
43+
});
44+
45+
it('should render component when hasText is false', async () => {
46+
const props = { hasText: false };
47+
renderComponent({ channel, client, props });
48+
49+
const { queryByTestId } = screen;
50+
51+
await waitFor(() => {
52+
expect(queryByTestId('commands-button')).toBeTruthy();
53+
});
54+
});
55+
56+
it('should call handleOnPress callback when the button is clicked if passed', async () => {
57+
const handleOnPress = jest.fn();
58+
const props = { handleOnPress };
59+
60+
renderComponent({ channel, client, props });
61+
62+
const { getByTestId, queryByTestId } = screen;
63+
64+
await waitFor(() => {
65+
expect(queryByTestId('commands-button')).toBeTruthy();
66+
});
67+
68+
await act(() => {
69+
userEvent.press(getByTestId('commands-button'));
70+
});
71+
72+
await waitFor(() => {
73+
expect(handleOnPress).toHaveBeenCalled();
74+
});
75+
});
76+
77+
it('should call textComposer handleChange when the button is clicked by default', async () => {
78+
const props = {};
79+
80+
renderComponent({ channel, client, props });
81+
82+
const { getByTestId, queryByTestId } = screen;
83+
84+
await waitFor(() => {
85+
expect(queryByTestId('commands-button')).toBeTruthy();
86+
});
87+
88+
await act(() => {
89+
userEvent.press(getByTestId('commands-button'));
90+
});
91+
92+
await waitFor(() => {
93+
expect(channel.messageComposer.textComposer.text).toBe('/');
94+
expect(channel.messageComposer.textComposer.selection.start).toBe(1);
95+
expect(channel.messageComposer.textComposer.selection.end).toBe(1);
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)