|
| 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 | +}); |
0 commit comments