|
| 1 | +import React from 'react'; |
| 2 | + |
1 | 3 | import { act, renderHook, waitFor } from '@testing-library/react-native'; |
2 | 4 | import type { Channel, ChannelMemberResponse } from 'stream-chat'; |
3 | 5 |
|
| 6 | +import { TranslationProvider } from '../../../../contexts/translationContext/TranslationContext'; |
4 | 7 | import { generateMember } from '../../../../mock-builders/generator/member'; |
5 | 8 | import { generateUser } from '../../../../mock-builders/generator/user'; |
| 9 | +import { useNotificationApi } from '../../../Notifications/hooks/useNotificationApi'; |
6 | 10 | import { useChannelAllMembers } from '../../hooks/members/useChannelAllMembers'; |
7 | 11 |
|
| 12 | +jest.mock('../../../Notifications/hooks/useNotificationApi', () => ({ |
| 13 | + useNotificationApi: jest.fn(() => ({ addNotification: jest.fn() })), |
| 14 | +})); |
| 15 | + |
| 16 | +const t = ((key: string) => key) as never; |
| 17 | + |
| 18 | +const translationWrapper = ({ children }: { children: React.ReactNode }) => ( |
| 19 | + <TranslationProvider |
| 20 | + value={{ t, tDateTimeParser: ((input: unknown) => input) as never, userLanguage: 'en' }} |
| 21 | + > |
| 22 | + {children} |
| 23 | + </TranslationProvider> |
| 24 | +); |
| 25 | + |
8 | 26 | type QueryMembersMock = jest.Mock< |
9 | 27 | Promise<{ members: ChannelMemberResponse[] }>, |
10 | 28 | [unknown, unknown, unknown] |
@@ -37,14 +55,11 @@ const buildMembers = (count: number, prefix = 'u') => |
37 | 55 | ); |
38 | 56 |
|
39 | 57 | describe('useChannelAllMembers', () => { |
40 | | - let warnSpy: jest.SpyInstance; |
| 58 | + let addNotification: jest.Mock; |
41 | 59 |
|
42 | 60 | beforeEach(() => { |
43 | | - warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined); |
44 | | - }); |
45 | | - |
46 | | - afterEach(() => { |
47 | | - warnSpy.mockRestore(); |
| 61 | + addNotification = jest.fn(); |
| 62 | + (useNotificationApi as jest.Mock).mockReturnValue({ addNotification }); |
48 | 63 | }); |
49 | 64 |
|
50 | 65 | describe('local mode', () => { |
@@ -192,21 +207,27 @@ describe('useChannelAllMembers', () => { |
192 | 207 | await waitFor(() => expect(result.current.loading).toBe(false)); |
193 | 208 | }); |
194 | 209 |
|
195 | | - it('recovers from a queryMembers rejection', async () => { |
| 210 | + it('recovers from a queryMembers rejection and notifies the user', async () => { |
196 | 211 | const queryMembers: QueryMembersMock = jest.fn().mockRejectedValue(new Error('boom')); |
197 | 212 | const channel = buildChannel({ |
198 | 213 | memberCount: 200, |
199 | 214 | members: buildMembers(25, 'loaded'), |
200 | 215 | queryMembers, |
201 | 216 | }); |
202 | 217 |
|
203 | | - const { result } = renderHook(() => useChannelAllMembers({ channel })); |
| 218 | + const { result } = renderHook(() => useChannelAllMembers({ channel }), { |
| 219 | + wrapper: translationWrapper, |
| 220 | + }); |
204 | 221 |
|
205 | 222 | await waitFor(() => expect(result.current.loading).toBe(false)); |
206 | 223 | expect(result.current.results).toEqual([]); |
207 | | - expect(warnSpy).toHaveBeenCalledWith( |
208 | | - '[useChannelAllMembers] queryMembers failed', |
209 | | - expect.any(Error), |
| 224 | + expect(addNotification).toHaveBeenCalledWith( |
| 225 | + expect.objectContaining({ |
| 226 | + options: expect.objectContaining({ |
| 227 | + severity: 'error', |
| 228 | + type: 'api:channel:query-members:failed', |
| 229 | + }), |
| 230 | + }), |
210 | 231 | ); |
211 | 232 | }); |
212 | 233 | }); |
|
0 commit comments