Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/sort-scheduled-subStructure-…
Browse files Browse the repository at this point in the history
…matchUps'
  • Loading branch information
CourtHive committed Aug 22, 2024
2 parents 19e3e2a + 12d1321 commit 80c7187
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/functions/sorters/scheduledSortedMatchUps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function scheduledSortedMatchUps({ schedulingProfile, matchUps = [] }: Sc
// for each time group, sort sub-structure group matchUps by round number
for (const timeKey of Object.keys(timeGroups)) {
const timeGroup = timeGroups[timeKey];
const sortedTimeGroup = timeGroup.sort((a, b) => a['roundNumber'] || 0 - b['roundNumber'] || 0);
const sortedTimeGroup = timeGroup.sort((a, b) => (a['roundNumber'] || 0) - (b['roundNumber'] || 0));
timeGroups[timeKey] = sortedTimeGroup;
}

Expand Down
229 changes: 229 additions & 0 deletions src/tests/functions/scheduledSortedMatchUps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import { scheduledSortedMatchUps } from '@Functions/sorters/scheduledSortedMatchUps';
import { MatchUp } from '@Types/tournamentTypes';
import { it, expect, describe } from 'vitest';

describe('scheduledSortedMatchUps function', () => {
it('can accurately sort matchUps by scheduled date, time, and round order', () => {
const schedulingProfile = [
{
venues: [
{
rounds: [
{ eventId: '1', drawId: '1', structureId: '1', roundNumber: 1, sortOrder: 2 },
{ eventId: '1', drawId: '1', structureId: '1', roundNumber: 2, sortOrder: 1 },
],
},
{
rounds: [
{ eventId: '1', drawId: '1', structureId: '2', roundNumber: 1, sortOrder: 1 },
{ eventId: '1', drawId: '1', structureId: '2', roundNumber: 2, sortOrder: 2 },
],
},
],
},
];

const shuffledMatchUps = [
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '11:00',
},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 2,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '11:00',
},
roundNumber: 2,
},
] as unknown as MatchUp[];

const sortedMatchUps = scheduledSortedMatchUps({ schedulingProfile, matchUps: shuffledMatchUps });

expect(sortedMatchUps).toEqual([
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 2,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '11:00',
},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '11:00',
},
roundNumber: 2,
},
]);
});

it('returns matchUps sorted only by date and time when no scheduling profile is provided', () => {
const schedulingProfile = [];

const shuffledMatchUps = [
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '11:00',
},
roundNumber: 2,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-02',
scheduledTime: '09:00',
},
roundNumber: 1,
},
] as unknown as MatchUp[];

const sortedMatchUps = scheduledSortedMatchUps({ schedulingProfile, matchUps: shuffledMatchUps });

expect(sortedMatchUps).toEqual([
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '11:00',
},
roundNumber: 2,
},
{
schedule: {
scheduledDate: '2022-01-02',
scheduledTime: '09:00',
},
roundNumber: 1,
},
]);
});

it('handles matchUps with no scheduled date or time', () => {
const schedulingProfile = [];

const shuffledMatchUps = [
{
schedule: {},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 2,
},
] as unknown as MatchUp[];

const sortedMatchUps = scheduledSortedMatchUps({ schedulingProfile, matchUps: shuffledMatchUps });

expect(sortedMatchUps).toEqual([
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 2,
},
{
schedule: {},
roundNumber: 1,
},
]);
});

it('sorts matchUps correctly when multiple matchUps have the same date and time', () => {
const schedulingProfile = [
{
venues: [
{
rounds: [
{ eventId: '1', drawId: '1', structureId: '1', roundNumber: 1, sortOrder: 1 },
{ eventId: '1', drawId: '1', structureId: '1', roundNumber: 2, sortOrder: 2 },
],
},
],
},
];

const shuffledMatchUps = [
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 2,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 1,
},
] as unknown as MatchUp[];

const sortedMatchUps = scheduledSortedMatchUps({ schedulingProfile, matchUps: shuffledMatchUps });

expect(sortedMatchUps).toEqual([
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 1,
},
{
schedule: {
scheduledDate: '2022-01-01',
scheduledTime: '10:00',
},
roundNumber: 2,
},
]);
});
});

0 comments on commit 80c7187

Please sign in to comment.