Skip to content

Commit

Permalink
fix: evaluate channel.lastRead when channel is not initialized (#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela authored Oct 6, 2023
1 parent b034b58 commit 13fa28a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,6 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
* @return {Date | null | undefined}
*/
lastRead() {
this._checkInitialized();
const { userID } = this.getClient();
if (userID) {
return this.state.read[userID] ? this.state.read[userID].last_read : null;
Expand Down
84 changes: 74 additions & 10 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ describe('Channel count unread', function () {
beforeEach(() => {
user = { id: 'user' };
lastRead = new Date('2020-01-01T00:00:00');
const channelResponse = generateChannel();

client = new StreamChat('apiKey');
client.user = user;
client.userID = 'user';
client.userMuteStatus = (targetId) => targetId.startsWith('mute');

const channelResponse = generateChannel();
channel = client.channel(channelResponse.channel.type, channelResponse.channel.id);
channel.initialized = true;
channel.lastRead = () => lastRead;
Expand Down Expand Up @@ -157,6 +157,39 @@ describe('Channel count unread', function () {

expect(channel.countUnreadMentions()).to.be.equal(1);
});

describe('channel.lastRead', () => {
let channelResponse;
beforeEach(() => {
channelResponse = generateChannel();
channel = client.channel(channelResponse.channel.type, channelResponse.channel.id);
channel.initialized = true;
});

it('should return null if no last read message', () => {
expect(channel.lastRead()).to.eq(null);
});

it('should return last read message date', () => {
const last_read = new Date();
const messages = [generateMsg()];
channel.state.read[user.id] = {
last_read,
last_read_message_id: messages[0].id,
user: user,
unread_messages: 0,
};
channel.state.addMessagesSorted(messages);
expect(channel.lastRead()).to.eq(last_read);
});

it('should return undefined if client user is not set (server-side client)', () => {
client = new StreamChat('apiKey', 'secret');
channel = client.channel(channelResponse.channel.type, channelResponse.channel.id);
channel.initialized = true;
expect(channel.lastRead()).to.be.undefined;
});
});
});

describe('Channel _handleChannelEvent', function () {
Expand Down Expand Up @@ -497,6 +530,21 @@ describe('Channel _handleChannelEvent', function () {
expect(channel.state.members[user.id].shadow_banned).eq(expectAfterSecond.shadow_banned);
});
});
});

describe('Channel WS events buffer', () => {
const user = { id: 'user' };
let client;
let channel;

beforeEach(() => {
client = new StreamChat('apiKey');
client.user = user;
client.userID = user.id;
client.userMuteStatus = (targetId) => targetId.startsWith('mute');
channel = client.channel('messaging', 'id');
channel.initialized = false;
});

const eventTypes = Object.keys(CHANNEL_HANDLED_EVENTS);
const receiveAllChannelEvents = (channel) => {
Expand All @@ -505,17 +553,13 @@ describe('Channel _handleChannelEvent', function () {
});
};

it('buffers WS events when uninitialized', () => {
channel.initialized = false;
channel.offlineMode = false;

it('when uninitialized', () => {
receiveAllChannelEvents(channel);

expect(channel.wsEventQueue).to.have.length(eventTypes.length);
});

it('does not buffer WS events when in offline mode', () => {
channel.initialized = false;
channel.offlineMode = true;

receiveAllChannelEvents(channel);
Expand All @@ -528,8 +572,6 @@ describe('Channel _handleChannelEvent', function () {
client.user = user;
client.userID = user.id;
channel = client.channel('messaging', 'id');
channel.initialized = false;
channel.offlineMode = false;

receiveAllChannelEvents(channel);

Expand All @@ -545,7 +587,6 @@ describe('Channel _handleChannelEvent', function () {
});

it('buffers WS events and channel.watch() flushes upon channel initialization', async () => {
channel.initialized = false;
sinon.stub(client, 'doAxiosRequest').resolves({ channel: generateChannel(), members: [] });

receiveAllChannelEvents(channel);
Expand All @@ -557,7 +598,6 @@ describe('Channel _handleChannelEvent', function () {
});

it('buffers WS events and channel.query() does not flush the queue', async () => {
channel.initialized = false;
sinon.stub(client, 'doAxiosRequest').resolves({ channel: generateChannel(), members: [] });

receiveAllChannelEvents(channel);
Expand All @@ -569,6 +609,30 @@ describe('Channel _handleChannelEvent', function () {
});
});

describe('Uninitialized Channel', () => {
const user = { id: 'user' };
let client;
let channel;

beforeEach(() => {
client = new StreamChat('apiKey');
client.user = user;
client.userID = user.id;
client.userMuteStatus = (targetId) => targetId.startsWith('mute');
channel = client.channel('messaging', 'id');
channel.initialized = false;
channel.offlineMode = false;
});

it('returns 0 mentions in unread messages', () => {
expect(channel.countUnreadMentions()).to.eq(0);
});

it('reports no lastRead data', () => {
expect(channel.lastRead()).to.eq(null);
});
});

describe('Channels - Constructor', function () {
const client = new StreamChat('key', 'secret');

Expand Down
2 changes: 1 addition & 1 deletion test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('test if sort is deterministic', () => {
});
});

describe.only('axiosParamsSerializer', () => {
describe('axiosParamsSerializer', () => {
const testCases = [
{
input: {
Expand Down

0 comments on commit 13fa28a

Please sign in to comment.