Skip to content

Commit 83c6dd5

Browse files
authored
1 parent e1b56ad commit 83c6dd5

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/modules/ChannelList/dux/__tests__/reducers.spec.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import mockData, { channel1, channel0, users, creatingChannel } from '../data.mo
22
import * as actionTypes from '../actionTypes';
33
import reducers from '../reducers';
44
import initialState from '../initialState';
5+
import { isChannelJustCreated } from '../../../../utils';
6+
import { getNextChannel } from '../getNextChannel';
7+
58
const [user1, user2, user3] = users;
69

710
describe('Channels-Reducers', () => {
@@ -906,4 +909,66 @@ describe('Channels-Reducers', () => {
906909
expect(hideWithPreventAutoOnPreventAutoState.allChannels.length).toEqual(preventAutoUnhideParamsState.allChannels.length + 1);
907910
});
908911
});
912+
describe('isChannelJustCreated', () => {
913+
let isChannelJustCreatedSpy;
914+
let getNextChannelSpy;
915+
916+
beforeEach(() => {
917+
isChannelJustCreatedSpy = jest.spyOn(require('../../../../utils'), 'isChannelJustCreated');
918+
getNextChannelSpy = jest.spyOn(require('../getNextChannel'), 'getNextChannel');
919+
});
920+
921+
afterEach(() => {
922+
isChannelJustCreatedSpy.mockRestore();
923+
getNextChannelSpy.mockRestore();
924+
});
925+
926+
it('should not add a newly created channel to the ChannelList if it is just created and the current user is the only member', () => {
927+
const newChannel = {
928+
...creatingChannel,
929+
members: [user1],
930+
createdAt: new Date(),
931+
invitedAt: new Date(),
932+
lastMessage: null,
933+
};
934+
isChannelJustCreated.mockReturnValue(true);
935+
const prevState = { ...mockData, channelListQuery: { includeEmpty: false } };
936+
937+
const nextState = reducers(prevState, {
938+
type: actionTypes.ON_USER_JOINED,
939+
payload: newChannel,
940+
});
941+
942+
expect(isChannelJustCreated).toHaveBeenCalledWith(newChannel);
943+
expect(nextState.allChannels.find(channel => channel.url === newChannel.url)).toBeUndefined();
944+
expect(nextState.currentChannel).toEqual(mockData.currentChannel);
945+
// Check if nextState and prevState are deeply equal
946+
expect(nextState).toEqual(prevState);
947+
});
948+
949+
it('should add a newly created channel to the ChannelList if it is not just created', () => {
950+
const newChannel = {
951+
...creatingChannel,
952+
members: [user1, user2],
953+
createdAt: 123456789,
954+
invitedAt: 129999967,
955+
lastMessage: null,
956+
};
957+
isChannelJustCreated.mockReturnValue(false);
958+
getNextChannel.mockReturnValue(newChannel);
959+
const prevState = { ...mockData, channelListQuery: { includeEmpty: false } };
960+
961+
const nextState = reducers(prevState, {
962+
type: actionTypes.ON_USER_JOINED,
963+
payload: newChannel,
964+
});
965+
966+
expect(isChannelJustCreated).toHaveBeenCalledWith(newChannel);
967+
// Should be undefind since the newly created channel won't be added to the allChannels state
968+
expect(nextState.allChannels.find(channel => channel.url === newChannel.url)).toBeUndefined();
969+
expect(getNextChannel).toHaveBeenCalled();
970+
// Ensure state has changed
971+
expect(nextState).not.toEqual(prevState);
972+
});
973+
});
909974
});

src/modules/ChannelList/dux/reducers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { match, P } from 'ts-pattern';
2-
import { filterChannelListParams, getChannelsWithUpsertedChannel } from '../../../utils';
2+
import { filterChannelListParams, getChannelsWithUpsertedChannel, isChannelJustCreated } from '../../../utils';
33
import * as channelListActions from './actionTypes';
44
import { ChannelListActionTypes } from './actionTypes';
55
import { getNextChannel } from './getNextChannel';
@@ -184,6 +184,11 @@ export default function channelListReducer(
184184
allChannels: getChannelsWithUpsertedChannel(allChannels, channel, state.channelListQuery?.order),
185185
};
186186
}
187+
// If the channel is just created and the current user is the only member,
188+
// don't add to the ChannelList but keep the currentChannel
189+
if (isChannelJustCreated(channel)) {
190+
return state;
191+
}
187192
// Filter the channel from the ChannelList
188193
// Replace the currentChannel if it's filtered channel
189194
const nextChannel = getNextChannel({

src/utils/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { getOutgoingMessageState, OutgoingMessageStates } from './exports/getOut
1717
import { MessageContentMiddleContainerType, Nullable } from '../types';
1818
import { isSafari } from './browser';
1919
import { match } from 'ts-pattern';
20+
import isSameSecond from 'date-fns/isSameSecond';
2021

2122
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
2223
export const SUPPORTED_MIMES = {
@@ -947,3 +948,10 @@ export const arrayEqual = (array1: Array<unknown>, array2: Array<unknown>): bool
947948
export const isSendableMessage = (message?: BaseMessage | null): message is SendableMessageType => {
948949
return Boolean(message) && 'sender' in (message as SendableMessage);
949950
};
951+
952+
/**
953+
* If the channel is just created, the channel's createdAt and currentUser's invitedAt are the same.
954+
*/
955+
export const isChannelJustCreated = (channel: GroupChannel): boolean => {
956+
return isSameSecond(channel.createdAt, channel.invitedAt) && !channel.lastMessage;
957+
};

0 commit comments

Comments
 (0)