Skip to content

Commit ee639b7

Browse files
Merge pull request #255 from GetStream/vishal/channel-preview-messenger-tests
Handling message.updated and message.deleted event in ChannelPreview …
2 parents 5e4e3ec + 4a7b686 commit ee639b7

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

src/components/ChannelPreview/ChannelPreview.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@ import PropTypes from 'prop-types';
44
import { ChatContext } from '../../context';
55
import { useLatestMessagePreview } from './hooks/useLatestMessagePreview';
66

7-
/**
8-
* NOTE: We created an inner component which only receives `client` from the ChatContext. This
9-
* practice will prevent unnecessary renders of the component when items in ChatContext change
10-
*/
11-
const ChannelPreviewWithContext = React.memo((props) => {
12-
const { channel, client } = props;
7+
const ChannelPreview = (props) => {
8+
const { channel } = props;
9+
const { client } = useContext(ChatContext);
1310
const [lastMessage, setLastMessage] = useState({});
1411
const [unread, setUnread] = useState(channel.countUnread());
1512
const latestMessage = useLatestMessagePreview(channel, lastMessage);
1613

1714
useEffect(() => {
18-
const handleNewMessageEvent = (e) => {
15+
const handleEvent = (e) => {
1916
setLastMessage(e.message);
20-
setUnread(channel.countUnread());
17+
18+
if (e.type === 'message.new') {
19+
setUnread(channel.countUnread());
20+
}
2121
};
2222

23-
channel.on('message.new', handleNewMessageEvent);
23+
channel.on('message.new', handleEvent);
24+
channel.on('message.updated', handleEvent);
25+
channel.on('message.deleted', handleEvent);
2426

2527
return () => {
26-
channel.off('message.new', handleNewMessageEvent);
28+
channel.off('message.new', handleEvent);
29+
channel.off('message.updated', handleEvent);
30+
channel.off('message.deleted', handleEvent);
2731
};
2832
}, []);
2933

@@ -42,12 +46,14 @@ const ChannelPreviewWithContext = React.memo((props) => {
4246
}, []);
4347

4448
const { Preview } = props;
45-
return <Preview {...props} latestMessage={latestMessage} unread={unread} />;
46-
});
47-
48-
const ChannelPreview = (props) => {
49-
const { client } = useContext(ChatContext);
50-
return <ChannelPreviewWithContext {...props} {...{ client }} />;
49+
return (
50+
<Preview
51+
{...props}
52+
lastMessage={lastMessage}
53+
latestMessage={latestMessage}
54+
unread={unread}
55+
/>
56+
);
5157
};
5258

5359
ChannelPreview.propTypes = {
@@ -57,4 +63,4 @@ ChannelPreview.propTypes = {
5763
Preview: PropTypes.oneOfType([PropTypes.node, PropTypes.elementType]),
5864
};
5965

60-
export default ChannelPreview;
66+
export default React.memo(ChannelPreview);

src/components/ChannelPreview/ChannelPreviewMessenger.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ const ChannelPreviewMessenger = ({
7171
}) => {
7272
const displayAvatar = useChannelPreviewDisplayAvatar(channel);
7373
const displayName = useChannelPreviewDisplayName(channel);
74-
7574
return (
7675
<Container
7776
onPress={setActiveChannel.bind(null, channel)}

src/components/ChannelPreview/__tests__/ChannelPreview.test.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
} from '@testing-library/react-native';
99

1010
import {
11+
dispatchMessageDeletedEvent,
12+
dispatchMessageNewEvent,
13+
dispatchMessageReadEvent,
14+
dispatchMessageUpdatedEvent,
1115
generateChannel,
1216
generateMessage,
1317
generateUser,
@@ -19,10 +23,6 @@ import {
1923
import ChannelPreview from '../ChannelPreview';
2024
import { Chat } from '../../Chat';
2125
import { Text } from 'react-native';
22-
import {
23-
dispatchMessageReadEvent,
24-
dispatchMessageNewEvent,
25-
} from 'mock-builders';
2626

2727
const ChannelPreviewUIComponent = (props) => (
2828
<>
@@ -98,7 +98,39 @@ describe('ChannelPreview', () => {
9898
});
9999
});
100100

101-
it('should update the last event message & unreadCount, when message.new event is received', async () => {
101+
const eventCases = [
102+
['message.new', dispatchMessageNewEvent],
103+
['message.updated', dispatchMessageUpdatedEvent],
104+
['message.deleted', dispatchMessageDeletedEvent],
105+
];
106+
107+
it.each(eventCases)(
108+
'should update the last event message',
109+
async (eventType, dispatcher) => {
110+
const c = generateChannel();
111+
await initializeChannel(c);
112+
113+
const { getByTestId } = render(getComponent());
114+
115+
await waitForElement(() => getByTestId('channel-id'));
116+
117+
const message = generateMessage({
118+
user: clientUser,
119+
});
120+
121+
act(() => {
122+
dispatcher(chatClient, message, channel);
123+
});
124+
125+
await wait(() => {
126+
expect(getNodeText(getByTestId('last-event-message'))).toBe(
127+
message.text,
128+
);
129+
});
130+
},
131+
);
132+
133+
it('should update the unread count on "message.new" event', async () => {
102134
const c = generateChannel();
103135
await initializeChannel(c);
104136

@@ -117,7 +149,6 @@ describe('ChannelPreview', () => {
117149
});
118150

119151
await wait(() => {
120-
expect(getNodeText(getByTestId('last-event-message'))).toBe(message.text);
121152
expect(getNodeText(getByTestId('unread-count'))).toBe('10');
122153
});
123154
});

src/components/ChannelPreview/hooks/useLatestMessagePreview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const useLatestMessagePreview = (channel, lastMessage) => {
5555
text: getLatestMessageDisplayText(message, t),
5656
});
5757
}
58-
}, [lastMessage]);
58+
}, [channel, lastMessage]);
5959

6060
return latestMessagePreview;
6161
};

0 commit comments

Comments
 (0)