|
1 | 1 | import React from 'react'; |
2 | | -import { Text, View } from 'react-native'; |
| 2 | +import { Text } from 'react-native'; |
3 | 3 |
|
4 | 4 | import { act, render, waitFor } from '@testing-library/react-native'; |
5 | 5 |
|
6 | 6 | import type { Channel, StreamChat } from 'stream-chat'; |
7 | 7 |
|
| 8 | +import { ChannelsProvider } from '../../../contexts/channelsContext/ChannelsContext'; |
| 9 | +import type { ChannelsContextValue } from '../../../contexts/channelsContext/ChannelsContext'; |
8 | 10 | import { |
9 | 11 | getOrCreateChannelApi, |
10 | 12 | GetOrCreateChannelApiParams, |
@@ -33,6 +35,13 @@ type ChannelPreviewUIComponentProps = { |
33 | 35 | }; |
34 | 36 |
|
35 | 37 | const channelOnMock = jest.fn().mockReturnValue({ unsubscribe: jest.fn() }); |
| 38 | +const mockChannelSwipableWrapper = jest.fn(({ children }: React.PropsWithChildren) => ( |
| 39 | + <Text testID='swipe-wrapper'>{children}</Text> |
| 40 | +)); |
| 41 | + |
| 42 | +jest.mock('../ChannelSwipableWrapper', () => ({ |
| 43 | + ChannelSwipableWrapper: (...args: unknown[]) => mockChannelSwipableWrapper(...args), |
| 44 | +})); |
36 | 45 |
|
37 | 46 | const ChannelPreviewUIComponent = (props: ChannelPreviewUIComponentProps) => { |
38 | 47 | return ( |
@@ -404,118 +413,76 @@ describe('ChannelPreview', () => { |
404 | 413 | }); |
405 | 414 |
|
406 | 415 | describe('swipeActionsEnabled', () => { |
407 | | - const swipeChannel = { cid: 'messaging:test-channel', id: 'test-channel' } as Channel; |
408 | | - const swipeClient = { userID: 'test-user-id' }; |
409 | | - const swipeLastMessage = { text: 'hello' } as LastMessageType; |
410 | | - |
411 | | - beforeEach(() => { |
412 | | - jest.resetModules(); |
413 | | - }); |
| 416 | + const ChannelDetailsBottomSheetOverride = () => null; |
| 417 | + |
| 418 | + const SwipePreview = ({ lastMessage, muted, unread }: ChannelPreviewUIComponentProps) => ( |
| 419 | + <> |
| 420 | + <Text testID='preview-muted'>{`${muted}`}</Text> |
| 421 | + <Text testID='preview-unread'>{`${unread}`}</Text> |
| 422 | + <Text testID='preview-last-message'>{lastMessage?.text ?? ''}</Text> |
| 423 | + </> |
| 424 | + ); |
414 | 425 |
|
415 | | - const renderWithSwipeMocks = ({ |
416 | | - ChannelDetailsBottomSheet, |
| 426 | + const SwipeTestComponent = ({ |
| 427 | + channelDetailsBottomSheet, |
417 | 428 | swipeActionsEnabled, |
418 | 429 | }: { |
419 | | - ChannelDetailsBottomSheet?: React.ComponentType; |
| 430 | + channelDetailsBottomSheet?: React.ComponentType; |
420 | 431 | swipeActionsEnabled: boolean; |
421 | 432 | }) => { |
422 | | - let rendered: ReturnType<typeof import('@testing-library/react-native').render> | undefined; |
423 | | - const mockChannelSwipableWrapper = jest.fn(({ children }: React.PropsWithChildren) => ( |
424 | | - <View testID='swipe-wrapper'>{children}</View> |
425 | | - )); |
426 | | - |
427 | | - const PreviewComponent = ({ |
428 | | - lastMessage, |
429 | | - muted, |
430 | | - unread, |
431 | | - }: { |
432 | | - lastMessage: LastMessageType; |
433 | | - muted: boolean; |
434 | | - unread: number; |
435 | | - }) => ( |
436 | | - <> |
437 | | - <Text testID='preview-muted'>{`${muted}`}</Text> |
438 | | - <Text testID='preview-unread'>{`${unread}`}</Text> |
439 | | - <Text testID='preview-last-message'>{lastMessage?.text ?? ''}</Text> |
440 | | - </> |
441 | | - ); |
442 | | - |
443 | | - jest.isolateModules(() => { |
444 | | - jest.doMock('../../../contexts/channelsContext/ChannelsContext', () => ({ |
445 | | - useChannelsContext: () => ({ |
446 | | - ChannelDetailsBottomSheet, |
447 | | - Preview: PreviewComponent, |
448 | | - getChannelActionItems: undefined, |
449 | | - swipeActionsEnabled, |
450 | | - }), |
451 | | - })); |
452 | | - |
453 | | - jest.doMock('../../../contexts/chatContext/ChatContext', () => ({ |
454 | | - useChatContext: () => ({ client: swipeClient }), |
455 | | - })); |
456 | | - |
457 | | - jest.doMock('../hooks/useChannelPreviewData', () => ({ |
458 | | - useChannelPreviewData: () => ({ |
459 | | - lastMessage: swipeLastMessage, |
460 | | - muted: false, |
461 | | - unread: 3, |
462 | | - }), |
463 | | - })); |
464 | | - |
465 | | - jest.doMock('../../../hooks/useTranslatedMessage', () => ({ |
466 | | - useTranslatedMessage: () => undefined, |
467 | | - })); |
468 | | - |
469 | | - jest.doMock('../ChannelSwipableWrapper', () => ({ |
470 | | - ChannelSwipableWrapper: (...args: unknown[]) => mockChannelSwipableWrapper(...args), |
471 | | - })); |
472 | | - |
473 | | - const { render } = |
474 | | - require('@testing-library/react-native') as typeof import('@testing-library/react-native'); |
475 | | - const { ChannelPreview: IsolatedChannelPreview } = |
476 | | - require('../ChannelPreview') as typeof import('../ChannelPreview'); |
477 | | - |
478 | | - rendered = render(<IsolatedChannelPreview channel={swipeChannel} />); |
479 | | - }); |
480 | | - |
481 | | - if (!rendered) { |
482 | | - throw new Error('Failed to render ChannelPreview with mocked modules'); |
| 433 | + if (channel === null) { |
| 434 | + return null; |
483 | 435 | } |
484 | 436 |
|
485 | | - return { ...rendered, mockChannelSwipableWrapper }; |
| 437 | + return ( |
| 438 | + <Chat client={chatClient}> |
| 439 | + <ChannelsProvider |
| 440 | + value={ |
| 441 | + { |
| 442 | + ChannelDetailsBottomSheet: channelDetailsBottomSheet, |
| 443 | + Preview: SwipePreview, |
| 444 | + getChannelActionItems: undefined, |
| 445 | + swipeActionsEnabled, |
| 446 | + } as unknown as ChannelsContextValue |
| 447 | + } |
| 448 | + > |
| 449 | + <ChannelPreview channel={channel} client={chatClient} Preview={SwipePreview} /> |
| 450 | + </ChannelsProvider> |
| 451 | + </Chat> |
| 452 | + ); |
486 | 453 | }; |
487 | 454 |
|
488 | | - it('does not render ChannelSwipableWrapper when swipeActionsEnabled is false', () => { |
489 | | - const { getByTestId, queryByTestId, mockChannelSwipableWrapper } = renderWithSwipeMocks({ |
490 | | - swipeActionsEnabled: false, |
491 | | - }); |
| 455 | + beforeEach(async () => { |
| 456 | + mockChannelSwipableWrapper.mockClear(); |
| 457 | + channel = await initChannelFromData(chatClient); |
| 458 | + }); |
492 | 459 |
|
493 | | - expect(getByTestId('preview-muted')).toHaveTextContent('false'); |
494 | | - expect(getByTestId('preview-unread')).toHaveTextContent('3'); |
495 | | - expect(getByTestId('preview-last-message')).toHaveTextContent('hello'); |
| 460 | + it('does not render ChannelSwipableWrapper when swipeActionsEnabled is false', async () => { |
| 461 | + const { getByTestId, queryByTestId } = render( |
| 462 | + <SwipeTestComponent swipeActionsEnabled={false} />, |
| 463 | + ); |
| 464 | + |
| 465 | + await waitFor(() => expect(getByTestId('preview-unread')).toHaveTextContent('0')); |
496 | 466 | expect(queryByTestId('swipe-wrapper')).toBeNull(); |
497 | 467 | expect(mockChannelSwipableWrapper).not.toHaveBeenCalled(); |
498 | 468 | }); |
499 | 469 |
|
500 | | - it('renders ChannelSwipableWrapper when swipeActionsEnabled is true', () => { |
501 | | - const { getByTestId, mockChannelSwipableWrapper } = renderWithSwipeMocks({ |
502 | | - swipeActionsEnabled: true, |
503 | | - }); |
| 470 | + it('renders ChannelSwipableWrapper when swipeActionsEnabled is true', async () => { |
| 471 | + const { getByTestId } = render(<SwipeTestComponent swipeActionsEnabled={true} />); |
504 | 472 |
|
505 | | - expect(getByTestId('swipe-wrapper')).toBeTruthy(); |
506 | | - expect(mockChannelSwipableWrapper).toHaveBeenCalledTimes(1); |
507 | | - expect(getByTestId('preview-unread')).toHaveTextContent('3'); |
| 473 | + await waitFor(() => expect(getByTestId('swipe-wrapper')).toBeTruthy()); |
| 474 | + expect(mockChannelSwipableWrapper).toHaveBeenCalled(); |
508 | 475 | }); |
509 | 476 |
|
510 | | - it('passes ChannelDetailsBottomSheet override to ChannelSwipableWrapper', () => { |
511 | | - const ChannelDetailsBottomSheetOverride = () => null; |
512 | | - |
513 | | - const { mockChannelSwipableWrapper } = renderWithSwipeMocks({ |
514 | | - ChannelDetailsBottomSheet: ChannelDetailsBottomSheetOverride, |
515 | | - swipeActionsEnabled: true, |
516 | | - }); |
| 477 | + it('passes ChannelDetailsBottomSheet override to ChannelSwipableWrapper', async () => { |
| 478 | + render( |
| 479 | + <SwipeTestComponent |
| 480 | + swipeActionsEnabled={true} |
| 481 | + channelDetailsBottomSheet={ChannelDetailsBottomSheetOverride} |
| 482 | + />, |
| 483 | + ); |
517 | 484 |
|
518 | | - expect(mockChannelSwipableWrapper).toHaveBeenCalled(); |
| 485 | + await waitFor(() => expect(mockChannelSwipableWrapper).toHaveBeenCalled()); |
519 | 486 | const swipableWrapperProps = mockChannelSwipableWrapper.mock.calls[0]?.[0]; |
520 | 487 | expect(swipableWrapperProps).toEqual( |
521 | 488 | expect.objectContaining({ |
|
0 commit comments