Skip to content

Commit

Permalink
Add test for stats resource route loader
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Nov 19, 2024
1 parent 0d9710d commit 1489ee2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/routes/__tests__/resources.stats.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, test, expect, vi } from "vitest";
import { loader } from "../resources.stats";

describe("resources.stats loader", () => {
test("returns formatted stats from analytics engine", async () => {
const mockGetCounts = vi.fn().mockResolvedValue({
views: 1000,
visits: 500,
visitors: 250,
});

const context = {
analyticsEngine: {
getCounts: mockGetCounts,
},
};

const request = new Request(
"https://example.com/resources/stats?site=test-site&interval=24h&timezone=UTC",
);

const response = await loader({ context, request } as any);

Check warning on line 22 in app/routes/__tests__/resources.stats.test.tsx

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const data = await response.json();

expect(mockGetCounts).toHaveBeenCalledWith(
"test-site",
"24h",
"UTC",
expect.any(Object),
);

expect(data).toEqual({
views: 1000,
visits: 500,
visitors: 250,
});
});
});

0 comments on commit 1489ee2

Please sign in to comment.