MM-59276 Don't update channel stats state unnecessarily (#27805)

This commit is contained in:
Harrison Healey 2024-08-02 11:16:58 -04:00 committed by GitHub
parent bb78de5b7f
commit 3f4b8e8137
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 97 additions and 4 deletions

View File

@ -114,6 +114,94 @@ describe('channels', () => {
});
});
describe('RECEIVED_CHANNEL_STATS', () => {
test("should store a channel's stats", () => {
const state = deepFreeze(channelsReducer({}, {}));
const nextState = channelsReducer(state, {
type: ChannelTypes.RECEIVED_CHANNEL_STATS,
data: {
channel_id: 'channel1',
member_count: 2,
guest_count: 3,
pinnedpost_count: 4,
files_count: 5,
},
});
expect(state).not.toBe(nextState);
expect(nextState.stats).toEqual({
channel1: {
channel_id: 'channel1',
member_count: 2,
guest_count: 3,
pinnedpost_count: 4,
files_count: 5,
},
});
});
test("should update a channel's stats", () => {
const state = deepFreeze(channelsReducer({
stats: {
channel1: {
channel_id: 'channel1',
member_count: 1,
guest_count: 1,
pinnedpost_count: 1,
files_count: 1,
},
},
}, {}));
const nextState = channelsReducer(state, {
type: ChannelTypes.RECEIVED_CHANNEL_STATS,
data: {
channel_id: 'channel1',
member_count: 2,
guest_count: 3,
pinnedpost_count: 4,
files_count: 5,
},
});
expect(state).not.toBe(nextState);
expect(nextState.stats).toEqual({
channel1: {
channel_id: 'channel1',
member_count: 2,
guest_count: 3,
pinnedpost_count: 4,
files_count: 5,
},
});
});
test("should return the same object when a channel's stats are unchanged", () => {
const state = deepFreeze(channelsReducer({
stats: {
channel1: {
channel_id: 'channel1',
member_count: 2,
guest_count: 3,
pinnedpost_count: 4,
files_count: 5,
},
},
}, {}));
const nextState = channelsReducer(state, {
type: ChannelTypes.RECEIVED_CHANNEL_STATS,
data: {
channel_id: 'channel1',
member_count: 2,
guest_count: 3,
pinnedpost_count: 4,
files_count: 5,
},
});
expect(state).toBe(nextState);
});
});
describe('INCREMENT_FILE_COUNT', () => {
test('should change channel file count stats', () => {
const state = deepFreeze(channelsReducer({

View File

@ -583,11 +583,16 @@ function membersInChannel(state: RelationOneToOne<Channel, Record<string, Channe
function stats(state: RelationOneToOne<Channel, ChannelStats> = {}, action: AnyAction) {
switch (action.type) {
case ChannelTypes.RECEIVED_CHANNEL_STATS: {
const nextState = {...state};
const stat = action.data;
nextState[stat.channel_id] = stat;
const stat: ChannelStats = action.data;
return nextState;
if (isEqual(state[stat.channel_id], stat)) {
return state;
}
return {
...state,
[stat.channel_id]: stat,
};
}
case ChannelTypes.ADD_CHANNEL_MEMBER_SUCCESS: {
const nextState = {...state};