Skip to content

Commit

Permalink
test: ✅ test coverage for setEventDates
Browse files Browse the repository at this point in the history
  • Loading branch information
CourtHive committed Mar 29, 2024
1 parent c9ad969 commit 8867131
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/mutate/events/setEventDates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ type SetEventDatesArgs = {

export function setEventDates(params: SetEventDatesArgs) {
const paramsCheck = checkRequiredParameters(params, [
{ tournamentRecord: true },
{ tournamentRecord: true, event: true },
{
[VALIDATE]: (value) => dateValidation.test(value),
[ANY_OF]: { startDate: false, endDate: false },
[INVALID]: INVALID_DATE,
startDate: false,
endDate: false,
},
{
[VALIDATE]: (value) => value.every((d) => dateValidation.test(d)),
Expand All @@ -119,7 +120,7 @@ export function setEventDates(params: SetEventDatesArgs) {
const end = endDate || tournamentRecord.endDate;
const validStart = !start || activeDates.every((d) => new Date(d) >= new Date(start));
const validEnd = !end || activeDates.every((d) => new Date(d) <= new Date(end));
if (!validStart || !validEnd) return { error: INVALID_VALUES };
if (!validStart || !validEnd) return { error: INVALID_DATE };
}

if (startDate) {
Expand Down
2 changes: 2 additions & 0 deletions src/mutate/tournaments/setTournamentDates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ export function setTournamentDates(params: SetTournamentDatesArgs): ResultType &
}

export function setTournamentStartDate({ tournamentRecord, startDate }) {
if (!startDate) return { error: INVALID_DATE };
return setTournamentDates({ tournamentRecord, startDate });
}

export function setTournamentEndDate({ tournamentRecord, endDate }) {
if (!endDate) return { error: INVALID_DATE };
return setTournamentDates({ tournamentRecord, endDate });
}

Expand Down
38 changes: 35 additions & 3 deletions src/tests/mutations/events/setEventDates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import mocksEngine from '@Assemblies/engines/mock';
import { expect, test } from 'vitest';

import tournamentEngineSync from '@Engines/syncEngine';
import { INVALID_DATE, MISSING_EVENT } from '@Constants/errorConditionConstants';
import { INVALID_DATE, INVALID_VALUES, MISSING_EVENT } from '@Constants/errorConditionConstants';
import { MON } from '@Constants/weekdayConstants';

test.each([tournamentEngineSync])('can modify event.startDate and event.endDate', async (tournamentEngine) => {
const { tournamentRecord } = mocksEngine.generateTournamentRecord();
Expand Down Expand Up @@ -84,8 +85,8 @@ test.each([tournamentEngineSync])(

const newEventStartDate = dateStringDaysChange(startDate, -1);
let result = await tournamentEngine.setEventStartDate({
eventId,
startDate: newEventStartDate,
eventId,
});
expect(result.success).toEqual(undefined);
expect(result.error).toEqual(INVALID_DATE);
Expand All @@ -94,8 +95,8 @@ test.each([tournamentEngineSync])(

const newEventEndDate = dateStringDaysChange(endDate, 1);
result = await tournamentEngine.setEventEndDate({
eventId,
endDate: newEventEndDate,
eventId,
});
expect(result.success).toEqual(undefined);
expect(result.error).toEqual(INVALID_DATE);
Expand All @@ -108,5 +109,36 @@ test.each([tournamentEngineSync])(
({ event } = await tournamentEngine.getEvent({ eventId }));
expect(event.startDate).toEqual(startDate);
expect(event.endDate).toEqual(endDate);

result = await tournamentEngine.setEventDates();
expect(result.error).toEqual(MISSING_EVENT);

result = await tournamentEngine.setEventDates({ eventId });
expect(result.success).toEqual(true);

result = await tournamentEngine.setEventDates({ eventId, weekdays: ['invalid'] });
expect(result.error).toEqual(INVALID_VALUES);

result = await tournamentEngine.setEventDates({ eventId, weekdays: [MON, MON] });
expect(result.error).toEqual(INVALID_VALUES);

result = await tournamentEngine.setEventDates({ eventId, weekdays: [MON] });
expect(result.success).toEqual(true);

let activeDates = [
dateStringDaysChange(startDate, -1),
dateStringDaysChange(startDate, 2),
dateStringDaysChange(startDate, 4),
];
result = await tournamentEngine.setEventDates({ eventId, activeDates });
expect(result.error).toEqual(INVALID_DATE);

activeDates = [startDate, dateStringDaysChange(startDate, 2), dateStringDaysChange(startDate, 8)];
result = await tournamentEngine.setEventDates({ eventId, activeDates });
expect(result.error).toEqual(INVALID_DATE);

activeDates = [startDate, dateStringDaysChange(startDate, 2), dateStringDaysChange(startDate, 4)];
result = await tournamentEngine.setEventDates({ eventId, activeDates });
expect(result.success).toEqual(true);
},
);

0 comments on commit 8867131

Please sign in to comment.