Skip to content

Commit 0a27963

Browse files
Merge pull request #1195 from GetStream/vishal/own-capabilities-fix
fix: add missing own_capabilities field after channel.updated event [CRNS-537]
2 parents f41e655 + fbb6771 commit 0a27963

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
});

package/src/components/ChannelList/hooks/listeners/useChannelUpdated.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ export const useChannelUpdated = <
3333
(channel) => channel.cid === (event.cid || event.channel?.cid),
3434
);
3535
if (index >= 0 && event.channel) {
36-
channels[index].data = event.channel;
36+
channels[index].data = {
37+
...event.channel,
38+
hidden: event.channel?.hidden ?? channels[index].data?.hidden,
39+
own_capabilities:
40+
event.channel?.own_capabilities ?? channels[index].data?.own_capabilities,
41+
};
3742
}
43+
3844
return [...channels];
3945
});
4046
}

0 commit comments

Comments
 (0)