Skip to content

Commit 627a19b

Browse files
authored
chore: Run jumble integration tests as Deno tests (#537)
chore: Run jumble integration tests as Deno tests, so that we can add more than 1 test
1 parent 207f142 commit 627a19b

File tree

5 files changed

+120
-84
lines changed

5 files changed

+120
-84
lines changed

typescript/packages/jumble/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"build": "deno run -A --node-modules-dir=auto npm:vite build",
55
"preview": "deno run -A --node-modules-dir=auto npm:vite preview",
66
"test": "echo 'No tests to run.'",
7-
"integration": "deno run -A ./integration/smoke-test.ts"
7+
"integration": "deno test -A ./integration/*.test.ts"
88
},
99
"imports": {
1010
"@/": "./src/",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { Browser, launch, Page } from "@astral/astral";
2+
import { assert } from "@std/assert";
3+
import {
4+
afterAll,
5+
afterEach,
6+
beforeAll,
7+
beforeEach,
8+
describe,
9+
it,
10+
} from "@std/testing/bdd";
11+
import {
12+
addCharm,
13+
inspectCharm,
14+
login,
15+
sleep,
16+
waitForSelectorWithText,
17+
} from "./utils.ts";
18+
19+
const TOOLSHED_API_URL = Deno.env.get("TOOLSHED_API_URL") ??
20+
"http://localhost:8000/";
21+
const FRONTEND_URL = Deno.env.get("FRONTEND_URL") ?? "http://localhost:5173/";
22+
const HEADLESS = true;
23+
24+
console.log(`TOOLSHED_API_URL=${TOOLSHED_API_URL}`);
25+
console.log(`FRONTEND_URL=${FRONTEND_URL}`);
26+
27+
describe("integration", () => {
28+
let browser: Browser | void = undefined;
29+
let page: Page | void = undefined;
30+
let testCharm: { charmId: string; space: string } | void = undefined;
31+
32+
beforeAll(async () => {
33+
testCharm = await addCharm(TOOLSHED_API_URL);
34+
console.log(`Charm added`, testCharm);
35+
browser = await launch({ headless: HEADLESS });
36+
});
37+
beforeEach(async () => {
38+
console.log(`Waiting to open website at ${FRONTEND_URL}`);
39+
page = await browser!.newPage(FRONTEND_URL);
40+
console.log(`Opened website at ${FRONTEND_URL}`);
41+
await login(page);
42+
});
43+
afterEach(async () => {
44+
await page!.close();
45+
});
46+
afterAll(async () => {
47+
await browser!.close();
48+
});
49+
50+
it("renders a new charm", async () => {
51+
assert(page);
52+
assert(testCharm);
53+
const anchor = await page.waitForSelector("nav a");
54+
assert(
55+
(await anchor.innerText()) === "common-knowledge",
56+
"Logged in and Common Knowledge title renders",
57+
);
58+
59+
await page.goto(`${FRONTEND_URL}${testCharm.space}/${testCharm.charmId}`);
60+
console.log(`Waiting for charm to render`);
61+
62+
await waitForSelectorWithText(
63+
page,
64+
"a[aria-current='charm-title']",
65+
"Simple Value: 1",
66+
);
67+
console.log("Charm rendered.");
68+
69+
console.log("Clicking button");
70+
// Sometimes clicking this button throws:
71+
// https://jsr.io/@astral/astral/0.5.2/src/element_handle.ts#L192
72+
// As if the reference was invalidated by a spurious re-render between
73+
// getting an element handle, and clicking it.
74+
await sleep(1000);
75+
const button = await page.waitForSelector(
76+
"div[aria-label='charm-content'] button",
77+
);
78+
await button.click();
79+
80+
console.log("Checking if title changed");
81+
await waitForSelectorWithText(
82+
page,
83+
"a[aria-current='charm-title']",
84+
"Simple Value: 2",
85+
);
86+
console.log("Title changed");
87+
88+
console.log("Inspecting charm to verify updates propagated from browser.");
89+
const charm = await inspectCharm(
90+
TOOLSHED_API_URL,
91+
testCharm.space,
92+
testCharm.charmId,
93+
);
94+
console.log("Charm:", charm);
95+
assert(charm.includes("Simple Value: 2"), "Charm updates propagated.");
96+
});
97+
98+
// Placeholder test ensuring browser can be used
99+
// across multiple tests (replace when we have more integration tests!)
100+
it("[placeholder]", () => {
101+
assert(page);
102+
assert(testCharm);
103+
});
104+
});

typescript/packages/jumble/integration/smoke-test.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

typescript/packages/jumble/integration/utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ export const sleep = (ms: number) =>
1212
new Promise((resolve) => setTimeout(resolve, ms));
1313

1414
export const login = async (page: Page) => {
15-
// First, see if any credential data is
15+
// Wait a second :(
16+
// See if #user-avatar is rendered
17+
// Check if we're already logged in
18+
await sleep(1000);
19+
const avatar = await page.$("#user-avatar");
20+
if (avatar) {
21+
console.log("Already logged in");
22+
return;
23+
}
24+
25+
// If not logged in, see if any credential data is
1626
// persisting. If so, destroy local data.
1727
let buttons = await page.$$("button");
1828
for (const button of buttons) {
@@ -21,6 +31,9 @@ export const login = async (page: Page) => {
2131
}
2232
}
2333

34+
// Try log in
35+
console.log("Logging in");
36+
2437
// Click the first button, "register"
2538
let button = await page.$("button");
2639
await button!.click();

typescript/packages/jumble/src/components/User.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function User() {
4343
};
4444
return (
4545
<div
46+
id="user-avatar"
4647
onClick={clearAuthentication}
4748
style={styles}
4849
className="relative group flex max-w-xs items-center rounded-full bg-gray-800 text-sm focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800 focus:outline-hidden cursor-pointer"

0 commit comments

Comments
 (0)