-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathorganizations-leaderboard-data-source.test.ts
More file actions
65 lines (53 loc) · 2.22 KB
/
organizations-leaderboard-data-source.test.ts
File metadata and controls
65 lines (53 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
describe, test, expect, vi, beforeEach
} from 'vitest';
import {DateTime} from "luxon";
import {mockTimeseries} from '../../mocks/tinybird-organizations-leaderboard-response.mock';
import type {OrganizationsLeaderboardResponse} from "~~/server/data/tinybird/organizations-leaderboard-source";
const mockFetchFromTinybird = vi.fn();
describe('Organizations Leaderboard Data Source', () => {
beforeEach(() => {
mockFetchFromTinybird.mockClear();
// Here be dragons! vi.doMock is not hoisted, and thus it is executed after the original import statement.
// This means that the import for tinybird.ts inside active-organizations-data-source.ts would still be used,
// and thus not mocked. This means we need to import the module again after the mock is set, whenever we want to
// use it.
vi.doMock(import("./tinybird"), () => ({
fetchFromTinybird: mockFetchFromTinybird,
}));
})
test('should fetch organizations leaderboard data with correct parameters', async () => {
// We have to import this here again because vi.doMock is not hoisted. See the explanation in beforeEach().
const {fetchOrganizationsLeaderboard} = await import("~~/server/data/tinybird/organizations-leaderboard-source");
mockFetchFromTinybird.mockResolvedValue(mockTimeseries);
const startDate = DateTime.utc(2024, 3, 20);
const endDate = DateTime.utc(2025, 3, 20);
const filter = {
project: 'the-linux-kernel-organization',
startDate,
endDate
};
const result = await fetchOrganizationsLeaderboard(filter);
expect(mockFetchFromTinybird).toHaveBeenCalledWith(
'/v0/pipes/organizations_leaderboard.json',
filter
);
const expectedResult: OrganizationsLeaderboardResponse = {
meta: {
offset: 0,
limit: 10,
total: 10
},
data: mockTimeseries.data.map((item) => ({
logo: item.logo,
name: item.displayName,
contributions: item.contributionCount,
contributionValue: 0,
percentage: item.contributionPercentage,
website: ''
}))
};
expect(result).toEqual(expectedResult);
});
// TODO: Add checks for invalid dates, invalid data, sql injections, and other edge cases.
});