|
| 1 | +// The component's markup is the contract's: for each golden fixture, rendering <Gallery> with the fixture's manifest |
| 2 | +// (served by a stub fetch) and options gives exactly its gallery.html. A manifest that can't be read fails the render, |
| 3 | +// so the site's build fails. |
| 4 | +import { experimental_AstroContainer as AstroContainer } from "astro/container"; |
| 5 | +import { readdirSync, readFileSync } from "node:fs"; |
| 6 | +import { join } from "node:path"; |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import Gallery from "../src/Gallery.astro"; |
| 9 | + |
| 10 | +const fixtures = join(import.meta.dirname, "../../contract/fixtures"); |
| 11 | +const serve = (body: string, status = 200) => async () => new Response(body, { status }); |
| 12 | + |
| 13 | +describe("Gallery", () => { |
| 14 | + for (const name of readdirSync(fixtures).sort()) { |
| 15 | + it(`renders the ${name} fixture's markup`, async () => { |
| 16 | + const dir = join(fixtures, name); |
| 17 | + const container = await AstroContainer.create(); |
| 18 | + const html = await container.renderToString(Gallery, { |
| 19 | + props: { |
| 20 | + ...JSON.parse(readFileSync(join(dir, "options.json"), "utf8")), |
| 21 | + manifest: "https://app.visualci.com/g/acme/web.json", |
| 22 | + fetch: serve(readFileSync(join(dir, "manifest.json"), "utf8")), |
| 23 | + }, |
| 24 | + }); |
| 25 | + expect(html).toContain(readFileSync(join(dir, "gallery.html"), "utf8")); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + it("fails when the manifest can't be read", async () => { |
| 30 | + const container = await AstroContainer.create(); |
| 31 | + await expect( |
| 32 | + container.renderToString(Gallery, { |
| 33 | + props: { manifest: "https://app.visualci.com/g/acme/web.json", attempts: 1, fetch: serve("", 503) }, |
| 34 | + }), |
| 35 | + ).rejects.toThrow("The screenshots gallery couldn't read https://app.visualci.com/g/acme/web.json: HTTP 503"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("fails when the manifest has nothing to show", async () => { |
| 39 | + const container = await AstroContainer.create(); |
| 40 | + await expect( |
| 41 | + container.renderToString(Gallery, { |
| 42 | + props: { manifest: "https://app.visualci.com/g/acme/web.json", fetch: serve('{"groups":[]}') }, |
| 43 | + }), |
| 44 | + ).rejects.toThrow(/can't show/); |
| 45 | + }); |
| 46 | +}); |
0 commit comments