|
| 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 | +}); |
0 commit comments