Skip to content

Commit 603ca1d

Browse files
committed
fix: lint and tests
1 parent d2fe291 commit 603ca1d

3 files changed

Lines changed: 75 additions & 17 deletions

File tree

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
22

3+
import { ActivityIndicator } from 'react-native';
4+
35
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react-native';
46

57
import { OverlayProvider } from '../../../contexts';
@@ -46,6 +48,19 @@ const renderComponent = ({ client, channel, props }) => {
4648
);
4749
};
4850

51+
const setPendingUploads = (client, uploads) => {
52+
act(() => {
53+
client.uploadManager.state.partialNext({
54+
uploads: Object.fromEntries(
55+
uploads.map(({ id, uploadProgress }) => [id, { id, uploadProgress }]),
56+
),
57+
});
58+
});
59+
};
60+
61+
const countActivityIndicators = (nodes) =>
62+
nodes.reduce((count, node) => count + node.findAllByType(ActivityIndicator).length, 0);
63+
4964
describe('AttachmentUploadPreviewList', () => {
5065
let client;
5166
let channel;
@@ -60,6 +75,7 @@ describe('AttachmentUploadPreviewList', () => {
6075
jest.clearAllMocks();
6176
cleanup();
6277
act(() => {
78+
client?.uploadManager?.reset();
6379
channel.messageComposer.attachmentManager.initState();
6480
});
6581
});
@@ -103,7 +119,11 @@ describe('AttachmentUploadPreviewList', () => {
103119
it('should render FileAttachmentUploadPreview when the sound package is unavailable', async () => {
104120
const attachments = [
105121
generateAudioAttachment({
122+
asset_url: undefined,
106123
localMetadata: {
124+
file: {
125+
uri: 'file://audio-attachment.mp3',
126+
},
107127
id: 'audio-attachment',
108128
uploadState: FileState.UPLOADING,
109129
},
@@ -115,28 +135,36 @@ describe('AttachmentUploadPreviewList', () => {
115135
act(() => {
116136
channel.messageComposer.attachmentManager.upsertAttachments(attachments);
117137
});
138+
setPendingUploads(client, [{ id: 'audio-attachment' }]);
118139

119140
renderComponent({ channel, client, props });
120141

121-
const { queryAllByTestId } = screen;
142+
const { getAllByTestId, queryAllByTestId } = screen;
122143

123144
await waitFor(() => {
124145
expect(queryAllByTestId('file-attachment-upload-preview')).toHaveLength(1);
125-
expect(queryAllByTestId('upload-progress-indicator')).toHaveLength(1);
146+
expect(countActivityIndicators(getAllByTestId('file-attachment-upload-preview'))).toBe(1);
126147
});
127148
});
128149

129150
describe('FileAttachmentUploadPreview', () => {
130151
it('should render FileAttachmentUploadPreview with all uploading files', async () => {
131152
const attachments = [
132153
generateFileAttachment({
154+
asset_url: undefined,
133155
localMetadata: {
156+
file: {
157+
uri: 'file://file-attachment.xls',
158+
},
134159
id: 'file-attachment',
135160
uploadState: FileState.UPLOADING,
136161
},
137162
}),
138163
generateVideoAttachment({
139164
localMetadata: {
165+
file: {
166+
uri: 'file://video-attachment.mp4',
167+
},
140168
id: 'video-attachment',
141169
uploadState: FileState.UPLOADING,
142170
},
@@ -147,14 +175,15 @@ describe('AttachmentUploadPreviewList', () => {
147175
act(() => {
148176
channel.messageComposer.attachmentManager.upsertAttachments(attachments);
149177
});
178+
setPendingUploads(client, [{ id: 'file-attachment' }, { id: 'video-attachment' }]);
150179

151180
renderComponent({ channel, client, props });
152181

153182
const { getAllByTestId, queryAllByTestId } = screen;
154183

155184
await waitFor(() => {
156185
expect(queryAllByTestId('file-attachment-upload-preview')).toHaveLength(2);
157-
expect(queryAllByTestId('upload-progress-indicator')).toHaveLength(2);
186+
expect(countActivityIndicators(getAllByTestId('file-attachment-upload-preview'))).toBe(2);
158187
});
159188

160189
act(() => {
@@ -285,6 +314,7 @@ describe('AttachmentUploadPreviewList', () => {
285314
generateImageAttachment({
286315
localMetadata: {
287316
id: 'image-attachment',
317+
previewUri: 'file://image-attachment.png',
288318
uploadState: FileState.UPLOADING,
289319
},
290320
}),
@@ -294,14 +324,15 @@ describe('AttachmentUploadPreviewList', () => {
294324
await act(() => {
295325
channel.messageComposer.attachmentManager.upsertAttachments(attachments ?? []);
296326
});
327+
setPendingUploads(client, [{ id: 'image-attachment' }]);
297328

298329
renderComponent({ channel, client, props });
299330

300331
const { getAllByTestId, queryAllByTestId } = screen;
301332

302333
await waitFor(() => {
303334
expect(queryAllByTestId('image-attachment-upload-preview')).toHaveLength(1);
304-
expect(queryAllByTestId('upload-progress-indicator')).toHaveLength(1);
335+
expect(countActivityIndicators(getAllByTestId('image-attachment-upload-preview'))).toBe(1);
305336
});
306337

307338
await act(() => {
@@ -437,6 +468,7 @@ describe('AttachmentUploadPreviewList', () => {
437468
generateImageAttachment({
438469
localMetadata: {
439470
id: 'image-attachment-1',
471+
previewUri: 'file://image-attachment-1.png',
440472
uploadState: FileState.UPLOADING,
441473
},
442474
}),
@@ -464,10 +496,11 @@ describe('AttachmentUploadPreviewList', () => {
464496
await act(() => {
465497
channel.messageComposer.attachmentManager.upsertAttachments(attachments ?? []);
466498
});
499+
setPendingUploads(client, [{ id: 'image-attachment-1' }]);
467500

468501
renderComponent({ channel, client, props });
469502

470-
const { queryAllByTestId } = screen;
503+
const { getAllByTestId, queryAllByTestId } = screen;
471504

472505
await waitFor(() => {
473506
const imageAttachments = queryAllByTestId('image-attachment-upload-preview-image');
@@ -478,7 +511,7 @@ describe('AttachmentUploadPreviewList', () => {
478511

479512
await waitFor(() => {
480513
expect(queryAllByTestId('image-attachment-upload-preview')).toHaveLength(4);
481-
expect(queryAllByTestId('upload-progress-indicator')).toHaveLength(1);
514+
expect(countActivityIndicators(getAllByTestId('image-attachment-upload-preview'))).toBe(1);
482515
expect(queryAllByTestId('retry-upload-progress-indicator')).toHaveLength(1);
483516
expect(queryAllByTestId('inline-not-supported-indicator')).toHaveLength(1);
484517
});

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
22

3+
import { ActivityIndicator } from 'react-native';
4+
35
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react-native';
46

57
import { OverlayProvider } from '../../../contexts';
@@ -41,6 +43,19 @@ const renderComponent = ({ client, channel, props }) => {
4143
);
4244
};
4345

46+
const setPendingUploads = (client, uploads) => {
47+
act(() => {
48+
client.uploadManager.state.partialNext({
49+
uploads: Object.fromEntries(
50+
uploads.map(({ id, uploadProgress }) => [id, { id, uploadProgress }]),
51+
),
52+
});
53+
});
54+
};
55+
56+
const countActivityIndicators = (nodes) =>
57+
nodes.reduce((count, node) => count + node.findAllByType(ActivityIndicator).length, 0);
58+
4459
describe('AudioAttachmentUploadPreview render', () => {
4560
let client;
4661
let channel;
@@ -55,13 +70,15 @@ describe('AudioAttachmentUploadPreview render', () => {
5570
jest.clearAllMocks();
5671
cleanup();
5772
act(() => {
73+
client?.uploadManager?.reset();
5874
channel.messageComposer.attachmentManager.initState();
5975
});
6076
});
6177

6278
it('should render AudioAttachmentUploadPreview with all uploading files', async () => {
6379
const attachments = [
6480
generateAudioAttachment({
81+
asset_url: undefined,
6582
localMetadata: {
6683
file: {
6784
uri: 'file://audio-attachment.mp3',
@@ -76,14 +93,15 @@ describe('AudioAttachmentUploadPreview render', () => {
7693
act(() => {
7794
channel.messageComposer.attachmentManager.upsertAttachments(attachments);
7895
});
96+
setPendingUploads(client, [{ id: 'audio-attachment' }]);
7997

8098
renderComponent({ channel, client, props });
8199

82100
const { getAllByTestId, queryAllByTestId } = screen;
83101

84102
await waitFor(() => {
85103
expect(queryAllByTestId('audio-attachment-upload-preview')).toHaveLength(1);
86-
expect(queryAllByTestId('upload-progress-indicator')).toHaveLength(1);
104+
expect(countActivityIndicators(getAllByTestId('audio-attachment-upload-preview'))).toBe(1);
87105
});
88106

89107
act(() => {

package/src/hooks/__tests__/usePendingAttachmentUpload.test.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import { ChatProvider } from '../../contexts/chatContext/ChatContext';
77
import { usePendingAttachmentUpload } from '../usePendingAttachmentUpload';
88

99
type UploadManagerState = {
10-
uploads: Array<{
11-
id: string;
12-
uploadProgress?: number;
13-
}>;
10+
uploads: Record<
11+
string,
12+
{
13+
id: string;
14+
uploadProgress?: number;
15+
}
16+
>;
1417
};
1518

1619
const createWrapper = (state: StateStore<UploadManagerState>) => {
@@ -35,7 +38,7 @@ describe('usePendingAttachmentUpload', () => {
3538
});
3639

3740
it('briefly holds completed upload progress after a ready upload record disappears', () => {
38-
const state = new StateStore<UploadManagerState>({ uploads: [] });
41+
const state = new StateStore<UploadManagerState>({ uploads: {} });
3942
const { result } = renderHook(() => usePendingAttachmentUpload('upload-id'), {
4043
wrapper: createWrapper(state),
4144
});
@@ -47,7 +50,9 @@ describe('usePendingAttachmentUpload', () => {
4750

4851
act(() => {
4952
state.partialNext({
50-
uploads: [{ id: 'upload-id', uploadProgress: 90 }],
53+
uploads: {
54+
'upload-id': { id: 'upload-id', uploadProgress: 90 },
55+
},
5156
});
5257
});
5358

@@ -57,7 +62,7 @@ describe('usePendingAttachmentUpload', () => {
5762
});
5863

5964
act(() => {
60-
state.partialNext({ uploads: [] });
65+
state.partialNext({ uploads: {} });
6166
});
6267

6368
expect(result.current).toEqual({
@@ -76,19 +81,21 @@ describe('usePendingAttachmentUpload', () => {
7681
});
7782

7883
it('does not hold completed progress when an upload record disappears before reaching the ready threshold', () => {
79-
const state = new StateStore<UploadManagerState>({ uploads: [] });
84+
const state = new StateStore<UploadManagerState>({ uploads: {} });
8085
const { result } = renderHook(() => usePendingAttachmentUpload('upload-id'), {
8186
wrapper: createWrapper(state),
8287
});
8388

8489
act(() => {
8590
state.partialNext({
86-
uploads: [{ id: 'upload-id', uploadProgress: 50 }],
91+
uploads: {
92+
'upload-id': { id: 'upload-id', uploadProgress: 50 },
93+
},
8794
});
8895
});
8996

9097
act(() => {
91-
state.partialNext({ uploads: [] });
98+
state.partialNext({ uploads: {} });
9299
});
93100

94101
expect(result.current).toEqual({

0 commit comments

Comments
 (0)