Skip to content

Commit

Permalink
Add test for getViewsByGroupedInterval + hourly
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Jan 14, 2024
1 parent 0738479 commit d5574ae
Showing 1 changed file with 60 additions and 10 deletions.
70 changes: 60 additions & 10 deletions app/analytics/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function createFetchResponse(data: any) {
}

describe("AnalyticsEngineAPI", () => {
const api = new AnalyticsEngineAPI("abc123", "def456");
const fetch = global.fetch as any;

beforeEach(() => {
vi.useFakeTimers()
Expand All @@ -22,11 +24,7 @@ describe("AnalyticsEngineAPI", () => {
});

describe("getViewsGroupedByInterval", () => {
test("should return an array of tuples representing [timestamp, count]", async () => {
const api = new AnalyticsEngineAPI("abc123", "def456");


const fetch = global.fetch as any;
test("should return an array of [timestamp, count] tuples grouped by day", async () => {
fetch.mockResolvedValue(new Promise(resolve => {
resolve(createFetchResponse({
data: [
Expand All @@ -47,7 +45,7 @@ describe("AnalyticsEngineAPI", () => {
}))
}));

vi.setSystemTime(new Date("2024-01-18 00:00:00").getTime());
vi.setSystemTime(new Date("2024-01-18 05:33:02").getTime());

const result1 = await api.getViewsGroupedByInterval("example.com", "DAY", 7);

Expand All @@ -59,6 +57,7 @@ describe("AnalyticsEngineAPI", () => {
["2024-01-15 00:00:00", 0],
["2024-01-16 00:00:00", 2],
["2024-01-17 00:00:00", 1],
["2024-01-18 00:00:00", 0],
]);

expect(await api.getViewsGroupedByInterval("example.com", "DAY", 5))
Expand All @@ -70,16 +69,67 @@ describe("AnalyticsEngineAPI", () => {
["2024-01-15 00:00:00", 0],
["2024-01-16 00:00:00", 2],
["2024-01-17 00:00:00", 1],
["2024-01-18 00:00:00", 0],
]);

});
});

test("should return an array of [timestamp, count] tuples grouped by hour", async () => {
fetch.mockResolvedValue(new Promise(resolve => {
resolve(createFetchResponse({
data: [
{
count: 3,
// note: intentionally sparse data (data for some timestamps missing)
bucket: "2024-01-17 11:00:00",
},
{
count: 2,
bucket: "2024-01-17 14:00:00"
},
{
count: 1,
bucket: "2024-01-17 16:00:00"
}
]
}))
}));

vi.setSystemTime(new Date("2024-01-18 05:33:02").getTime());

const result1 = await api.getViewsGroupedByInterval("example.com", "HOUR", 1);

expect(result1).toEqual([
['2024-01-17 05:00:00', 0],
['2024-01-17 06:00:00', 0],
['2024-01-17 07:00:00', 0],
['2024-01-17 08:00:00', 0],
['2024-01-17 09:00:00', 0],
['2024-01-17 10:00:00', 0],
['2024-01-17 11:00:00', 3],
['2024-01-17 12:00:00', 0],
['2024-01-17 13:00:00', 0],
['2024-01-17 14:00:00', 2],
['2024-01-17 15:00:00', 0],
['2024-01-17 16:00:00', 1],
['2024-01-17 17:00:00', 0],
['2024-01-17 18:00:00', 0],
['2024-01-18 00:00:00', 0],
['2024-01-18 01:00:00', 0],
['2024-01-18 02:00:00', 0],
['2024-01-18 03:00:00', 0],
['2024-01-18 04:00:00', 0],
['2024-01-18 05:00:00', 0],
['2024-01-18 19:00:00', 0],
['2024-01-18 20:00:00', 0],
['2024-01-18 21:00:00', 0],
['2024-01-18 22:00:00', 0],
['2024-01-18 23:00:00', 0],
]);
});

describe("getCounts", () => {
test("should return an object with view, visit, and visitor counts", async () => {
const api = new AnalyticsEngineAPI("abc123", "def456");

const fetch = global.fetch as any;
fetch.mockResolvedValue(new Promise(resolve => {
resolve(createFetchResponse({
data: [
Expand Down

0 comments on commit d5574ae

Please sign in to comment.