Skip to content

Commit

Permalink
test: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Oct 4, 2023
1 parent b4809e9 commit fd8464e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "./out/core/index.js",
"main": "./out/core/index.js",
"types": "./out/core/index.d.ts",
"version": "0.0.4",
"version": "0.0.5",
"description": "lightweight and flexible front-end library based on Bun.js for modern web apps",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -64,4 +64,4 @@
"peerDependencies": {
"typescript": "5.2.2"
}
}
}
3 changes: 3 additions & 0 deletions src/__fixtures__/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Home() {
return <div>Hello world</div>;
}
32 changes: 32 additions & 0 deletions src/cli/serve/serve-options.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ describe("CLI: serve", () => {
expect(html).toContain("<h1>Page not found 404</h1>");
});

it("should redirect the home to the correct locale", async () => {
const response = await testRequest(new Request("http://localhost:1234"));
expect(response.status).toBe(301);
expect(response.headers.get("Location")).toBe("/es");
});

it("should redirect the home to the correct locale and trailingSlash", async () => {
globalThis.mockConstants = {
...globalThis.mockConstants,
CONFIG: {
trailingSlash: true,
},
};
const response = await testRequest(new Request("http://localhost:1234/"));
expect(response.status).toBe(301);
expect(response.headers.get("Location")).toBe("/es/");
});

it("should redirect to the correct locale", async () => {
const response = await testRequest(
new Request(`http://localhost:1234/somepage`),
Expand All @@ -121,6 +139,20 @@ describe("CLI: serve", () => {
);
});

it("should redirect with locale and trailingSlash", async () => {
globalThis.mockConstants = {
...globalThis.mockConstants,
CONFIG: {
trailingSlash: true,
},
};
const response = await testRequest(
new Request(`http://localhost:1234/somepage`),
);
expect(response.status).toBe(301);
expect(response.headers.get("Location")).toBe("/es/somepage/");
});

it("should return a page with layout and i18n", async () => {
const response = await testRequest(
new Request(`http://localhost:1234/es/somepage`),
Expand Down
28 changes: 7 additions & 21 deletions src/cli/serve/serve-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,20 @@ export const serveOptions: Serve = {
const isAnAsset = !isHome && fs.existsSync(assetPath);
const i18nRes = isAnAsset ? {} : handleI18n(request);

const isResponseLocationValidRoute = (response: Response) => {
const location = response.headers.get("Location") ?? "";

url.pathname = URL.canParse(location)
? new URL(location).pathname
: location;

const req = extendRequestContext({
originalRequest: request,
finalURL: url.toString(),
});

return pagesRouter.match(req).route || rootRouter.match(req).route;
const isValidRoute = () => {
return (
pagesRouter.match(request).route || rootRouter.match(request).route
);
};

// 404 page
const return404Error = () =>
const error404 = () =>
route404
? responseRenderedPage({ req: request, route: route404, status: 404 })
: new Response("Not found", { status: 404 });

if (i18nRes.response) {
// Redirect to the locale
if (isResponseLocationValidRoute(i18nRes.response))
return i18nRes.response;

return return404Error();
return isValidRoute() ? i18nRes.response : error404();
}

if (i18nRes.pagesRouter && i18nRes.rootRouter) {
Expand All @@ -95,8 +82,7 @@ export const serveOptions: Serve = {
if (!isAnAsset) {
const redirect = redirectTrailingSlash(request);

if (redirect && isResponseLocationValidRoute(redirect)) return redirect;
if (redirect) return return404Error();
if (redirect) return isValidRoute() ? redirect : error404();
}

request.getIP = () => server.requestIP(req);
Expand Down
41 changes: 39 additions & 2 deletions src/utils/extend-request-context/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from "bun:test";
import { describe, it, expect, mock } from "bun:test";
import extendRequestContext from ".";

describe("brisa core", () => {
Expand All @@ -13,7 +13,7 @@ describe("brisa core", () => {
route,
});
expect(requestContext.route).toEqual(route);
expect(requestContext.finalURL).toEqual(request.finalURL);
expect(requestContext.finalURL).toEqual(request.url);
expect(requestContext.context).toBeInstanceOf(Map);
});

Expand All @@ -29,5 +29,42 @@ describe("brisa core", () => {
requestContext.context.set("foo", "bar");
expect(requestContext.context.get("foo")).toBe("bar");
});

it("should work i18n", () => {
const mockT = mock(() => "foo");
const request = new Request("https://example.com");
const route = {
path: "/",
} as any;
const requestContext = extendRequestContext({
originalRequest: request,
route,
i18n: {
locale: "es",
defaultLocale: "en",
locales: ["en", "es"],
t: mockT,
},
});

expect(requestContext.i18n.locale).toBe("es");
expect(requestContext.i18n.defaultLocale).toBe("en");
expect(requestContext.i18n.locales).toEqual(["en", "es"]);
expect(requestContext.i18n.t("some-key")).toBe("foo");
});

it("should be linked with websockets", () => {
globalThis.ws = { send: mock(() => "some message") } as any;
const request = new Request("https://example.com");
const route = {
path: "/",
} as any;
const requestContext = extendRequestContext({
originalRequest: request,
route,
});

expect(requestContext.ws.send()).toBe("some message");
});
});
});
9 changes: 6 additions & 3 deletions src/utils/get-entrypoints/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ describe("utils", () => {
describe("getEntrypoints", () => {
it("should return an array", () => {
const entrypoints = getEntrypoints(pagesDir);
const expected = ["_404.tsx", "somepage.tsx", "user/[username].tsx"].map(
(route) => path.join(pagesDir, route),
);
const expected = [
"_404.tsx",
"somepage.tsx",
"/index.tsx",
"user/[username].tsx",
].map((route) => path.join(pagesDir, route));
expect(entrypoints).toEqual(expected);
});

Expand Down

0 comments on commit fd8464e

Please sign in to comment.