|
| 1 | +/** |
| 2 | + * The gallery's `sizes` must describe a grid cell, not the viewport: each |
| 3 | + * image renders in one of `columns` cells (two at 640px and below), and a |
| 4 | + * `100vw` estimate lets the browser pick a far larger srcset candidate than |
| 5 | + * the cell needs (#2930). |
| 6 | + */ |
| 7 | +import { experimental_AstroContainer as AstroContainer } from "astro/container"; |
| 8 | +import { describe, expect, test } from "vitest"; |
| 9 | + |
| 10 | +import Gallery from "../../src/components/Gallery.astro"; |
| 11 | + |
| 12 | +const testMediaProviders = [ |
| 13 | + { |
| 14 | + id: "mock-gallery-images", |
| 15 | + name: "Mock Gallery Images", |
| 16 | + capabilities: { list: false, upload: false, delete: false, metadata: false }, |
| 17 | + createProvider: () => ({ |
| 18 | + id: "mock-gallery-images", |
| 19 | + name: "Mock Gallery Images", |
| 20 | + capabilities: { list: false, upload: false, delete: false, metadata: false }, |
| 21 | + getEmbed: (_value: unknown, options: { width?: number; height?: number } = {}) => ({ |
| 22 | + type: "image", |
| 23 | + src: `https://img.example.com/original?w=${options.width ?? "auto"}`, |
| 24 | + getSrc: ({ width, height }: { width?: number; height?: number } = {}) => |
| 25 | + `https://img.example.com/render?w=${width ?? "auto"}&h=${height ?? "auto"}`, |
| 26 | + }), |
| 27 | + }), |
| 28 | + }, |
| 29 | +]; |
| 30 | + |
| 31 | +const providerGlobal = globalThis as typeof globalThis & { |
| 32 | + __emdashTestMediaProviders?: typeof testMediaProviders; |
| 33 | +}; |
| 34 | +providerGlobal.__emdashTestMediaProviders = [ |
| 35 | + ...(providerGlobal.__emdashTestMediaProviders ?? []), |
| 36 | + ...testMediaProviders, |
| 37 | +]; |
| 38 | + |
| 39 | +const locals = { |
| 40 | + emdash: { getPublicMediaUrl: (k: string) => `/_emdash/api/media/file/${k}` }, |
| 41 | +}; |
| 42 | + |
| 43 | +const imgTags = (html: string) => html.match(/<img\b[^>]*>/g) ?? []; |
| 44 | +const attr = (tag: string, name: string) => |
| 45 | + tag.match(new RegExp(`\\b${name}="([^"]*)"`))?.[1]?.replaceAll("&", "&"); |
| 46 | + |
| 47 | +function providerImage(key: string) { |
| 48 | + return { |
| 49 | + _type: "image" as const, |
| 50 | + _key: key, |
| 51 | + asset: { _ref: `provider-${key}`, provider: "mock-gallery-images" }, |
| 52 | + alt: key, |
| 53 | + width: 1600, |
| 54 | + height: 1200, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +async function renderGallery(props: Record<string, unknown>) { |
| 59 | + const container = await AstroContainer.create(); |
| 60 | + return container.renderToString(Gallery, { props, locals }); |
| 61 | +} |
| 62 | + |
| 63 | +describe("Gallery sizes", () => { |
| 64 | + test("provider images are sized to one of three default columns", async () => { |
| 65 | + const html = await renderGallery({ |
| 66 | + node: { _type: "gallery", _key: "g", images: [providerImage("a"), providerImage("b")] }, |
| 67 | + }); |
| 68 | + const tags = imgTags(html); |
| 69 | + |
| 70 | + expect(tags).toHaveLength(2); |
| 71 | + for (const tag of tags) { |
| 72 | + expect(attr(tag, "srcset")).toContain("https://img.example.com/render?w=640"); |
| 73 | + expect(attr(tag, "sizes")).toBe( |
| 74 | + "(max-width: 640px) calc((100vw - 1rem) / 2), calc((100vw - 2rem) / 3)", |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + test("the slot follows the block's column count", async () => { |
| 80 | + const html = await renderGallery({ |
| 81 | + node: { _type: "gallery", _key: "g", columns: 4, images: [providerImage("a")] }, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(attr(imgTags(html)[0]!, "sizes")).toBe( |
| 85 | + "(max-width: 640px) calc((100vw - 1rem) / 2), calc((100vw - 3rem) / 4)", |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + test("locally stored images with dimensions get the same cell estimate", async () => { |
| 90 | + const html = await renderGallery({ |
| 91 | + node: { |
| 92 | + _type: "gallery", |
| 93 | + _key: "g", |
| 94 | + images: [ |
| 95 | + { |
| 96 | + _type: "image", |
| 97 | + _key: "local", |
| 98 | + asset: { _ref: "media-1", url: "/_emdash/api/media/file/local.jpg" }, |
| 99 | + alt: "local", |
| 100 | + width: 1600, |
| 101 | + height: 1200, |
| 102 | + }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + }); |
| 106 | + const tag = imgTags(html)[0]!; |
| 107 | + |
| 108 | + expect(attr(tag, "data-astro-image")).toBe("constrained"); |
| 109 | + expect(attr(tag, "sizes")).toBe( |
| 110 | + "(max-width: 640px) calc((100vw - 1rem) / 2), calc((100vw - 2rem) / 3)", |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + test("a consumer-provided sizes wins", async () => { |
| 115 | + const html = await renderGallery({ |
| 116 | + node: { _type: "gallery", _key: "g", images: [providerImage("a")] }, |
| 117 | + sizes: "(max-width: 640px) 45vw, 220px", |
| 118 | + }); |
| 119 | + |
| 120 | + expect(attr(imgTags(html)[0]!, "sizes")).toBe("(max-width: 640px) 45vw, 220px"); |
| 121 | + }); |
| 122 | +}); |
0 commit comments