11/* eslint-disable no-console */
2- import React from 'react' ;
2+ import React , { act } from 'react' ;
33import { render , renderHook , screen } from '@testing-library/react' ;
44import SendbirdProvider , { SendbirdProviderProps } from './Sendbird' ;
55import useSendbirdStateContext from './Sendbird/context/hooks/useSendbirdStateContext' ;
66import { match } from 'ts-pattern' ;
77import { DEFAULT_MULTIPLE_FILES_MESSAGE_LIMIT , DEFAULT_UPLOAD_SIZE_LIMIT } from '../utils/consts' ;
88
9+ jest . mock ( '@sendbird/chat' , ( ) => {
10+ const mockConnect = jest . fn ( ) . mockResolvedValue ( {
11+ userId : 'test-user-id' ,
12+ nickname : 'test-nickname' ,
13+ profileUrl : 'test-profile-url' ,
14+ } ) ;
15+ const mockDisconnect = jest . fn ( ) . mockResolvedValue ( null ) ;
16+ const mockUpdateCurrentUserInfo = jest . fn ( ) . mockResolvedValue ( null ) ;
17+ const mockAddExtension = jest . fn ( ) . mockReturnThis ( ) ;
18+ const mockAddSendbirdExtensions = jest . fn ( ) . mockReturnThis ( ) ;
19+ const mockGetMessageTemplatesByToken = jest . fn ( ) . mockResolvedValue ( {
20+ hasMore : false ,
21+ token : null ,
22+ templates : [ ] ,
23+ } ) ;
24+
25+ const mockSdk = {
26+ init : jest . fn ( ) . mockImplementation ( ( ) => mockSdk ) ,
27+ connect : mockConnect ,
28+ disconnect : mockDisconnect ,
29+ updateCurrentUserInfo : mockUpdateCurrentUserInfo ,
30+ addExtension : mockAddExtension ,
31+ addSendbirdExtensions : mockAddSendbirdExtensions ,
32+ GroupChannel : { createMyGroupChannelListQuery : jest . fn ( ) } ,
33+ message : {
34+ getMessageTemplatesByToken : mockGetMessageTemplatesByToken ,
35+ } ,
36+ appInfo : {
37+ uploadSizeLimit : 1024 * 1024 * 5 ,
38+ multipleFilesMessageFileCountLimit : 10 ,
39+ } ,
40+ } ;
41+
42+ return {
43+ __esModule : true ,
44+ default : mockSdk ,
45+ SendbirdProduct : {
46+ UIKIT_CHAT : 'UIKIT_CHAT' ,
47+ } ,
48+ SendbirdPlatform : {
49+ JS : 'JS' ,
50+ } ,
51+ DeviceOsPlatform : {
52+ WEB : 'WEB' ,
53+ MOBILE_WEB : 'MOBILE_WEB' ,
54+ } ,
55+ } ;
56+ } ) ;
57+
958const mockProps : SendbirdProviderProps = {
1059 appId : 'test-app-id' ,
1160 userId : 'test-user-id' ,
@@ -39,37 +88,6 @@ const mockProps: SendbirdProviderProps = {
3988 children : < div > Test Child</ div > ,
4089} ;
4190
42- const mockDisconnect = jest . fn ( ) ;
43- const mockConnect = jest . fn ( ) ;
44- const mockUpdateCurrentUserInfo = jest . fn ( ) ;
45-
46- /**
47- * Mocking Sendbird SDK
48- * sdk.connect causes DOMException issue in jest.
49- * Because it retries many times to connect indexDB.
50- */
51- jest . mock ( '@sendbird/chat' , ( ) => {
52- return {
53- __esModule : true ,
54- default : jest . fn ( ) . mockImplementation ( ( ) => {
55- return {
56- connect : mockConnect . mockResolvedValue ( {
57- userId : 'test-user-id' ,
58- nickname : 'test-nickname' ,
59- profileUrl : 'test-profile-url' ,
60- } ) ,
61- disconnect : mockDisconnect . mockResolvedValue ( null ) ,
62- updateCurrentUserInfo : mockUpdateCurrentUserInfo . mockResolvedValue ( null ) ,
63- GroupChannel : { createMyGroupChannelListQuery : jest . fn ( ) } ,
64- appInfo : {
65- uploadSizeLimit : 1024 * 1024 * 5 , // 5MB
66- multipleFilesMessageFileCountLimit : 10 ,
67- } ,
68- } ;
69- } ) ,
70- } ;
71- } ) ;
72-
7391describe ( 'SendbirdProvider Props & Context Interface Validation' , ( ) => {
7492 const originalConsoleError = console . error ;
7593 let originalFetch ;
@@ -95,9 +113,6 @@ describe('SendbirdProvider Props & Context Interface Validation', () => {
95113
96114 beforeEach ( ( ) => {
97115 jest . clearAllMocks ( ) ;
98- mockConnect . mockClear ( ) ;
99- mockDisconnect . mockClear ( ) ;
100- mockUpdateCurrentUserInfo . mockClear ( ) ;
101116
102117 global . MediaRecorder = {
103118 isTypeSupported : jest . fn ( ( type ) => {
@@ -119,24 +134,27 @@ describe('SendbirdProvider Props & Context Interface Validation', () => {
119134 } ) ;
120135
121136 it ( 'should accept all legacy props without type errors' , async ( ) => {
122- const { rerender } = render (
123- < SendbirdProvider { ...mockProps } >
124- { mockProps . children }
125- </ SendbirdProvider > ,
126- ) ;
137+ const { rerender } = await act ( async ( ) => (
138+ render (
139+ < SendbirdProvider { ...mockProps } >
140+ { mockProps . children }
141+ </ SendbirdProvider > ,
142+ )
143+ ) ) ;
127144
128- rerender (
129- < SendbirdProvider { ...mockProps } >
130- { mockProps . children }
131- </ SendbirdProvider > ,
132- ) ;
145+ await act ( async ( ) => (
146+ rerender (
147+ < SendbirdProvider { ...mockProps } >
148+ { mockProps . children }
149+ </ SendbirdProvider > ,
150+ )
151+ ) ) ;
133152 } ) ;
134153
135- it ( 'should provide all expected keys in context' , ( ) => {
154+ it ( 'should provide all expected keys in context' , async ( ) => {
136155 const expectedKeys = [
137156 'config' ,
138157 'stores' ,
139- 'dispatchers' ,
140158 'eventHandlers' ,
141159 'emojiManager' ,
142160 'utils' ,
@@ -159,19 +177,21 @@ describe('SendbirdProvider Props & Context Interface Validation', () => {
159177 ) ;
160178 } ;
161179
162- render (
163- < SendbirdProvider appId = "test-app-id" userId = "test-user-id" >
164- < TestComponent />
165- </ SendbirdProvider > ,
166- ) ;
180+ await act ( ( ) => (
181+ render (
182+ < SendbirdProvider appId = "test-app-id" userId = "test-user-id" >
183+ < TestComponent />
184+ </ SendbirdProvider > ,
185+ )
186+ ) ) ;
167187
168188 expectedKeys . forEach ( ( key ) => {
169189 const element = screen . getByTestId ( `context-${ key } ` ) ;
170190 expect ( element ) . toBeInTheDocument ( ) ;
171191 } ) ;
172192 } ) ;
173193
174- it ( 'should pass all expected values to the config object' , ( ) => {
194+ it ( 'should pass all expected values to the config object' , async ( ) => {
175195 const mockProps : SendbirdProviderProps = {
176196 appId : 'test-app-id' ,
177197 userId : 'test-user-id' ,
@@ -192,7 +212,10 @@ describe('SendbirdProvider Props & Context Interface Validation', () => {
192212 < SendbirdProvider { ...mockProps } > { children } </ SendbirdProvider >
193213 ) ;
194214
195- const { result } = renderHook ( ( ) => useSendbirdStateContext ( ) , { wrapper } ) ;
215+ let result ;
216+ await act ( async ( ) => {
217+ result = renderHook ( ( ) => useSendbirdStateContext ( ) , { wrapper } ) . result ;
218+ } ) ;
196219
197220 const config = result . current . config ;
198221
@@ -220,14 +243,17 @@ describe('SendbirdProvider Props & Context Interface Validation', () => {
220243 expect ( config . markAsDeliveredScheduler ) . toBeDefined ( ) ;
221244 } ) ;
222245
223- it ( 'should handle optional and default values correctly' , ( ) => {
246+ it ( 'should handle optional and default values correctly' , async ( ) => {
224247 const wrapper = ( { children } ) => (
225248 < SendbirdProvider { ...mockProps } appId = "test-app-id" userId = "test-user-id" >
226249 { children }
227250 </ SendbirdProvider >
228251 ) ;
229252
230- const { result } = renderHook ( ( ) => useSendbirdStateContext ( ) , { wrapper } ) ;
253+ let result ;
254+ await act ( async ( ) => {
255+ result = renderHook ( ( ) => useSendbirdStateContext ( ) , { wrapper } ) . result ;
256+ } ) ;
231257
232258 expect ( result . current . config . pubSub ) . toBeDefined ( ) ;
233259 expect ( result . current . config . logger ) . toBeDefined ( ) ;
0 commit comments