Skip to content

Commit

Permalink
Fixed tests and added new for squad statistics whith different modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Afgan0r committed Jul 16, 2024
1 parent 1160382 commit 959c537
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 21 deletions.
158 changes: 143 additions & 15 deletions src/!tests/unit-tests/3 - statistics/calculateSquadStatistics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@ import {
beforeAll(() => { prepareNamesWithMock(); });

describe('Calculation of squad statistics on any non-weekend day should return correct and same results', () => {
const initialDate = dayjs.dayjsUTC('2022-08-15 12:25:37');
const rotationStartDate = initialDate.startOf('year');
let mondayResults: GlobalSquadStatistics[] = [];

for (let dayNumber = 0; dayNumber < 5; dayNumber += 1) {
const date = dayjs.dayjsUTC('2022-08-15 12:25:37').weekday(dayNumber);
const date = initialDate.weekday(dayNumber);

it(`Calculations on ${date.format('dddd')} should be correct`, () => {
jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);
const squadStatistics = calculateSquadStatistics(parsedReplays);
const squadStatistics = calculateSquadStatistics(
parsedReplays,
rotationStartDate,
null,
true,
);

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);
const squadStatisticsWithRotationEndDate = calculateSquadStatistics(
parsedReplays,
rotationStartDate,
date,
true,
);

if (dayNumber === 0) mondayResults = squadStatistics;
Expand All @@ -43,18 +52,26 @@ describe('Calculation of squad statistics on any non-weekend day should return c

describe('Calculation of squad statistics on Friday, after the game, and on Saturday morning should return correct and same results', () => {
const dates = [dayjs.dayjsUTC('2022-08-19 20:04:23'), dayjs.dayjsUTC('2022-08-20 12:00')];
const rotationStartDate = dates[0].startOf('year');
const replays = [...parsedReplays, ...parsedReplaysOnLastFriday];
let statisticsToCompare: GlobalSquadStatistics[] = [];

dates.forEach((date, index) => {
it(`Calculations on ${index === 0 ? 'Friday, after the game,' : 'Saturday morning'} should be correct`, () => {
jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);
const squadStatistics = calculateSquadStatistics(replays);
const squadStatistics = calculateSquadStatistics(
replays,
rotationStartDate,
null,
true,
);

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);
const squadStatisticsWithRotationEndDate = calculateSquadStatistics(
replays,
rotationStartDate,
date,
true,
);

if (index === 0) statisticsToCompare = squadStatistics;
Expand All @@ -73,6 +90,7 @@ describe('Calculation of squad statistics on Saturday, after the game, on Sunday
dayjs.dayjsUTC('2022-08-21').endOf('day'),
dayjs.dayjsUTC('2022-08-22 10:00'),
];
const rotationStartDate = dates[0].startOf('year');
const replays = [
...parsedReplays,
...parsedReplaysOnLastFriday,
Expand All @@ -97,12 +115,19 @@ describe('Calculation of squad statistics on Saturday, after the game, on Sunday

it(`Calculations on ${dayText} should be correct`, () => {
jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);
const squadStatistics = calculateSquadStatistics(replays);
const squadStatistics = calculateSquadStatistics(
replays,
rotationStartDate,
null,
true,
);

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);
const squadStatisticsWithRotationEndDate = calculateSquadStatistics(
replays,
rotationStartDate,
date,
true,
);

if (index === 0) statisticsToCompare = squadStatistics;
Expand All @@ -115,14 +140,25 @@ describe('Calculation of squad statistics on Saturday, after the game, on Sunday
});

test('Calculation with empty replays should return nothing', () => {
const squadStatistics = calculateSquadStatistics([]);
const date = dayjs.dayjsUTC('2024-07-15');

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);

const squadStatistics = calculateSquadStatistics(
[],
date,
null,
true,
);

expect(squadStatistics).toHaveLength(0);
});

test('Squads with less than 5 members should not account', () => {
const initialDate = dayjs.dayjsUTC('2022-08-16').startOf('day');
const rotationStartDate = initialDate.startOf('year');
const replays: PlayersGameResult[] = [{
date: dayjs.dayjsUTC('2022-08-16').startOf('day').toISOString(),
date: initialDate.toISOString(),
missionName: '',
result: [
generatePlayerInfo({ id: 0, name: '[HH]smth1' }),
Expand All @@ -135,21 +171,113 @@ test('Squads with less than 5 members should not account', () => {
],
}];

const squadStatistics = calculateSquadStatistics(replays, dayjs.dayjsUTC('2022-08-20'));
const squadStatistics = calculateSquadStatistics(
replays,
rotationStartDate,
initialDate.add(1, 'day'),
true,
);

expect(squadStatistics).toHaveLength(1);
});

test(getDefaultTestDescription('Calculation of squad statistics with rotationEndDate parameter'), () => {
const date = dayjs.dayjsUTC('2023-08-15');
const rotationEndDate = dayjs.dayjsUTC('2022-08-15').endOf('day');
describe(getDefaultTestDescription('Squad statistics with different modes'), () => {
const getReplays = (date: string): PlayersGameResult => ({
date,
missionName: '',
result: [
generatePlayerInfo({ id: 0, name: '[FNX]Afgan0r' }),
generatePlayerInfo({ id: 1, name: '[FNX]Flashback' }),
generatePlayerInfo({ id: 2, name: '[FNX]Skywalker' }),
generatePlayerInfo({ id: 3, name: '[FNX]Brom' }),
generatePlayerInfo({ id: 4, name: '[FNX]Mecheniy' }),
generatePlayerInfo({ id: 5, name: '[FNX]LOXDOR' }),
generatePlayerInfo({ id: 6, name: '[FNX]T1m' }),
],
});

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);
const rotationStartDate = dayjs.dayjsUTC('2024-07-15');

const squadStatistics = calculateSquadStatistics(
parsedReplays,
rotationEndDate,
const beforeRotationReplays: PlayersGameResult[] = [
getReplays('2024-07-12T18:00:00.000Z'),
getReplays('2024-07-13T18:00:00.000Z'),
];
const afterRotationReplays: PlayersGameResult[] = [
getReplays('2024-07-19T18:00:00.000Z'),
getReplays('2024-07-20T18:00:00.000Z'),
];
const replays = [...beforeRotationReplays, ...afterRotationReplays];

const dates = [
dayjs.dayjsUTC('2024-07-28'),

dayjs.dayjsUTC('2024-07-29'),
dayjs.dayjsUTC('2024-08-04'),

dayjs.dayjsUTC('2024-08-05'),
dayjs.dayjsUTC('2024-08-11'),
dayjs.dayjsUTC('2024-08-15'),
];

const firstDate = dayjs.dayjsUTC('2024-07-22');

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => firstDate);
prepareNamesWithMock();
const squadStatics = calculateSquadStatistics(
replays,
rotationStartDate,
null,
true,
);

expect(squadStatistics).toMatchObject(squadStatisticsOnNonWeekend);
test('Players should have played 2 games on the first date', () => {
expect(squadStatics[0].players[0].totalPlayedGames).toBe(2);
});

dates.forEach(
(date) => {
test(`Squad statistics for ${date.toJSON()} date should return the same result as for ${firstDate.toJSON()} date`, () => {
jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => date);

const squadStatisticsForDate = calculateSquadStatistics(
replays,
rotationStartDate,
null,
true,
);

expect(squadStatisticsForDate).toStrictEqual(squadStatics);
});
},
);

test('Squad statistics for far date should return empty array', () => {
const farDate = dayjs.dayjsUTC('2024-08-18');

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => farDate);

const squadStatisticsForFarDate = calculateSquadStatistics(
replays,
rotationStartDate,
null,
true,
);

expect(squadStatisticsForFarDate).toStrictEqual([]);
});

test('Squad statistics for far date with full rotation mode should return the same result as for the first date', () => {
const farDate = dayjs.dayjsUTC('2024-08-18');

jest.spyOn(dayjs, 'dayjsUTC').mockImplementationOnce(() => farDate);

const squadStatisticsForFarDate = calculateSquadStatistics(
replays,
rotationStartDate,
null,
false,
);

expect(squadStatisticsForFarDate).toStrictEqual(squadStatics);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,70 @@ test('Prepare names changes list snapshot', () => {
describe('getPlayerId func should work correctly', () => {
it('Name change should work correctly', () => {
const playersGameResult = nameChangesTestData;
const rotationStartDate = dayjsUTC(playersGameResult[0].date).startOf('week');

const resultSquadStatistics = calculateSquadStatistics(playersGameResult, dayjsUTC('2023-04-15'));
const resultSquadStatistics = calculateSquadStatistics(
playersGameResult,
rotationStartDate,
dayjsUTC('2023-04-15'),
true,
);

expect(resultSquadStatistics).toMatchSnapshot();
});

it('Name changes sequence should work correctly', () => {
const playersGameResult = nameChangesSequenceTestData;
const rotationStartDate = dayjsUTC(playersGameResult[0].date).startOf('week');

const resultSquadStatistics = calculateSquadStatistics(playersGameResult, dayjsUTC('2023-09-02'));
const resultSquadStatistics = calculateSquadStatistics(
playersGameResult,
rotationStartDate,
dayjsUTC('2023-09-02'),
true,
);

expect(resultSquadStatistics).toMatchSnapshot();
});

it('Name change and then change back to the same name should work correctly', () => {
const playersGameResult = nameChangeAndChangeBackTestData;
const rotationStartDate = dayjsUTC(playersGameResult[0].date).startOf('week');

const resultSquadStatistics = calculateSquadStatistics(playersGameResult, dayjsUTC('2022-11-19'));
const resultSquadStatistics = calculateSquadStatistics(
playersGameResult,
rotationStartDate,
dayjsUTC('2022-11-19'),
true,
);

expect(resultSquadStatistics).toMatchSnapshot();
});

it('Name change collision after name change and before change back should be handled correctly', () => {
const playersGameResult = nameChangeAndChangeBackWithCollisionsTestData;
const rotationStartDate = dayjsUTC(playersGameResult[0].date).startOf('week');

const resultSquadStatistics = calculateSquadStatistics(playersGameResult, dayjsUTC('2023-08-12'));
const resultSquadStatistics = calculateSquadStatistics(
playersGameResult,
rotationStartDate,
dayjsUTC('2023-08-12'),
true,
);

expect(resultSquadStatistics).toMatchSnapshot();
});

it('Name change after squad change should be handled correctly', () => {
const playersGameResult = nameChangeAfterSquadChangeTestData;

const resultSquadStatistics = calculateSquadStatistics(playersGameResult, dayjsUTC('2023-04-15'));
const rotationStartDate = dayjsUTC(playersGameResult[0].date).startOf('week');

const resultSquadStatistics = calculateSquadStatistics(
playersGameResult,
rotationStartDate,
dayjsUTC('2023-04-15'),
true,
);

expect(resultSquadStatistics).toMatchSnapshot();
});
Expand Down

0 comments on commit 959c537

Please sign in to comment.