@@ -2,6 +2,9 @@ import mockData, { channel1, channel0, users, creatingChannel } from '../data.mo
22import * as actionTypes from '../actionTypes' ;
33import reducers from '../reducers' ;
44import initialState from '../initialState' ;
5+ import { isChannelJustCreated } from '../../../../utils' ;
6+ import { getNextChannel } from '../getNextChannel' ;
7+
58const [ user1 , user2 , user3 ] = users ;
69
710describe ( '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} ) ;
0 commit comments