Skip to content

Commit

Permalink
Query helpers should invoke fetch before returning w/ promise (refs #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Feb 4, 2024
1 parent 6c71083 commit 24a68f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
29 changes: 19 additions & 10 deletions app/analytics/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest'

import { AnalyticsEngineAPI } from "./query";

// mock out fetch
global.fetch = vi.fn();
function createFetchResponse(data: any) {
return {
ok: true,
Expand All @@ -13,14 +11,16 @@ function createFetchResponse(data: any) {

describe("AnalyticsEngineAPI", () => {
const api = new AnalyticsEngineAPI("test_account_id_abc123", "test_api_token_def456");
const fetch = global.fetch as any;
let fetch: any; // todo: figure out how to type this mocked fetch

beforeEach(() => {
fetch = global.fetch = vi.fn();
vi.useFakeTimers()
});

afterEach(() => {
vi.useRealTimers()
vi.useRealTimers();
vi.restoreAllMocks();
});

describe("query", () => {
Expand Down Expand Up @@ -180,8 +180,11 @@ describe("AnalyticsEngineAPI", () => {
}))
}));

const result = await api.getCounts("example.com", 7);
expect(result).toEqual({
const result = api.getCounts("example.com", 7);

// verify fetch was invoked before awaiting result
expect(fetch).toHaveBeenCalled();
expect(await result).toEqual({
views: 6,
visits: 3,
visitors: 1,
Expand Down Expand Up @@ -210,8 +213,11 @@ describe("AnalyticsEngineAPI", () => {
}))
}));

const result = await api.getVisitorCountByColumn("example.com", "country", 7);
expect(result).toEqual([
const result = api.getVisitorCountByColumn("example.com", "country", 7);

// verify fetch was invoked before awaiting result
expect(fetch).toHaveBeenCalled();
expect(await result).toEqual([
["CA", 3],
["US", 2],
["GB", 1],
Expand Down Expand Up @@ -243,8 +249,11 @@ describe("AnalyticsEngineAPI", () => {
}))
}));

const result = await api.getSitesOrderedByHits(7);
expect(result).toEqual([

const result = api.getSitesOrderedByHits(7);
// verify fetch was invoked before awaiting result
expect(fetch).toHaveBeenCalled();
expect(await result).toEqual([
["example.com", 130],
["foo.com", 100],
["test.dev", 90],
Expand Down
14 changes: 10 additions & 4 deletions app/analytics/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ export class AnalyticsEngineAPI {
GROUP BY _bucket
ORDER BY _bucket ASC`;

const queryResult = this.query(query);
const returnPromise = new Promise<any>((resolve, reject) => (async () => {
const response = await this.query(query);
const response = await queryResult;

if (!response.ok) {
reject(response.statusText);
Expand Down Expand Up @@ -217,8 +218,10 @@ export class AnalyticsEngineAPI {
AND ${siteIdColumn} = '${siteId}'
GROUP BY isVisitor, isVisit
ORDER BY isVisitor, isVisit ASC`;

const queryResult = this.query(query);
const returnPromise = new Promise<AnalyticsCountResult>((resolve, reject) => (async () => {
const response = await this.query(query);
const response = await queryResult;

if (!response.ok) {
reject(response.statusText);
Expand Down Expand Up @@ -265,8 +268,9 @@ export class AnalyticsEngineAPI {
ORDER BY count DESC
LIMIT ${limit}`;

const queryResult = this.query(query);
const returnPromise = new Promise<any>((resolve, reject) => (async () => {
const response = await this.query(query);
const response = await queryResult;

if (!response.ok) {
reject(response.statusText);
Expand Down Expand Up @@ -319,8 +323,10 @@ export class AnalyticsEngineAPI {
ORDER BY count DESC
LIMIT ${limit}
`;

const queryResult = this.query(query);
const returnPromise = new Promise<any>((resolve, reject) => (async () => {
const response = await this.query(query);
const response = await queryResult;

if (!response.ok) {
reject(response.statusText);
Expand Down

0 comments on commit 24a68f5

Please sign in to comment.