From 8235f61e5a5d0f487c6ec61146a32e73857f1d1c Mon Sep 17 00:00:00 2001 From: Thomas Belin Date: Wed, 11 Oct 2023 11:47:17 +0200 Subject: [PATCH] fix random tests --- .../Message/MemberMessage.test.tsx | 136 ++---------------- .../Message/MemberMessage/MessageContent.tsx | 119 --------------- 2 files changed, 12 insertions(+), 243 deletions(-) diff --git a/src/script/components/MessagesList/Message/MemberMessage.test.tsx b/src/script/components/MessagesList/Message/MemberMessage.test.tsx index d3d86f1359d..66b04cecd0a 100644 --- a/src/script/components/MessagesList/Message/MemberMessage.test.tsx +++ b/src/script/components/MessagesList/Message/MemberMessage.test.tsx @@ -19,6 +19,8 @@ import {render} from '@testing-library/react'; +import {randomInt} from 'crypto'; + import en from 'I18n/en-US.json'; import {MemberMessage as MemberMessageEntity} from 'src/script/entity/message/MemberMessage'; import {User} from 'src/script/entity/User'; @@ -27,10 +29,11 @@ import {generateUser} from 'test/helper/UserGenerator'; import {setStrings} from 'Util/LocalizerUtil'; import {MemberMessage} from './MemberMessage'; -import {config} from './MemberMessage/MessageContent'; setStrings({en}); +const config = MemberMessageEntity.CONFIG; + function createMemberMessage(type: SystemMessageType, users?: User[]) { const message = new MemberMessageEntity(); message.memberMessageType = type; @@ -67,7 +70,7 @@ describe('MemberMessage', () => { describe('CONVERSATION_CREATE', () => { it('displays participants of a newly created conversation', () => { - const nbUsers = Math.floor(Math.random() * 10); + const nbUsers = randomInt(1, 10); const users = Array.from({length: nbUsers}, () => generateUser()); const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); const props = { @@ -82,7 +85,7 @@ describe('MemberMessage', () => { }); it('displays a showMore when there are more than 17 users', () => { - const nbExtraUsers = Math.floor(Math.random() * 10); + const nbExtraUsers = randomInt(1, 10); const nbUsers = config.MAX_USERS_VISIBLE + nbExtraUsers; const users = Array.from({length: nbUsers}, () => generateUser()); @@ -97,7 +100,7 @@ describe('MemberMessage', () => { }); it('displays all team members', () => { - const nbExtraUsers = Math.floor(Math.random() * 10); + const nbExtraUsers = randomInt(1, 10); const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); @@ -113,7 +116,7 @@ describe('MemberMessage', () => { }); it('displays all team members and one guest message', () => { - const nbExtraUsers = Math.floor(Math.random() * 10); + const nbExtraUsers = randomInt(1, 10); const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE + nbExtraUsers; const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); @@ -131,7 +134,7 @@ describe('MemberMessage', () => { }); it('displays all team members and multiple guests message', () => { - const nbGuests = 2 + Math.floor(Math.random() * 10); + const nbGuests = randomInt(2, 10); const nbTeamUsers = config.MAX_WHOLE_TEAM_USERS_VISIBLE; const teamUsers = Array.from({length: nbTeamUsers}, () => generateUser()); @@ -152,7 +155,7 @@ describe('MemberMessage', () => { }); it('displays that another user created a conversation', () => { - const nbUsers = Math.floor(Math.random() * 10); + const nbUsers = randomInt(1, 10); const users = Array.from({length: nbUsers}, () => generateUser()); const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); message.name(''); @@ -167,7 +170,7 @@ describe('MemberMessage', () => { }); it('displays that self user created a conversation', () => { - const nbUsers = Math.floor(Math.random() * 10); + const nbUsers = randomInt(1, 10); const users = Array.from({length: nbUsers}, () => generateUser()); const message = createMemberMessage(SystemMessageType.CONVERSATION_CREATE, users); message.name(''); @@ -182,120 +185,5 @@ describe('MemberMessage', () => { }); }); - 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`); - }); - }); + describe('MEMBER_JOIN', () => {}); }); diff --git a/src/script/components/MessagesList/Message/MemberMessage/MessageContent.tsx b/src/script/components/MessagesList/Message/MemberMessage/MessageContent.tsx index 38908b2032a..07b11530e2c 100644 --- a/src/script/components/MessagesList/Message/MemberMessage/MessageContent.tsx +++ b/src/script/components/MessagesList/Message/MemberMessage/MessageContent.tsx @@ -20,126 +20,7 @@ import {MemberMessage as MemberMessageEntity} from 'src/script/entity/message/MemberMessage'; import {useKoSubscribableChildren} from 'Util/ComponentUtil'; -export const config = { - MAX_USERS_VISIBLE: 17, - MAX_WHOLE_TEAM_USERS_VISIBLE: 10, - REDUCED_USERS_COUNT: 15, -} as const; - -/* -function getMessage(message: MemberMessageEntity) { - if (!message.hasUsers()) { - return ''; - } - const exceedsMaxVisibleUsers = message.joinedUserEntities().length > config.MAX_USERS_VISIBLE; - - const replaceShowMore = { - '/showmore': '', - showmore: '', - }; - - const count = message.hiddenUserCount(); - const dativeUsers = message.generateNameString(exceedsMaxVisibleUsers, Declension.DATIVE); - const accusativeUsers = message.generateNameString(exceedsMaxVisibleUsers, Declension.ACCUSATIVE); - const name = message.senderName(); - - switch (message.memberMessageType) { - case SystemMessageType.CONVERSATION_CREATE: { - if (message.name().length) { - const exceedsMaxTeam = message.joinedUserEntities().length > config.MAX_WHOLE_TEAM_USERS_VISIBLE; - if (message.allTeamMembers && exceedsMaxTeam) { - const guestCount = message.joinedUserEntities().filter(userEntity => userEntity.isGuest()).length; - if (!guestCount) { - return t('conversationCreateTeam', {}, replaceShowMore); - } - - const hasSingleGuest = guestCount === 1; - return hasSingleGuest - ? t('conversationCreateTeamGuest', {}, replaceShowMore) - : t('conversationCreateTeamGuests', guestCount, replaceShowMore); - } - - return exceedsMaxVisibleUsers - ? t('conversationCreateWithMore', {count: count.toString(), users: dativeUsers}, replaceShowMore) - : t('conversationCreateWith', dativeUsers); - } - - if (message.user().isMe) { - return exceedsMaxVisibleUsers - ? t('conversationCreatedYouMore', {count: count.toString(), users: dativeUsers}, replaceShowMore) - : t('conversationCreatedYou', dativeUsers); - } - - return exceedsMaxVisibleUsers - ? t('conversationCreatedMore', {count: count.toString(), name, users: dativeUsers}, replaceShowMore) - : t('conversationCreated', {name, users: dativeUsers}); - } - - case SystemMessageType.CONVERSATION_RESUME: { - return t('conversationResume', message.generateNameString(false, Declension.DATIVE)); - } - - default: - break; - } - - switch (message.type) { - case CONVERSATION_EVENT.MEMBER_JOIN: { - const senderJoined = matchQualifiedIds(message.otherUser(), message.user()); - if (senderJoined) { - return message.user().isMe - ? t('conversationMemberJoinedSelfYou') - : t('conversationMemberJoinedSelf', message.senderName()); - } - - if (message.user().isMe) { - return exceedsMaxVisibleUsers - ? t('conversationMemberJoinedYouMore', {count: count.toString(), users: accusativeUsers}, replaceShowMore) - : t('conversationMemberJoinedYou', accusativeUsers, replaceShowMore); - } - return exceedsMaxVisibleUsers - ? t('conversationMemberJoinedMore', {count: count.toString(), name, users: accusativeUsers}, replaceShowMore) - : t('conversationMemberJoined', {name, users: accusativeUsers}, replaceShowMore); - } - - case CONVERSATION_EVENT.MEMBER_LEAVE: { - if (message.reason === MemberLeaveReason.LEGAL_HOLD_POLICY_CONFLICT) { - return message.generateLegalHoldLeaveMessage(); - } - const temporaryGuestRemoval = message.otherUser().isMe && message.otherUser().isTemporaryGuest(); - if (temporaryGuestRemoval) { - return t('temporaryGuestLeaveMessage'); - } - - const senderLeft = matchQualifiedIds(message.otherUser(), message.user()); - if (senderLeft) { - return message.user().isMe ? t('conversationMemberLeftYou') : t('conversationMemberLeft', name); - } - - const allUsers = message.generateNameString(); - if (!message.user().isMe && !name) { - return t('conversationMemberWereRemoved', allUsers); - } - return message.user().isMe - ? t('conversationMemberRemovedYou', allUsers) - : t('conversationMemberRemoved', {name, users: allUsers}); - } - - case ClientEvent.CONVERSATION.TEAM_MEMBER_LEAVE: { - return t('conversationTeamLeft', name); - } - - default: - break; - } - return ''; -} -*/ - export function MessageContent({message}: {message: MemberMessageEntity}) { const {htmlCaption} = useKoSubscribableChildren(message, ['htmlCaption']); return

; - //const textContent = getMessage(message); - - //return

{textContent}

; }