Skip to content

Commit

Permalink
add api tests in build and basic dev
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Aug 8, 2023
1 parent 4ad14d7 commit e00dd9c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 140 deletions.
88 changes: 0 additions & 88 deletions test/api-hmr.test.ts

This file was deleted.

34 changes: 8 additions & 26 deletions test/basic-dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,7 @@ test.describe("rendering", () => {

test.beforeAll(async () => {
fixture = await createDevFixture({
files: {
"app/root.tsx": js`
import { useState } from "react";
export default function App({ assets }) {
const [count, setCount] = useState(0);
return (
<html lang="en">
<head>
<link rel="icon" href="/favicon.ico" />
{assets}
</head>
<body>
<section>
<h1 data-test-id="content">Hello from Vinxi</h1>
<button data-test-id="button" onClick={() => setCount(count + 1)}>
Click me
</button>
<span data-test-id="count">{count}</span>
</section>
</body>
</html>
);
}
`,
},
files: {},
});

appFixture = await fixture.createServer();
Expand Down Expand Up @@ -88,4 +63,11 @@ test.describe("rendering", () => {
prettyHtml(`<span data-test-id="count">1</span>`),
);
});

test("api", async () => {
let res = await fixture.requestDocument("/api/hello");
expect(res.status).toBe(200);
expect(res.headers.get("Content-Type")).toBe("text/html");
expect(await res.text()).toBe("Hello world");
});
});
34 changes: 8 additions & 26 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,7 @@ test.describe("rendering", () => {

test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.tsx": js`
import { useState } from "react";
export default function App({ assets }) {
const [count, setCount] = useState(0);
return (
<html lang="en">
<head>
<link rel="icon" href="/favicon.ico" />
{assets}
</head>
<body>
<section>
<h1 data-test-id="content">Hello from Vinxi</h1>
<button data-test-id="button" onClick={() => setCount(count + 1)}>
Click me
</button>
<span data-test-id="count">{count}</span>
</section>
</body>
</html>
);
}
`,
},
files: {},
});

appFixture = await fixture.createServer();
Expand Down Expand Up @@ -84,4 +59,11 @@ test.describe("rendering", () => {
prettyHtml(`<span data-test-id="count">1</span>`),
);
});

test("api", async () => {
let res = await fixture.requestDocument("/api/hello");
expect(res.status).toBe(200);
expect(res.headers.get("Content-Type")).toBe("text/html");
expect(await res.text()).toBe("Hello world");
});
});
41 changes: 41 additions & 0 deletions test/hmr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,45 @@ test.describe("rendering", () => {
prettyHtml(`<button data-test-id="button">Click me again</button>`),
);
});

test("hmr api", async () => {
let res = await fixture.requestDocument("/api/hello");
expect(res.status).toBe(200);
expect(res.headers.get("Content-Type")).toBe("text/html");
expect(await res.text()).toBe("Hello world");

await fixture.updateFile(
"app/api/hello.ts",
js`export default function handler(event) {
return "Hello world too";
}`,
);

await new Promise((r) => setTimeout(r, 1000));

res = await fixture.requestDocument("/api/hello");
expect(res.status).toBe(200);
expect(res.headers.get("Content-Type")).toBe("text/html");
expect(await res.text()).toBe("Hello world too");

await fixture.updateFile(
"app/api/new.ts",
js`export default function handler(event) {
return "Hello new";
}`,
);

await new Promise((r) => setTimeout(r, 1000));

res = await fixture.requestDocument("/api/new");
expect(res.status).toBe(200);
expect(res.headers.get("Content-Type")).toBe("text/html");
expect(await res.text()).toBe("Hello new");

await fixture.deleteFile("app/api/new.ts");

await new Promise((r) => setTimeout(r, 1000));
res = await fixture.requestDocument("/api/new");
expect(res.status).toBe(404);
});
});

0 comments on commit e00dd9c

Please sign in to comment.