Skip to content

Commit

Permalink
When no site provided on query string, redirect w/ with valid site sp…
Browse files Browse the repository at this point in the history
…ecified (#35)

* If no ?site provided, redirect to the most visited site

* Dashboard loader test when site has no corresponding data

* Remove unnecessary promise layer

* Add test when no site data available
  • Loading branch information
benvinegar authored Feb 5, 2024
1 parent 2bc5c48 commit 0d43f18
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 169 deletions.
187 changes: 82 additions & 105 deletions app/analytics/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ describe("AnalyticsEngineAPI", () => {

describe("query", () => {
test("forms a valid HTTP request query for CF analytics engine", () => {
fetch.mockResolvedValue(
new Promise((resolve) => {
resolve(createFetchResponse({}));
}),
);
fetch.mockResolvedValue(createFetchResponse({}));

api.query("SELECT * FROM web_counter");

Expand All @@ -56,26 +52,22 @@ describe("AnalyticsEngineAPI", () => {
expect(process.env.TZ).toBe("EST");

fetch.mockResolvedValue(
new Promise((resolve) => {
resolve(
createFetchResponse({
data: [
{
count: 3,
// note: intentionally sparse data (data for some timestamps missing)
bucket: "2024-01-13 05:00:00",
},
{
count: 2,
bucket: "2024-01-16 05:00:00",
},
{
count: 1,
bucket: "2024-01-17 05:00:00",
},
],
}),
);
createFetchResponse({
data: [
{
count: 3,
// note: intentionally sparse data (data for some timestamps missing)
bucket: "2024-01-13 05:00:00",
},
{
count: 2,
bucket: "2024-01-16 05:00:00",
},
{
count: 1,
bucket: "2024-01-17 05:00:00",
},
],
}),
);

Expand Down Expand Up @@ -122,26 +114,22 @@ describe("AnalyticsEngineAPI", () => {
expect(process.env.TZ).toBe("EST");

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",
},
],
}),
);
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",
},
],
}),
);

Expand All @@ -151,6 +139,7 @@ describe("AnalyticsEngineAPI", () => {
"example.com",
"HOUR",
1,
"America/New_York",
);

// reminder results are expressed as UTC
Expand Down Expand Up @@ -188,28 +177,24 @@ describe("AnalyticsEngineAPI", () => {
describe("getCounts", () => {
test("should return an object with view, visit, and visitor counts", async () => {
fetch.mockResolvedValue(
new Promise((resolve) => {
resolve(
createFetchResponse({
data: [
{
count: 3,
isVisit: 1,
isVisitor: 0,
},
{
count: 2,
isVisit: 0,
isVisitor: 0,
},
{
count: 1,
isVisit: 0,
isVisitor: 1,
},
],
}),
);
createFetchResponse({
data: [
{
count: 3,
isVisit: 1,
isVisitor: 0,
},
{
count: 2,
isVisit: 0,
isVisitor: 0,
},
{
count: 1,
isVisit: 0,
isVisitor: 1,
},
],
}),
);

Expand All @@ -228,25 +213,21 @@ describe("AnalyticsEngineAPI", () => {
describe("getVisitorCountByColumn", () => {
test("it should map logical columns to schema columns and return an array of [column, count] tuples", async () => {
fetch.mockResolvedValue(
new Promise((resolve) => {
resolve(
createFetchResponse({
data: [
{
blob4: "CA",
count: 3,
},
{
blob4: "US",
count: 2,
},
{
blob4: "GB",
count: 1,
},
],
}),
);
createFetchResponse({
data: [
{
blob4: "CA",
count: 3,
},
{
blob4: "US",
count: 2,
},
{
blob4: "GB",
count: 1,
},
],
}),
);

Expand All @@ -271,25 +252,21 @@ describe("AnalyticsEngineAPI", () => {
// 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,
},
],
}),
);
createFetchResponse({
data: [
{
siteId: "example.com",
count: 130,
},
{
siteId: "foo.com",
count: 100,
},
{
siteId: "test.dev",
count: 90,
},
],
}),
);

Expand Down
Loading

0 comments on commit 0d43f18

Please sign in to comment.