From e13787d8178177ad37f415e8feba076b51845453 Mon Sep 17 00:00:00 2001 From: Thomas Belin Date: Wed, 11 Oct 2023 11:34:06 +0200 Subject: [PATCH] move test to parent component --- .../Message/MemberMessage.test.tsx | 307 +++++++++++++++--- .../MemberMessage/MessageContent.test.tsx | 110 ------- src/script/entity/message/MemberMessage.ts | 15 - 3 files changed, 264 insertions(+), 168 deletions(-) delete mode 100644 src/script/components/MessagesList/Message/MemberMessage/MessageContent.test.tsx diff --git a/src/script/components/MessagesList/Message/MemberMessage.test.tsx b/src/script/components/MessagesList/Message/MemberMessage.test.tsx index d9dd926670a2..d3d86f1359d8 100644 --- a/src/script/components/MessagesList/Message/MemberMessage.test.tsx +++ b/src/script/components/MessagesList/Message/MemberMessage.test.tsx @@ -18,63 +18,284 @@ */ import {render} from '@testing-library/react'; -import ko from 'knockout'; +import en from 'I18n/en-US.json'; import {MemberMessage as MemberMessageEntity} from 'src/script/entity/message/MemberMessage'; import {User} from 'src/script/entity/User'; +import {SystemMessageType} from 'src/script/message/SystemMessageType'; +import {generateUser} from 'test/helper/UserGenerator'; +import {setStrings} from 'Util/LocalizerUtil'; import {MemberMessage} from './MemberMessage'; +import {config} from './MemberMessage/MessageContent'; -const createMemberMessage = (partialMemberMessage: Partial) => { - const memberMessage: Partial = { - hasUsers: ko.pureComputed(() => false), - isGroupCreation: () => false, - isMemberChange: () => false, - isMemberJoin: () => false, - isMemberLeave: () => false, - isMemberRemoval: () => false, - showLargeAvatar: () => false, - showNamedCreation: ko.pureComputed(() => true), - timestamp: ko.observable(Date.now()), - ...partialMemberMessage, - }; - return memberMessage as MemberMessageEntity; +setStrings({en}); + +function createMemberMessage(type: SystemMessageType, users?: User[]) { + const message = new MemberMessageEntity(); + message.memberMessageType = type; + message.user(new User()); + if (users) { + message.userIds(users.map(user => user.qualifiedId)); + message.userEntities(users); + } + message.name('message'); + + return message; +} + +const baseProps = { + hasReadReceiptsTurnedOn: false, + isSelfTemporaryGuest: false, + onClickCancelRequest: jest.fn(), + onClickInvitePeople: jest.fn(), + onClickParticipants: jest.fn(), + shouldShowInvitePeople: false, + conversationName: 'group 1', }; describe('MemberMessage', () => { it('shows connected message', async () => { const props = { - hasReadReceiptsTurnedOn: false, - isSelfTemporaryGuest: false, - message: createMemberMessage({ - otherUser: ko.pureComputed(() => new User('id')), - showLargeAvatar: () => true, - }), - onClickCancelRequest: () => {}, - onClickInvitePeople: () => {}, - onClickParticipants: () => {}, - shouldShowInvitePeople: false, - conversationName: 'group 1', + ...baseProps, + message: createMemberMessage(SystemMessageType.CONNECTION_ACCEPTED, [new User('id')]), }; - const {queryByTestId} = render(); - expect(queryByTestId('element-connected-message')).not.toBeNull(); + const {getByTestId} = render(); + expect(getByTestId('element-connected-message')).not.toBeNull(); }); - it('shows conversation title', async () => { - const props = { - hasReadReceiptsTurnedOn: false, - isSelfTemporaryGuest: false, - message: createMemberMessage({ - otherUser: ko.pureComputed(() => new User('id')), - }), - onClickCancelRequest: () => {}, - onClickInvitePeople: () => {}, - onClickParticipants: () => {}, - shouldShowInvitePeople: false, - conversationName: 'group 1', - }; - const {getByTestId} = render(); - expect(getByTestId('conversation-name').textContent).toBe(props.conversationName); + describe('CONVERSATION_CREATE', () => { + it('displays participants of a newly created conversation', () => { + const nbUsers = Math.floor(Math.random() * 10); + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + users.forEach(user => { + expect(getByText(user.name())).not.toBeNull(); + }); + }); + + it('displays a showMore when there are more than 17 users', () => { + const nbExtraUsers = Math.floor(Math.random() * 10); + const nbUsers = config.MAX_USERS_VISIBLE + nbExtraUsers; + + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`${nbUsers - config.REDUCED_USERS_COUNT} more`)).not.toBeNull(); + }); + + it('displays all team members', () => { + const nbExtraUsers = Math.floor(Math.random() * 10); + const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; + + const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, teamUsers); + message.allTeamMembers = teamUsers; + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`all team members`)).not.toBeNull(); + }); + + it('displays all team members and one guest message', () => { + const nbExtraUsers = Math.floor(Math.random() * 10); + const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; + + const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); + const guest = generateUser(); + guest.isGuest(true); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, [...teamUsers, guest]); + message.allTeamMembers = teamUsers; + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`all team members and one guest`)).not.toBeNull(); + }); + + it('displays all team members and multiple guests message', () => { + const nbGuests = 2 + Math.floor(Math.random() * 10); + const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE; + + const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); + const guests = Array.from({length: nbGuests}, () => { + const guest = generateUser(); + guest.isGuest(true); + return guest; + }); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, [...teamUsers, ...guests]); + message.allTeamMembers = teamUsers; + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`all team members and ${nbGuests} guests`)).not.toBeNull(); + }); + + it('displays that another user created a conversation', () => { + const nbUsers = Math.floor(Math.random() * 10); + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + message.name(''); + message.user().name('Creator'); + const props = { + ...baseProps, + message, + }; + + const {container} = render(); + expect(container.textContent).toContain(`Creator started a conversation with`); + }); + + it('displays that self user created a conversation', () => { + const nbUsers = Math.floor(Math.random() * 10); + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + message.name(''); + message.user().isMe = true; + const props = { + ...baseProps, + message, + }; + + const {container} = render(); + expect(container.textContent).toContain(`You started a conversation with`); + }); + }); + + describe('MEMBER_JOIN', () => { + it('displays participants of a newly created conversation', () => { + const nbUsers = Math.floor(Math.random() * 10); + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + users.forEach(user => { + expect(getByText(user.name())).not.toBeNull(); + }); + }); + + it('displays a showMore when there are more than 17 users', () => { + const nbExtraUsers = Math.floor(Math.random() * 10); + const nbUsers = config.MAX_USERS_VISIBLE + nbExtraUsers; + + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`${nbUsers - config.REDUCED_USERS_COUNT} more`)).not.toBeNull(); + }); + + it('displays all team members', () => { + const nbExtraUsers = Math.floor(Math.random() * 10); + const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; + + const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, teamUsers); + message.allTeamMembers = teamUsers; + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`all team members`)).not.toBeNull(); + }); + + it('displays all team members and one guest message', () => { + const nbExtraUsers = Math.floor(Math.random() * 10); + const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; + + const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); + const guest = generateUser(); + guest.isGuest(true); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, [...teamUsers, guest]); + message.allTeamMembers = teamUsers; + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`all team members and one guest`)).not.toBeNull(); + }); + + it('displays all team members and multiple guests message', () => { + const nbGuests = 2 + Math.floor(Math.random() * 10); + const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE; + + const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); + const guests = Array.from({length: nbGuests}, () => { + const guest = generateUser(); + guest.isGuest(true); + return guest; + }); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, [...teamUsers, ...guests]); + message.allTeamMembers = teamUsers; + const props = { + ...baseProps, + message, + }; + + const {getByText} = render(); + expect(getByText(`all team members and ${nbGuests} guests`)).not.toBeNull(); + }); + + it('displays that another user created a conversation', () => { + const nbUsers = Math.floor(Math.random() * 10); + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + message.name(''); + message.user().name('Creator'); + const props = { + ...baseProps, + message, + }; + + const {container} = render(); + expect(container.textContent).toContain(`Creator started a conversation with`); + }); + + it('displays that self user created a conversation', () => { + const nbUsers = Math.floor(Math.random() * 10); + const users = Array.from({length: nbUsers}, () => generateUser()); + const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); + message.name(''); + message.user().isMe = true; + const props = { + ...baseProps, + message, + }; + + const {container} = render(); + expect(container.textContent).toContain(`You started a conversation with`); + }); }); }); diff --git a/src/script/components/MessagesList/Message/MemberMessage/MessageContent.test.tsx b/src/script/components/MessagesList/Message/MemberMessage/MessageContent.test.tsx deleted file mode 100644 index 5e5c008e9191..000000000000 --- a/src/script/components/MessagesList/Message/MemberMessage/MessageContent.test.tsx +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Wire - * Copyright (C) 2021 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - * - */ - -import {render} from '@testing-library/react'; - -import en from 'I18n/en-US.json'; -import {MemberMessage as MemberMessageEntity} from 'src/script/entity/message/MemberMessage'; -import {User} from 'src/script/entity/User'; -import {SystemMessageType} from 'src/script/message/SystemMessageType'; -import {generateUser} from 'test/helper/UserGenerator'; -import {setStrings} from 'Util/LocalizerUtil'; - -import {config, MessageContent} from './MessageContent'; - -setStrings({en}); - -function createMemberMessage(type: SystemMessageType, users: User[]) { - const message = new MemberMessageEntity(); - message.memberMessageType = type; - message.userIds(users.map(user => user.qualifiedId)); - message.userEntities(users); - message.name('message'); - - return message; -} - -describe('MessageContent', () => { - describe('CONVERSATION_CREATE', () => { - it('displays participants of a newly created conversation', () => { - const nbUsers = Math.floor(Math.random() * 10); - const users = Array.from({length: nbUsers}, () => generateUser()); - const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); - - const {getByText} = render(); - users.forEach(user => { - expect(getByText(user.name())).not.toBeNull(); - }); - }); - - it('displays a showMore when there are more than 17 users', () => { - const nbExtraUsers = Math.floor(Math.random() * 10); - const nbUsers = config.MAX_USERS_VISIBLE + nbExtraUsers; - - const users = Array.from({length: nbUsers}, () => generateUser()); - const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); - - const {getByText} = render(); - expect(getByText(`${nbUsers - config.REDUCED_USERS_COUNT} more`)).not.toBeNull(); - }); - - it('displays all team members', () => { - const nbExtraUsers = Math.floor(Math.random() * 10); - const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; - - const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); - const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, teamUsers); - message.allTeamMembers = teamUsers; - - const {getByText} = render(); - expect(getByText(`all team members`)).not.toBeNull(); - }); - - it('displays all team members and one guest message', () => { - const nbExtraUsers = Math.floor(Math.random() * 10); - const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; - - const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); - const guest = generateUser(); - guest.isGuest(true); - const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, [...teamUsers, guest]); - message.allTeamMembers = teamUsers; - - const {getByText} = render(); - expect(getByText(`all team members and one guest`)).not.toBeNull(); - }); - - it('displays all team members and multiple guests message', () => { - const nbGuests = 2 + Math.floor(Math.random() * 10); - const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE; - - const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); - const guests = Array.from({length: nbGuests}, () => { - const guest = generateUser(); - guest.isGuest(true); - return guest; - }); - const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, [...teamUsers, ...guests]); - message.allTeamMembers = teamUsers; - - const {getByText} = render(); - expect(getByText(`all team members and ${nbGuests} guests`)).not.toBeNull(); - }); - }); -}); diff --git a/src/script/entity/message/MemberMessage.ts b/src/script/entity/message/MemberMessage.ts index 581788b22fce..a94df421b0c6 100644 --- a/src/script/entity/message/MemberMessage.ts +++ b/src/script/entity/message/MemberMessage.ts @@ -134,21 +134,6 @@ export class MemberMessage extends SystemMessage { const name = this.senderName(); switch (this.memberMessageType) { - case SystemMessageType.CONNECTION_ACCEPTED: - case SystemMessageType.CONNECTION_REQUEST: { - if (this.otherUser()) { - if (this.otherUser().isBlocked()) { - return t('conversationConnectionBlocked'); - } - - if (this.otherUser().isOutgoingRequest()) { - return ''; - } - } - - return t('conversationConnectionAccepted'); - } - case SystemMessageType.CONVERSATION_CREATE: { if (this.name().length) { const exceedsMaxTeam = this.joinedUserEntities().length > MemberMessage.CONFIG.MAX_WHOLE_TEAM_USERS_VISIBLE;