Skip to content

Commit 1328195

Browse files
Updating unreadcount only on message.new event
1 parent c237e6a commit 1328195

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/components/ChannelPreview/ChannelPreview.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const ChannelPreview = (props) => {
1414
useEffect(() => {
1515
const handleEvent = (e) => {
1616
setLastMessage(e.message);
17-
setUnread(channel.countUnread());
17+
18+
if (e.type === 'message.new') {
19+
setUnread(channel.countUnread());
20+
}
1821
};
1922

2023
channel.on('message.new', handleEvent);

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@ describe('ChannelPreview', () => {
9797
expect(getNodeText(getByTestId('unread-count'))).toBe('0');
9898
});
9999
});
100+
100101
const eventCases = [
101102
['message.new', dispatchMessageNewEvent],
102103
['message.updated', dispatchMessageUpdatedEvent],
103104
['message.deleted', dispatchMessageDeletedEvent],
104105
];
105106

106107
it.each(eventCases)(
107-
'should update the last event message & unreadCount on %s event',
108+
'should update the last event message',
108109
async (eventType, dispatcher) => {
109110
const c = generateChannel();
110111
await initializeChannel(c);
@@ -117,8 +118,6 @@ describe('ChannelPreview', () => {
117118
user: clientUser,
118119
});
119120

120-
channel.countUnread = () => 10;
121-
122121
act(() => {
123122
dispatcher(chatClient, message, channel);
124123
});
@@ -127,8 +126,30 @@ describe('ChannelPreview', () => {
127126
expect(getNodeText(getByTestId('last-event-message'))).toBe(
128127
message.text,
129128
);
130-
expect(getNodeText(getByTestId('unread-count'))).toBe('10');
131129
});
132130
},
133131
);
132+
133+
it('should update the unread count on "message.new" event', async () => {
134+
const c = generateChannel();
135+
await initializeChannel(c);
136+
137+
const { getByTestId } = render(getComponent());
138+
139+
await waitForElement(() => getByTestId('channel-id'));
140+
141+
const message = generateMessage({
142+
user: clientUser,
143+
});
144+
145+
channel.countUnread = () => 10;
146+
147+
act(() => {
148+
dispatchMessageNewEvent(chatClient, message, channel);
149+
});
150+
151+
await wait(() => {
152+
expect(getNodeText(getByTestId('unread-count'))).toBe('10');
153+
});
154+
});
134155
});

0 commit comments

Comments
 (0)