Skip to content

Commit

Permalink
Upgrade latest Remix and enable all future flags
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Dec 19, 2024
1 parent 3834ac5 commit 8c46d07
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 239 deletions.
9 changes: 4 additions & 5 deletions app/__tests__/root.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @vitest-environment jsdom
import { json } from "@remix-run/node";
import { beforeAll, afterEach, describe, expect, test, vitest } from "vitest";
import "vitest-dom/extend-expect";
import { render, waitFor, screen, cleanup } from "@testing-library/react";
Expand All @@ -17,11 +16,11 @@ describe("Root", () => {

test("renders without crashing", async () => {
function loader() {
return json({
return {
version: "ABC123",
origin: "http://example.com",
url: "http://example.com/path",
});
};
}

const RemixStub = createRemixStub([
Expand Down Expand Up @@ -75,11 +74,11 @@ describe("Layout", () => {

test("renders with provided route data", async () => {
function loader() {
return json({
return {
version: "v1.2.3",
origin: "test.counterscale.dev",
url: "https://test.counterscale.dev/",
});
};
}

const RemixStub = createRemixStub([
Expand Down
10 changes: 3 additions & 7 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/// <reference types="vite/client" />
import styles from "./globals.css?url";
import {
json,
LoaderFunctionArgs,
type LinksFunction,
} from "@remix-run/cloudflare";
import { LoaderFunctionArgs, type LinksFunction } from "@remix-run/cloudflare";

import {
Links,
Expand All @@ -19,11 +15,11 @@ export const links: LinksFunction = () => [{ rel: "stylesheet", href: styles }];

export const loader = ({ context, request }: LoaderFunctionArgs) => {
const url = new URL(request.url);
return json({
return {

Check warning on line 18 in app/root.tsx

View check run for this annotation

Codecov / codecov/patch

app/root.tsx#L18

Added line #L18 was not covered by tests
version: context.cloudflare?.env?.CF_PAGES_COMMIT_SHA,
origin: url.origin,
url: request.url,
});
};

Check warning on line 22 in app/root.tsx

View check run for this annotation

Codecov / codecov/patch

app/root.tsx#L22

Added line #L22 was not covered by tests
};

export const Layout = ({ children = [] }: { children: React.ReactNode }) => {
Expand Down
3 changes: 3 additions & 0 deletions app/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { flatRoutes } from "@remix-run/fs-routes";

export default flatRoutes();

Check warning on line 3 in app/routes.ts

View check run for this annotation

Codecov / codecov/patch

app/routes.ts#L3

Added line #L3 was not covered by tests
112 changes: 61 additions & 51 deletions app/routes/__tests__/dashboard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
import { json, LoaderFunctionArgs } from "@remix-run/node";
import { LoaderFunctionArgs } from "@remix-run/node";
import {
vi,
test,
Expand Down Expand Up @@ -97,19 +97,24 @@ describe("Dashboard route", () => {
}),
);

const response = await loader({
...getDefaultContext(),
// @ts-expect-error we don't need to provide all the properties of the request object
request: {
url: "http://localhost:3000/dashboard", // no site query param
},
});
try {
await loader({
...getDefaultContext(),
// @ts-expect-error we don't need to provide all the properties of the request object
request: {
url: "http://localhost:3000/dashboard", // no site query param
},
});
} catch (error) {
expect(error).toBeInstanceOf(Response);
const response = error as Response;

// expect redirect
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe(
"http://localhost:3000/dashboard?site=test-siteid",
);
// expect redirect
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe(
"http://localhost:3000/dashboard?site=test-siteid",
);
}
});

test("redirects to ?site= if no siteId is provided via query string / no site data", async () => {
Expand All @@ -120,19 +125,24 @@ describe("Dashboard route", () => {
}),
);

const response = await loader({
...getDefaultContext(),
// @ts-expect-error we don't need to provide all the properties of the request object
request: {
url: "http://localhost:3000/dashboard", // no site query param
},
});
try {
await loader({
...getDefaultContext(),
// @ts-expect-error we don't need to provide all the properties of the request object
request: {
url: "http://localhost:3000/dashboard", // no site query param
},
});
} catch (error) {
expect(error).toBeInstanceOf(Response);
const response = error as Response;

// expect redirect
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe(
"http://localhost:3000/dashboard?site=",
);
// expect redirect
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe(
"http://localhost:3000/dashboard?site=",
);
}
});

test("assembles data returned from CF API", async () => {
Expand All @@ -153,7 +163,7 @@ describe("Dashboard route", () => {
},
});

const json = await response.json();
const json = await response;

expect(json).toEqual({
filters: {},
Expand All @@ -179,7 +189,7 @@ describe("Dashboard route", () => {
},
});

const json = await response.json();
const json = await response;

expect(json).toEqual({
filters: {},
Expand All @@ -193,11 +203,11 @@ describe("Dashboard route", () => {

test("renders when no data", async () => {
function loader() {
return json({
return {
siteId: "@unknown",
sites: [],
intervalType: "day",
});
};
}

const RemixStub = createRemixStub([
Expand All @@ -209,46 +219,46 @@ describe("Dashboard route", () => {
{
path: "/resources/timeseries",
loader: () => {
return json({ chartData: [] });
return { chartData: [] };
},
},
{
path: "/resources/stats",
loader: () => {
return json({
return {
views: 0,
visitors: 0,
});
};
},
},
{
path: "/resources/paths",
loader: () => {
return json({ countsByProperty: [] });
return { countsByProperty: [] };
},
},
{
path: "/resources/referrer",
loader: () => {
return json({ countsByProperty: [] });
return { countsByProperty: [] };
},
},
{
path: "/resources/browser",
loader: () => {
return json({ countsByProperty: [] });
return { countsByProperty: [] };
},
},
{
path: "/resources/country",
loader: () => {
return json({ countsByProperty: [] });
return { countsByProperty: [] };
},
},
{
path: "/resources/device",
loader: () => {
return json({ countsByProperty: [] });
return { countsByProperty: [] };
},
},
],
Expand Down Expand Up @@ -288,7 +298,7 @@ describe("Dashboard route", () => {

test("renders with valid data", async () => {
function loader() {
return json({ ...defaultMockedLoaderJson });
return { ...defaultMockedLoaderJson };
}

const RemixStub = createRemixStub([
Expand All @@ -300,76 +310,76 @@ describe("Dashboard route", () => {
{
path: "/resources/stats",
loader: () => {
return json({
return {
views: 2133,
visitors: 33,
});
};
},
},
{
path: "/resources/timeseries",
loader: () => {
return json({});
return {};
},
},
{
path: "/resources/paths",
loader: () => {
return json({
return {
countsByProperty: [
["/", 100],
["/about", 80],
["/contact", 60],
],
});
};
},
},
{
path: "/resources/referrer",
loader: () => {
return json({
return {
countsByProperty: [
["google.com", 100],
["facebook.com", 80],
["twitter.com", 60],
],
});
};
},
},
{
path: "/resources/browser",
loader: () => {
return json({
return {
countsByProperty: [
["Chrome", 100],
["Safari", 80],
["Firefox", 60],
],
});
};
},
},
{
path: "/resources/country",
loader: () => {
return json({
return {
countsByProperty: [
["United States", 100],
["Canada", 80],
["United Kingdom", 60],
],
});
};
},
},
{
path: "/resources/device",
loader: () => {
return json({
return {
countsByProperty: [
["Desktop", 100],
["Mobile", 80],
["Tablet", 60],
],
});
};
},
},
],
Expand Down
5 changes: 1 addition & 4 deletions app/routes/__tests__/resources.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ describe("Resources/Browser route", () => {
},
});

// expect redirect
expect(response.status).toBe(200);

const json = await response.json();
const json = await response;
expect(json).toEqual({
countsByProperty: [
["Chrome", 5],
Expand Down
5 changes: 1 addition & 4 deletions app/routes/__tests__/resources.country.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ describe("Resources/Country route", () => {
},
});

// expect redirect
expect(response.status).toBe(200);

const json = await response.json();
const json = await response;
expect(json).toEqual({
countsByProperty: [
[["CA", "Canada"], 5],
Expand Down
5 changes: 1 addition & 4 deletions app/routes/__tests__/resources.device.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ describe("Resources/Device route", () => {
},
});

// expect redirect
expect(response.status).toBe(200);

const json = await response.json();
const json = await response;
expect(json).toEqual({
countsByProperty: [
["Android", 5],
Expand Down
3 changes: 1 addition & 2 deletions app/routes/__tests__/resources.paths.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ describe("Resources/Paths route", () => {
});

// expect redirect
expect(response.status).toBe(200);
expect(fetch).toHaveBeenCalledTimes(2);

const json = await response.json();
const json = await response;

expect(json).toEqual({
countsByProperty: [
Expand Down
3 changes: 1 addition & 2 deletions app/routes/__tests__/resources.referrer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ describe("Resources/Referrer route", () => {
});

// expect redirect
expect(response.status).toBe(200);
expect(fetch).toHaveBeenCalledTimes(2);

const json = await response.json();
const json = await response;
expect(json).toEqual({
countsByProperty: [
["/", 1, 6],
Expand Down
Loading

0 comments on commit 8c46d07

Please sign in to comment.