Skip to content

Commit 1753842

Browse files
committed
tests: add test for useChannelUpdated
1 parent 5c4e9ef commit 1753842

File tree

1 file changed

+77
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)