-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add userMuteStatus * refactor generateMsg to test utils * skip muted users in unreadCounts * message.new updates unreadCount correctly * reversing the unread count condition
- Loading branch information
Amin Mahboubi
authored
Nov 17, 2020
1 parent
3527d20
commit 5f44dd1
Showing
6 changed files
with
307 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import chai from 'chai'; | ||
import { Channel } from '../../src/channel'; | ||
import { generateMsg } from './utils'; | ||
|
||
const expect = chai.expect; | ||
|
||
describe('Channel count unread', function () { | ||
const user = { id: 'user' }; | ||
const lastRead = new Date('2020-01-01T00:00:00'); | ||
|
||
const channel = new Channel({}, 'messaging', 'id'); | ||
channel.lastRead = () => lastRead; | ||
channel.getClient = () => ({ | ||
user, | ||
userID: 'user', | ||
userMuteStatus: (targetId) => targetId.startsWith('mute'), | ||
}); | ||
|
||
const ignoredMessages = [ | ||
generateMsg({ date: '2018-01-01T00:00:00', mentioned_users: [user] }), | ||
generateMsg({ date: '2019-01-01T00:00:00' }), | ||
generateMsg({ date: '2020-01-01T00:00:00' }), | ||
generateMsg({ | ||
date: '2023-01-01T00:00:00', | ||
shadowed: true, | ||
mentioned_users: [user], | ||
}), | ||
generateMsg({ | ||
date: '2024-01-01T00:00:00', | ||
silent: true, | ||
mentioned_users: [user], | ||
}), | ||
generateMsg({ | ||
date: '2025-01-01T00:00:00', | ||
user: { id: 'mute1' }, | ||
mentioned_users: [user], | ||
}), | ||
]; | ||
channel.state.addMessagesSorted(ignoredMessages); | ||
|
||
it('_countMessageAsUnread should return false shadowed or silent messages', function () { | ||
expect(channel._countMessageAsUnread({ shadowed: true })).not.to.be.ok; | ||
expect(channel._countMessageAsUnread({ silent: true })).not.to.be.ok; | ||
}); | ||
|
||
it('_countMessageAsUnread should return false for current user messages', function () { | ||
expect(channel._countMessageAsUnread({ user })).not.to.be.ok; | ||
}); | ||
|
||
it('_countMessageAsUnread should return false for muted user', function () { | ||
expect(channel._countMessageAsUnread({ user: { id: 'mute1' } })).not.to.be.ok; | ||
}); | ||
|
||
it('_countMessageAsUnread should return true for unmuted user', function () { | ||
expect(channel._countMessageAsUnread({ user: { id: 'unmute' } })).to.be.ok; | ||
}); | ||
|
||
it('_countMessageAsUnread should return true for other messages', function () { | ||
expect( | ||
channel._countMessageAsUnread({ | ||
shadowed: false, | ||
silent: false, | ||
user: { id: 'random' }, | ||
}), | ||
).to.be.ok; | ||
}); | ||
|
||
it('countUnread should return state.unreadCount without lastRead', function () { | ||
expect(channel.countUnread()).to.be.equal(channel.state.unreadCount); | ||
channel.state.unreadCount = 10; | ||
expect(channel.countUnread()).to.be.equal(10); | ||
channel.state.unreadCount = 0; | ||
}); | ||
|
||
it('countUnread should return correct count', function () { | ||
expect(channel.countUnread(lastRead)).to.be.equal(0); | ||
channel.state.addMessagesSorted([ | ||
generateMsg({ date: '2021-01-01T00:00:00' }), | ||
generateMsg({ date: '2022-01-01T00:00:00' }), | ||
]); | ||
expect(channel.countUnread(lastRead)).to.be.equal(2); | ||
}); | ||
|
||
it('countUnreadMentions should return correct count', function () { | ||
expect(channel.countUnreadMentions()).to.be.equal(0); | ||
channel.state.addMessageSorted( | ||
generateMsg({ | ||
date: '2021-01-01T00:00:00', | ||
mentioned_users: [user, { id: 'random' }], | ||
}), | ||
generateMsg({ | ||
date: '2022-01-01T00:00:00', | ||
mentioned_users: [{ id: 'random' }], | ||
}), | ||
); | ||
expect(channel.countUnreadMentions()).to.be.equal(1); | ||
}); | ||
}); | ||
|
||
describe('Channel _handleChannelEvent', function () { | ||
const user = { id: 'user' }; | ||
const channel = new Channel( | ||
{ | ||
logger: () => null, | ||
user, | ||
userID: user.id, | ||
userMuteStatus: (targetId) => targetId.startsWith('mute'), | ||
}, | ||
'messaging', | ||
'id', | ||
); | ||
|
||
it('message.new reset the unreadCount for current user messages', function () { | ||
channel.state.unreadCount = 100; | ||
channel._handleChannelEvent({ | ||
type: 'message.new', | ||
user, | ||
message: generateMsg(), | ||
}); | ||
|
||
expect(channel.state.unreadCount).to.be.equal(0); | ||
}); | ||
|
||
it('message.new increment unreadCount properly', function () { | ||
channel.state.unreadCount = 20; | ||
channel._handleChannelEvent({ | ||
type: 'message.new', | ||
user: { id: 'id' }, | ||
message: generateMsg({ user: { id: 'id' } }), | ||
}); | ||
expect(channel.state.unreadCount).to.be.equal(21); | ||
channel._handleChannelEvent({ | ||
type: 'message.new', | ||
user: { id: 'id2' }, | ||
message: generateMsg({ user: { id: 'id2' } }), | ||
}); | ||
expect(channel.state.unreadCount).to.be.equal(22); | ||
}); | ||
|
||
it('message.new skip increment for silent/shadowed/muted messages', function () { | ||
channel.state.unreadCount = 30; | ||
channel._handleChannelEvent({ | ||
type: 'message.new', | ||
user: { id: 'id' }, | ||
message: generateMsg({ silent: true }), | ||
}); | ||
expect(channel.state.unreadCount).to.be.equal(30); | ||
channel._handleChannelEvent({ | ||
type: 'message.new', | ||
user: { id: 'id2' }, | ||
message: generateMsg({ shadowed: true }), | ||
}); | ||
expect(channel.state.unreadCount).to.be.equal(30); | ||
channel._handleChannelEvent({ | ||
type: 'message.new', | ||
user: { id: 'mute1' }, | ||
message: generateMsg({ user: { id: 'mute1' } }), | ||
}); | ||
expect(channel.state.unreadCount).to.be.equal(30); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.