From 4ee518f648581022afef91307d4fa772cc4b2a31 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Sun, 14 Jan 2024 09:07:38 -0500 Subject: [PATCH] Add cursory getSitesOrderedByHits test --- app/analytics/query.test.ts | 33 +++++++++++++++++++++++++++++++++ app/analytics/query.ts | 2 +- app/routes/dashboard.tsx | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/analytics/query.test.ts b/app/analytics/query.test.ts index 379dbfa3..fc8194da 100644 --- a/app/analytics/query.test.ts +++ b/app/analytics/query.test.ts @@ -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], + ]); + }); + }); }); \ No newline at end of file diff --git a/app/analytics/query.ts b/app/analytics/query.ts index 480d8ea4..e16d6906 100644 --- a/app/analytics/query.ts +++ b/app/analytics/query.ts @@ -290,7 +290,7 @@ export class AnalyticsEngineAPI { return this.getVisitorCountByColumn(siteId, 'deviceModel', sinceDays); } - async getSitesByHits(sinceDays: number, limit?: number): Promise { + async getSitesOrderedByHits(sinceDays: number, limit?: number): Promise { // defaults to 1 day if not specified const interval = sinceDays || 1; limit = limit || 10; diff --git a/app/routes/dashboard.tsx b/app/routes/dashboard.tsx index 6c5f3a68..e3519bcf 100644 --- a/app/routes/dashboard.tsx +++ b/app/routes/dashboard.tsx @@ -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