From 8d2e3ca10929ef7389b157bb4a6ada71c7074e6b Mon Sep 17 00:00:00 2001 From: MartinCupela <32706194+MartinCupela@users.noreply.github.com> Date: Fri, 15 Apr 2022 16:19:57 +0200 Subject: [PATCH] fix: mark all active channels as read only if notification.mark_read event's unread_channels is 0 (#955) --- src/client.ts | 2 +- test/unit/client.js | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/client.ts b/src/client.ts index 6a14931cc..a077a0819 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1195,7 +1195,7 @@ export class StreamChat (this.activeChannels[activeChannelKey].state.unreadCount = 0)); } diff --git a/test/unit/client.js b/test/unit/client.js index 5f80ddc20..ce5ec445b 100644 --- a/test/unit/client.js +++ b/test/unit/client.js @@ -127,19 +127,39 @@ describe('Client userMuteStatus', function () { expect(client.userMuteStatus('mute4')).not.to.be.ok; expect(client.userMuteStatus('missingUser')).not.to.be.ok; }); +}); - it('should update all active channel unread count to 0 when notification.mark_read event is called', function () { +describe('Client active channels cache', () => { + const client = new StreamChat('', ''); + const user = { id: 'user' }; + + client.connectUser = async () => { + client.user = user; + client.wsPromise = Promise.resolve(); + }; + beforeEach(() => { client.activeChannels = { vish: { state: { unreadCount: 1 } }, vish2: { state: { unreadCount: 2 } } }; + }); + + const countUnreadChannels = (channels) => + Object.values(channels).reduce((prevSum, currSum) => prevSum + currSum.state.unreadCount, 0); + + it('should mark all active channels as read on notification.mark_read event if event.unread_channels is 0', function () { client.dispatchEvent({ type: 'notification.mark_read', + unread_channels: 0, }); - const unreadCountSum = Object.values(client.activeChannels).reduce( - (prevSum, currSum) => prevSum + currSum.state.unreadCount, - 0, - ); + expect(countUnreadChannels(client.activeChannels)).to.be.equal(0); + }); + + it('should not mark any active channel as read on notification.mark_read event if event.unread_channels > 0', function () { + client.dispatchEvent({ + type: 'notification.mark_read', + unread_channels: 1, + }); - expect(unreadCountSum).to.be.equal(0); + expect(countUnreadChannels(client.activeChannels)).to.be.equal(3); }); });