Skip to content

Commit

Permalink
Add cursory getSitesOrderedByHits test
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Jan 14, 2024
1 parent e883210 commit 4ee518f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
33 changes: 33 additions & 0 deletions app/analytics/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,37 @@ describe("AnalyticsEngineAPI", () => {
]);
});
});

describe("getSitesOrderedByHits", () => {
test("it should return an array of [siteId, count] tuples", async () => {

// note: getSitesByHits orders by count descending in SQL; since we're mocking
// the HTTP/SQL response, the mocked results are pre-sorted
fetch.mockResolvedValue(new Promise(resolve => {
resolve(createFetchResponse({
data: [
{
siteId: "example.com",
count: 130,
},
{
siteId: "foo.com",
count: 100,
},
{
siteId: "test.dev",
count: 90,
}
]
}))
}));

const result = await api.getSitesOrderedByHits(7);
expect(result).toEqual([
["example.com", 130],
["foo.com", 100],
["test.dev", 90],
]);
});
});
});
2 changes: 1 addition & 1 deletion app/analytics/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export class AnalyticsEngineAPI {
return this.getVisitorCountByColumn(siteId, 'deviceModel', sinceDays);
}

async getSitesByHits(sinceDays: number, limit?: number): Promise<any> {
async getSitesOrderedByHits(sinceDays: number, limit?: number): Promise<any> {
// defaults to 1 day if not specified
const interval = sinceDays || 1;
limit = limit || 10;
Expand Down
2 changes: 1 addition & 1 deletion app/routes/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const loader = async ({ context, request }: LoaderFunctionArgs) => {
interval = 7;
}

const sitesByHits = (await analyticsEngine.getSitesByHits(interval));
const sitesByHits = (await analyticsEngine.getSitesOrderedByHits(interval));

if (!siteId) {
// pick first non-empty site
Expand Down

0 comments on commit 4ee518f

Please sign in to comment.