Skip to content

Commit

Permalink
test: ✅ setOrderOfFinish: extend test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
CourtHive committed Apr 2, 2024
1 parent 53cb2bb commit 002d0fa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/mutate/drawDefinitions/matchUpGovernor/setOrderOfFinish.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import { getStructureMatchUps } from '@Query/structure/getStructureMatchUps';
import { modifyMatchUpNotice } from '@Mutate/notifications/drawNotifications';
import { decorateResult } from '@Functions/global/decorateResult';
import { getDrawMatchUps } from '@Query/matchUps/drawMatchUps';
import { getMatchUpId } from '@Functions/global/extractors';
import { mustBeAnArray } from '@Tools/mustBeAnArray';
import { isConvertableInteger } from '@Tools/math';
import { uniqueValues } from '@Tools/arrays';

import { SUCCESS } from '@Constants/resultConstants';
// constants and types
import { INVALID_MATCHUP_STATUS, INVALID_VALUES, MISSING_DRAW_DEFINITION } from '@Constants/errorConditionConstants';
import { DrawDefinition } from '@Types/tournamentTypes';

/**
*
* @param {object} drawDefinition - provided by drawEngine
* @param {object[]} finishingOrder - [{ matchUpId, orderOfFinish }] where order of finish is whole number
* @returns { success, error }
*/
import { DrawDefinition, Tournament } from '@Types/tournamentTypes';
import { SUCCESS } from '@Constants/resultConstants';

type SetOrderOfFinishArgs = {
finishingOrder: { matchUpId: string; orderOfFinish: number }[];
tournamentRecord: Tournament;
drawDefinition: DrawDefinition;
};
export function setOrderOfFinish({ drawDefinition, finishingOrder }: SetOrderOfFinishArgs) {

export function setOrderOfFinish({ tournamentRecord, drawDefinition, finishingOrder }: SetOrderOfFinishArgs) {
if (!drawDefinition) return { error: MISSING_DRAW_DEFINITION };
const stack = 'setOrderOfFinish';

Expand Down Expand Up @@ -143,7 +140,15 @@ export function setOrderOfFinish({ drawDefinition, finishingOrder }: SetOrderOfF
if (result.error) return decorateResult({ result, stack });

// apply the new values to targeted matchUps
result.completedMatchUps?.forEach((matchUp) => (matchUp.orderOfFinish = valuesMap[matchUp.matchUpId]));
result.completedMatchUps?.forEach((matchUp) => {
matchUp.orderOfFinish = valuesMap[matchUp.matchUpId];
modifyMatchUpNotice({
tournamentId: tournamentRecord.tournamentId,
context: 'setOrderOfFinish',
drawDefinition,
matchUp,
});
});
}

return { ...SUCCESS };
Expand Down
22 changes: 20 additions & 2 deletions src/tests/mutations/events/setOrderOfFinish.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { generateTeamTournament } from '../participants/team/generateTestTeamTournament';
import { setSubscriptions } from '@Global/state/globalState';
import tournamentEngine from '@Engines/syncEngine';
import { mocksEngine } from '../../..';
import { expect, it } from 'vitest';

// constants
import { INVALID_MATCHUP_STATUS, INVALID_VALUES } from '@Constants/errorConditionConstants';
import { DOUBLES, SINGLES, TEAM } from '@Constants/matchUpTypes';
import { COMPLETED } from '@Constants/matchUpStatusConstants';
import { MODIFY_MATCHUP } from '@Constants/topicConstants';
import { MAIN } from '@Constants/drawDefinitionConstants';
import { DOUBLES, SINGLES, TEAM } from '@Constants/matchUpTypes';
import { INVALID_MATCHUP_STATUS, INVALID_VALUES } from '@Constants/errorConditionConstants';

it('can both assign and remove individualParticipants in SINGLES matchUps that are part of team events', () => {
let matchUpModifications: any[] = [];
setSubscriptions({
subscriptions: {
[MODIFY_MATCHUP]: (result) => {
matchUpModifications.push(result);
},
},
});
const { tournamentRecord, drawId } = generateTeamTournament({
singlesCount: 6,
doublesCount: 3,
Expand Down Expand Up @@ -112,6 +123,7 @@ it('can both assign and remove individualParticipants in SINGLES matchUps that a
});
expect(result.error).toEqual(INVALID_VALUES);

matchUpModifications = [];
// attempt to assign orderOfFinish to all singlesMatchUps
finishingOrder = singlesMatchUps.slice(0, 3).map(({ matchUpId }, index) => ({
orderOfFinish: index + 1,
Expand All @@ -122,4 +134,10 @@ it('can both assign and remove individualParticipants in SINGLES matchUps that a
drawId,
});
expect(result.success).toEqual(true);

const matchUpIds = singlesMatchUps.map(({ matchUpId }) => matchUpId);
const matchUps = tournamentEngine.allTournamentMatchUps({ matchUpFilters: { matchUpIds } }).matchUps;
expect(matchUps.map(({ orderOfFinish }) => orderOfFinish)).toEqual([1, 2, 3, undefined, undefined, undefined]);
expect(matchUpModifications.length).toEqual(1);
expect(matchUpModifications.flat(Infinity).length).toEqual(3);
});

0 comments on commit 002d0fa

Please sign in to comment.