Skip to content

Commit

Permalink
fix random tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc committed Oct 11, 2023
1 parent e13787d commit 8235f61
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 243 deletions.
136 changes: 12 additions & 124 deletions src/script/components/MessagesList/Message/MemberMessage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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 = {
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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('');
Expand All @@ -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('');
Expand All @@ -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(<MemberMessage {...props} />);
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(<MemberMessage {...props} />);
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(<MemberMessage {...props} />);
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(<MemberMessage {...props} />);
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(<MemberMessage {...props} />);
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(<MemberMessage {...props} />);
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(<MemberMessage {...props} />);
expect(container.textContent).toContain(`You started a conversation with`);
});
});
describe('MEMBER_JOIN', () => {});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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': '</a>',
showmore: '<a class="message-header-show-more" data-uie-name="do-show-more">',
};
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 <p className="message-header-caption" dangerouslySetInnerHTML={{__html: htmlCaption}}></p>;
//const textContent = getMessage(message);

//return <p className="message-header-caption">{textContent}</p>;
}

0 comments on commit 8235f61

Please sign in to comment.