Skip to content

Commit

Permalink
test: ✅ extend test coverage and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CourtHive committed Apr 22, 2024
1 parent a6c0360 commit 92ded0b
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 5 deletions.
86 changes: 86 additions & 0 deletions src/tests/generators/events/generateEventsFromTieFormat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import mocksEngine from '@Assemblies/engines/mock';
import tournamentEngine from '@Engines/syncEngine';
import { expect, it } from 'vitest';

// Constants
import { INVALID_TIE_FORMAT, INVALID_VALUES } from '@Constants/errorConditionConstants';
import { USTA_GOLD_TEAM_CHALLENGE } from '@Constants/tieFormatConstants';
import tieFormatDefaults from '@Generators/templates/tieFormatDefaults';
import { ALTERNATE, UNGROUPED } from '@Constants/entryStatusConstants';
import { SINGLES_EVENT, TEAM_EVENT } from '@Constants/eventConstants';
import { TEAM_PARTICIPANT } from '@Constants/participantConstants';
import { FEMALE, MALE, MIXED } from '@Constants/genderConstants';

it('can generate singles/doubles events from tieFormats', () => {
const drawSize = 8;
mocksEngine.generateTournamentRecord({
participantsProfile: { participantsCount: 0 }, // required to force generation of tieFormat-specific participants
drawProfiles: [
{ generate: false, addEvent: false, eventType: TEAM_EVENT, drawSize, tieFormatName: USTA_GOLD_TEAM_CHALLENGE },
],
setState: true,
});

let events = tournamentEngine.getEvents().events;
expect(events.length).toBe(0);

const teamParticipants = tournamentEngine.getParticipants({
participantFilters: { participantTypes: [TEAM_PARTICIPANT] },
withIndividualParticipants: true,
}).participants;
expect(teamParticipants.length).toBe(drawSize);
teamParticipants.forEach((teamParticipant) => {
expect(teamParticipant.individualParticipantIds.length).toEqual(8);
const genders = teamParticipant.individualParticipants.map((p) => p.person?.sex);
expect(genders).toEqual([FEMALE, FEMALE, FEMALE, FEMALE, MALE, MALE, MALE, MALE]);
});

let result = tournamentEngine.generateEventsFromTieFormat();
expect(result.error).toEqual(INVALID_VALUES);

result = tournamentEngine.generateEventsFromTieFormat({ tieFormat: {} });
expect(result.error).toEqual(INVALID_TIE_FORMAT);

result = tournamentEngine.generateEventsFromTieFormat({ tieFormatName: {} });
expect(result.error).toEqual(INVALID_VALUES);

result = tournamentEngine.generateEventsFromTieFormat({ tieFormatName: '' });
expect(result.error).toEqual(INVALID_VALUES);

const tieFormat = tieFormatDefaults({ namedFormat: USTA_GOLD_TEAM_CHALLENGE });

const entryStatus = ALTERNATE;
result = tournamentEngine.generateEventsFromTieFormat({
addEntriesFromTeams: true,
addEvents: false,
entryStatus,
tieFormat,
});
expect(result.events.length).toBe(5);
expect(result.success).toBe(true);
for (const event of result.events) {
if (event.gender === MIXED) {
// 64 entries because 8 teams with 8 individualParticipants each = 64, all INDIVIDUALS are UNGROUPED
expect(event.entries.length).toBe(64);
} else {
// 32 entries in SINGLES because 8 teams with 4 gendered individualParticipants each = 32
// 32 entries in DOUBLES because 8 teams with 4 gendered individualParticipants each = 32, all INDIVIDUALS are UNGROUPED
expect(event.entries.length).toBe(32);
}

if (event.eventType === SINGLES_EVENT) {
expect(event.entries.every((entry) => entry.entryStatus === entryStatus)).toBe(true);
} else {
expect(event.entries.every((entry) => entry.entryStatus === UNGROUPED)).toBe(true);
}
}

events = tournamentEngine.getEvents().events;
expect(events.length).toBe(0);

result = tournamentEngine.generateEventsFromTieFormat({ tieFormatName: USTA_GOLD_TEAM_CHALLENGE });
expect(result.success).toBe(true);

events = tournamentEngine.getEvents().events;
expect(events.length).toBe(5);
});
87 changes: 87 additions & 0 deletions src/tests/generators/leagueScenarios/leagueNoRounds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { mocksEngine } from '@Assemblies/engines/mock';
import tournamentEngine from '@Engines/syncEngine';
import { expect, it } from 'vitest';

// constants
import { TEAM_PARTICIPANT } from '@Constants/participantConstants';
import { FEMALE, MALE, MIXED } from '@Constants/genderConstants';
import { DOUBLES, SINGLES } from '@Constants/matchUpTypes';
import { TEAM_EVENT } from '@Constants/eventConstants';
import { instanceCount } from '@Tools/arrays';

it('can generate Teams from drawProfiles without persisting events', () => {
const matchUpCount = 2;
const tieFormat = {
collectionDefinitions: [
{
collectionName: 'Doubles',
matchUpType: DOUBLES,
collectionOrder: 1,
matchUpValue: 1,
gender: MIXED,
matchUpCount,
},
{
collectionName: 'Singles',
matchUpType: SINGLES,
collectionOrder: 2,
matchUpValue: 1,
gender: MALE,
matchUpCount,
},
{
collectionName: 'Singles',
matchUpType: SINGLES,
collectionOrder: 2,
matchUpValue: 1,
gender: FEMALE,
matchUpCount,
},
],
winCriteria: { aggregateValue: true },
};

const teamNames = ['Team One', 'Team Two'];
const drawSize = 4;
const mockProfile = {
participantsProfile: { participantsCount: 0 }, // do not generate participants unless specified by drawProfile(s)
drawProfiles: [
{
eventType: TEAM_EVENT,
addEvent: false, // do not add event to tournamentRecord, only generate participants
generate: false,
teamNames,
tieFormat,
drawSize,
},
],
};

const { tournamentRecord } = mocksEngine.generateTournamentRecord({
setState: true,
...mockProfile,
});

const participantResult = tournamentEngine.getParticipants({ withIndividualParticipants: true });
const teams = participantResult.participants.filter((p) => p.participantType === TEAM_PARTICIPANT);

for (const team of teams) {
const counts = instanceCount(team.individualParticipants.map((i) => i.person.sex));
expect(counts.FEMALE).toEqual(matchUpCount);
expect(counts.MALE).toEqual(matchUpCount);
}

expect(teams.map((team) => team.participantName).slice(0, 2)).toEqual(teamNames);

const teamIndividualParticipants = teams
.reduce((acc, team) => {
acc.push(...team.individualParticipantIds);
return acc;
}, [])
.map((participantId) => participantResult.participantMap[participantId]);

const gendersCount = 2;
expect(teamIndividualParticipants.length).toEqual(drawSize * matchUpCount * gendersCount);

expect(tournamentRecord.events).toBeUndefined();
});
2 changes: 1 addition & 1 deletion src/tests/generators/mocks/participantScales.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ it('can assess predictive accuracy of scaleValues', () => {
matchUpFilters: { matchUpStatuses: [COMPLETED] },
ascending: true, // scale goes from low to high
valueAccessor: 'wtnRating',
matchUps: { boo: 1 },
scaleName: WTN,
zoneMargin: 3,
matchUps: { boo: 1 },
});
expect(result.error).toEqual(INVALID_VALUES);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ test('Consolation WO/WO advancing fed BYE', () => {
outcome: { matchUpStatus: DOUBLE_WALKOVER },
matchUpId,
drawId,
boo: true,
});
expect(result.success).toEqual(true);

Expand Down
2 changes: 1 addition & 1 deletion src/tests/mutations/tieFormats/compareTieFormats.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tieFormatDefaults from '@Assemblies/generators/templates/tieFormatDefaults';
import { compareTieFormats } from '@Query/hierarchical/tieFormats/compareTieFormats';
import tieFormatDefaults from '@Assemblies/generators/templates/tieFormatDefaults';
import { it, expect, test } from 'vitest';

// constants and types
Expand Down
2 changes: 1 addition & 1 deletion src/tests/queries/events/getPublishState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test('calling getPublishState directly', () => {
result = getPublishState({ tournamentRecord: {} });
expect(result.error).toEqual(INVALID_VALUES);

result = getPublishState({ tournamentRecord: { tournamentId: 'tid' }, eventId: 'boo' });
result = getPublishState({ tournamentRecord: { tournamentId: 'tid' }, eventId: 'eid' });
expect(result.error).toEqual(EVENT_NOT_FOUND);

// just for test coverage
Expand Down
3 changes: 2 additions & 1 deletion src/tests/queries/participants/getTournamentPersons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import mocksEngine from '@Assemblies/engines/mock';
import tournamentEngine from '@Engines/syncEngine';
import { expect, it } from 'vitest';

// Constants and fixtures
import PARTICIPANT_PRIVACY_DEFAULT from '@Fixtures/policies/POLICY_PRIVACY_DEFAULT';
import { MISSING_VALUE } from '@Constants/errorConditionConstants';
import { COMPETITOR, OFFICIAL } from '@Constants/participantRoles';
import { INDIVIDUAL } from '@Constants/participantConstants';

it('can retrieve and modify tournament persons', () => {
let tournamentPersons = tournamentEngine.getTournamentPersons({
tournamentRecord: { tournamentId: 'boo' },
tournamentRecord: { tournamentId: 'tid' },
}).tournamentPersons;
expect(tournamentPersons.length).toEqual(0);

Expand Down

0 comments on commit 92ded0b

Please sign in to comment.