|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Text } from 'react-native'; |
| 3 | + |
| 4 | +import { act } from 'react-test-renderer'; |
| 5 | + |
| 6 | +import { render, waitFor } from '@testing-library/react-native'; |
| 7 | +import type { Channel, ChannelResponse, Event, StreamChat } from 'stream-chat'; |
| 8 | + |
| 9 | +import { ChatContext, useChannelUpdated } from '../../../../../index'; |
| 10 | + |
| 11 | +describe('useChannelUpdated', () => { |
| 12 | + it("defaults to the channels own_capabilities if the event doesn't include it", async () => { |
| 13 | + let eventHanler: (event: Event) => void; |
| 14 | + const mockChannel = { |
| 15 | + cid: 'channeltype:123abc', |
| 16 | + data: { |
| 17 | + own_capabilities: { |
| 18 | + send_messages: true, |
| 19 | + }, |
| 20 | + }, |
| 21 | + } as unknown as Channel; |
| 22 | + |
| 23 | + const mockEvent = { |
| 24 | + channel: { |
| 25 | + cid: mockChannel.cid, |
| 26 | + } as ChannelResponse, |
| 27 | + type: 'channel.updated' as Event['type'], |
| 28 | + }; |
| 29 | + |
| 30 | + const mockClient = { |
| 31 | + off: jest.fn(), |
| 32 | + on: jest.fn().mockImplementation((_eventName: string, handler: (event: Event) => void) => { |
| 33 | + eventHanler = handler; |
| 34 | + }), |
| 35 | + } as unknown as StreamChat; |
| 36 | + |
| 37 | + const TestComponent = () => { |
| 38 | + const [channels, setChannels] = useState<Channel[]>([mockChannel]); |
| 39 | + |
| 40 | + useChannelUpdated({ setChannels }); |
| 41 | + |
| 42 | + if ( |
| 43 | + channels[0].data?.own_capabilities && |
| 44 | + Object.keys(channels[0].data?.own_capabilities as { [key: string]: boolean }).includes( |
| 45 | + 'send_messages', |
| 46 | + ) |
| 47 | + ) { |
| 48 | + return <Text>Send messages enabled</Text>; |
| 49 | + } |
| 50 | + |
| 51 | + return <Text>Send messages NOT enabled</Text>; |
| 52 | + }; |
| 53 | + |
| 54 | + const { getByText } = await waitFor(() => |
| 55 | + render( |
| 56 | + <ChatContext.Provider |
| 57 | + value={{ |
| 58 | + client: mockClient, |
| 59 | + connectionRecovering: false, |
| 60 | + isOnline: true, |
| 61 | + mutedUsers: [], |
| 62 | + setActiveChannel: () => null, |
| 63 | + }} |
| 64 | + > |
| 65 | + <TestComponent /> |
| 66 | + </ChatContext.Provider>, |
| 67 | + ), |
| 68 | + ); |
| 69 | + |
| 70 | + await waitFor(() => { |
| 71 | + expect(getByText('Send messages enabled')).toBeTruthy(); |
| 72 | + }); |
| 73 | + |
| 74 | + act(() => { |
| 75 | + eventHanler(mockEvent); |
| 76 | + }); |
| 77 | + |
| 78 | + await waitFor(() => { |
| 79 | + expect(getByText('Send messages enabled')).toBeTruthy(); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments