Skip to content

Commit

Permalink
Improve UT coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
matushorvath committed Oct 3, 2023
1 parent 5b30b6c commit 39b6231
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/leaderboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ const onStop = async (year, day, part, name) => {
// Use the year and day parameter to update a single leaderboard
const result = await updateLeaderboards({ year, day });

console.debug(result);

// Return true if the user was invited to the channel for requested day
return result.sent.any(r => r.aocUser === name && r.year === year && r.day === day);
return result.sent.some(r => r.aocUser === name && r.year === year && r.day === day);
};

const updateLeaderboards = async (selection = {}) => {
Expand Down
35 changes: 34 additions & 1 deletion unit-tests/leaderboards.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { handler, updateLeaderboards } = require('../src/leaderboards');
const { handler, onStop, updateLeaderboards } = require('../src/leaderboards');

const invites = require('../src/invites');
jest.mock('../src/invites');
Expand All @@ -25,6 +25,39 @@ beforeEach(() => {
logs.logActivity.mockReset();
});

describe('onStop', () => {
test('triggers leaderboard update', async () => {
years.getYears.mockResolvedValueOnce(new Set());

await expect(onStop(1945, 11, 2, 'sOmE oNe')).resolves.toBe(false);
});

test('reports invitation sent to this user', async () => {
years.getYears.mockResolvedValueOnce(new Set([1945]));

network.getLeaderboard.mockResolvedValueOnce({ lEaDeRbOaRd2020: true });
invites.processInvites.mockResolvedValueOnce({ sent: [{ aocUser: 'sOmE oNe', year: 1945, day: 11 }], failed: [] });
boardPublish.publishBoards.mockResolvedValueOnce({ created: [], updated: [] });

await expect(onStop(1945, 11, 2, 'sOmE oNe')).resolves.toBe(true);
});

test('reports invitation not sent to this user', async () => {
years.getYears.mockResolvedValueOnce(new Set([1945]));

network.getLeaderboard.mockResolvedValueOnce({ lEaDeRbOaRd2020: true });
invites.processInvites.mockResolvedValueOnce({ sent: [{ aocUser: 'aNoThEr OnE', year: 1945, day: 11 }], failed: [] });
boardPublish.publishBoards.mockResolvedValueOnce({ created: [], updated: [] });

await expect(onStop(1945, 11, 2, 'sOmE oNe')).resolves.toBe(false);
});

test('fails after an error while updating leaderboards', async () => {
years.getYears.mockRejectedValueOnce(new Error('sOmEeRrOr'));
await expect(onStop(1945, 11, 2, 'sOmE oNe')).rejects.toThrow('sOmEeRrOr');
});
});

describe('leaderboards.handler', () => {
test('succeeds updating the leaderboard', async () => {
years.getYears.mockResolvedValueOnce(new Set([2021, 2020]));
Expand Down

0 comments on commit 39b6231

Please sign in to comment.