1- import { TranslationProvider } from '../../../../context' ;
2- import { renderHook } from '@testing-library/react' ;
1+ import { act , renderHook } from '@testing-library/react' ;
32import React from 'react' ;
43import { useMediaRecorder } from '../useMediaRecorder' ;
54import { EventEmitterMock , MediaRecorderMock } from '../../../../mock-builders/browser' ;
6- import { act } from '@testing-library/react' ;
75import { DEFAULT_AMPLITUDE_RECORDER_CONFIG } from '../../classes/AmplitudeRecorder' ;
86import { DEFAULT_AUDIO_TRANSCODER_CONFIG } from '../../classes' ;
9- import { generateVoiceRecordingAttachment } from '../../../../mock-builders' ;
7+ import {
8+ generateVoiceRecordingAttachment ,
9+ initClientWithChannels ,
10+ } from '../../../../mock-builders' ;
11+ import { Chat } from '../../../Chat' ;
12+ import { Channel } from '../../../Channel' ;
1013
1114window . MediaRecorder = MediaRecorderMock ;
1215
1316const handleSubmit = jest . fn ( ) ;
14- const uploadAttachment = jest . fn ( ) ;
1517
1618const defaultMockPermissionState = 'prompt' ;
1719const status = new EventEmitterMock ( ) ;
@@ -20,21 +22,23 @@ window.navigator.permissions = {
2022 query : jest . fn ( ) . mockResolvedValue ( status ) ,
2123} ;
2224
23- const translationContext = {
24- t : ( s ) => s ,
25- } ;
26-
2725const render = async ( params = { } ) => {
26+ const {
27+ channels : [ channel ] ,
28+ client,
29+ } = await initClientWithChannels ( ) ;
2830 const wrapper = ( { children } ) => (
29- < TranslationProvider value = { translationContext } > { children } </ TranslationProvider >
31+ < Chat client = { client } >
32+ < Channel channel = { channel } > { children } </ Channel >
33+ </ Chat >
3034 ) ;
3135 let result ;
3236 await act ( async ( ) => {
3337 result = await renderHook ( ( ) => useMediaRecorder ( { enabled : true , ...params } ) , {
3438 wrapper,
3539 } ) ;
3640 } ) ;
37- return result ;
41+ return { channel , ... result } ;
3842} ;
3943
4044describe ( 'useMediaRecorder' , ( ) => {
@@ -107,70 +111,89 @@ describe('useMediaRecorder', () => {
107111 describe ( 'completeRecording' , ( ) => {
108112 it ( 'does nothing if recording is disabled' , async ( ) => {
109113 const {
114+ channel,
110115 result : {
111116 current : { completeRecording } ,
112117 } ,
113- } = await render ( { enabled : false , handleSubmit, uploadAttachment } ) ;
118+ } = await render ( { enabled : false , handleSubmit } ) ;
119+ const uploadAttachmentSpy = jest . spyOn (
120+ channel . messageComposer . attachmentManager ,
121+ 'uploadAttachment' ,
122+ ) ;
114123 await completeRecording ( ) ;
115- expect ( uploadAttachment ) . not . toHaveBeenCalled ( ) ;
124+ expect ( uploadAttachmentSpy ) . not . toHaveBeenCalled ( ) ;
116125 expect ( handleSubmit ) . not . toHaveBeenCalled ( ) ;
117126 } ) ;
118127
119128 it ( 'does nothing if recording attachment is not generated on stop' , async ( ) => {
120129 const {
130+ channel,
121131 result : {
122132 current : { completeRecording, recorder } ,
123133 } ,
124- } = await render ( { handleSubmit, uploadAttachment } ) ;
134+ } = await render ( { handleSubmit } ) ;
135+ const uploadAttachmentSpy = jest . spyOn (
136+ channel . messageComposer . attachmentManager ,
137+ 'uploadAttachment' ,
138+ ) ;
125139 const recorderStopSpy = jest . spyOn ( recorder , 'stop' ) . mockResolvedValue ( undefined ) ;
126140 const recorderCleanUpSpy = jest
127141 . spyOn ( recorder , 'cleanUp' )
128142 . mockResolvedValue ( undefined ) ;
129143 await completeRecording ( ) ;
130144 expect ( recorderStopSpy ) . toHaveBeenCalledWith ( ) ;
131145 expect ( recorderCleanUpSpy ) . not . toHaveBeenCalledWith ( ) ;
132- expect ( uploadAttachment ) . not . toHaveBeenCalled ( ) ;
146+ expect ( uploadAttachmentSpy ) . not . toHaveBeenCalled ( ) ;
133147 expect ( handleSubmit ) . not . toHaveBeenCalled ( ) ;
134148 } ) ;
135149
136150 it ( 'uploads and submits the attachment' , async ( ) => {
137151 const generatedVoiceRecording = generateVoiceRecordingAttachment ( ) ;
138152 const {
153+ channel,
139154 result : {
140155 current : { completeRecording, recorder } ,
141156 } ,
142- } = await render ( { handleSubmit, uploadAttachment } ) ;
157+ } = await render ( { handleSubmit } ) ;
158+ const uploadAttachmentSpy = jest . spyOn (
159+ channel . messageComposer . attachmentManager ,
160+ 'uploadAttachment' ,
161+ ) ;
143162 jest . spyOn ( recorder , 'stop' ) . mockResolvedValue ( generatedVoiceRecording ) ;
144163 const recorderCleanUpSpy = jest
145164 . spyOn ( recorder , 'cleanUp' )
146165 . mockResolvedValue ( undefined ) ;
147166 await act ( ( ) => {
148167 completeRecording ( ) ;
149168 } ) ;
150- expect ( uploadAttachment ) . toHaveBeenCalledWith ( generatedVoiceRecording ) ;
169+ expect ( uploadAttachmentSpy ) . toHaveBeenCalledWith ( generatedVoiceRecording ) ;
151170 expect ( handleSubmit ) . toHaveBeenCalledWith ( ) ;
152171 expect ( recorderCleanUpSpy ) . toHaveBeenCalledWith ( ) ;
153172 } ) ;
154173
155174 it ( 'uploads but does not submit the attachment if multiple async messages enabled' , async ( ) => {
156175 const generatedVoiceRecording = generateVoiceRecordingAttachment ( ) ;
157176 const {
177+ channel,
158178 result : {
159179 current : { completeRecording, recorder } ,
160180 } ,
161181 } = await render ( {
162182 asyncMessagesMultiSendEnabled : true ,
163183 handleSubmit,
164- uploadAttachment,
165184 } ) ;
185+ const uploadAttachmentSpy = jest . spyOn (
186+ channel . messageComposer . attachmentManager ,
187+ 'uploadAttachment' ,
188+ ) ;
166189 jest . spyOn ( recorder , 'stop' ) . mockResolvedValue ( generatedVoiceRecording ) ;
167190 const recorderCleanUpSpy = jest
168191 . spyOn ( recorder , 'cleanUp' )
169192 . mockResolvedValue ( undefined ) ;
170193 await act ( ( ) => {
171194 completeRecording ( ) ;
172195 } ) ;
173- expect ( uploadAttachment ) . toHaveBeenCalledWith ( generatedVoiceRecording ) ;
196+ expect ( uploadAttachmentSpy ) . toHaveBeenCalledWith ( generatedVoiceRecording ) ;
174197 expect ( handleSubmit ) . not . toHaveBeenCalled ( ) ;
175198 expect ( recorderCleanUpSpy ) . toHaveBeenCalledWith ( ) ;
176199 } ) ;
0 commit comments