MM-59276 Don't update channel stats state unnecessarily (#27805)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
bb78de5b7f
Коммит
3f4b8e8137
@@ -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', () => {
|
describe('INCREMENT_FILE_COUNT', () => {
|
||||||
test('should change channel file count stats', () => {
|
test('should change channel file count stats', () => {
|
||||||
const state = deepFreeze(channelsReducer({
|
const state = deepFreeze(channelsReducer({
|
||||||
|
|||||||
@@ -583,11 +583,16 @@ function membersInChannel(state: RelationOneToOne<Channel, Record<string, Channe
|
|||||||
function stats(state: RelationOneToOne<Channel, ChannelStats> = {}, action: AnyAction) {
|
function stats(state: RelationOneToOne<Channel, ChannelStats> = {}, action: AnyAction) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case ChannelTypes.RECEIVED_CHANNEL_STATS: {
|
case ChannelTypes.RECEIVED_CHANNEL_STATS: {
|
||||||
const nextState = {...state};
|
const stat: ChannelStats = action.data;
|
||||||
const stat = action.data;
|
|
||||||
nextState[stat.channel_id] = stat;
|
|
||||||
|
|
||||||
return nextState;
|
if (isEqual(state[stat.channel_id], stat)) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
[stat.channel_id]: stat,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
case ChannelTypes.ADD_CHANNEL_MEMBER_SUCCESS: {
|
case ChannelTypes.ADD_CHANNEL_MEMBER_SUCCESS: {
|
||||||
const nextState = {...state};
|
const nextState = {...state};
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user