|
| 1 | +import type { ImageMetadata } from 'astro'; |
1 | 2 | import { expect, test, vi } from 'vitest'; |
2 | | -import { getRouteDataTestContext } from '../test-utils'; |
3 | | -import { generateRouteData } from '../../utils/routing/data'; |
4 | 3 | import { routes } from '../../utils/routing'; |
| 4 | +import { generateRouteData } from '../../utils/routing/data'; |
5 | 5 | import { |
6 | 6 | generateStarlightPageRouteData, |
7 | 7 | type StarlightPageProps, |
8 | 8 | } from '../../utils/starlight-page'; |
| 9 | +import { getRouteDataTestContext } from '../test-utils'; |
9 | 10 |
|
10 | 11 | vi.mock('virtual:starlight/collection-config', async () => |
11 | 12 | (await import('../test-utils')).mockedCollectionConfig() |
@@ -523,3 +524,89 @@ test('generates data with a similar root shape to regular route data', async () |
523 | 524 |
|
524 | 525 | expect(Object.keys(data).sort()).toEqual(Object.keys(starlightPageData).sort()); |
525 | 526 | }); |
| 527 | + |
| 528 | +test('parses an ImageMetadata object successfully', async () => { |
| 529 | + const fakeImportedImage: ImageMetadata = { |
| 530 | + src: '/image-src.png', |
| 531 | + width: 100, |
| 532 | + height: 100, |
| 533 | + format: 'png', |
| 534 | + }; |
| 535 | + const data = await generateStarlightPageRouteData({ |
| 536 | + props: { |
| 537 | + ...starlightPageProps, |
| 538 | + frontmatter: { |
| 539 | + ...starlightPageProps.frontmatter, |
| 540 | + hero: { |
| 541 | + image: { file: fakeImportedImage }, |
| 542 | + }, |
| 543 | + }, |
| 544 | + }, |
| 545 | + context: getRouteDataTestContext(starlightPagePathname), |
| 546 | + }); |
| 547 | + expect(data.entry.data.hero?.image).toBeDefined(); |
| 548 | + // @ts-expect-error — image’s type can be different shapes but we know it’s this one here |
| 549 | + expect(data.entry.data.hero?.image!['file']).toMatchInlineSnapshot(` |
| 550 | + { |
| 551 | + "format": "png", |
| 552 | + "height": 100, |
| 553 | + "src": "/image-src.png", |
| 554 | + "width": 100, |
| 555 | + } |
| 556 | + `); |
| 557 | +}); |
| 558 | + |
| 559 | +test('parses an image that is also a function successfully', async () => { |
| 560 | + const fakeImportedSvg = (() => {}) as unknown as ImageMetadata; |
| 561 | + Object.assign(fakeImportedSvg, { src: '/image-src.svg', width: 100, height: 100, format: 'svg' }); |
| 562 | + const data = await generateStarlightPageRouteData({ |
| 563 | + props: { |
| 564 | + ...starlightPageProps, |
| 565 | + frontmatter: { |
| 566 | + ...starlightPageProps.frontmatter, |
| 567 | + hero: { |
| 568 | + image: { file: fakeImportedSvg }, |
| 569 | + }, |
| 570 | + }, |
| 571 | + }, |
| 572 | + context: getRouteDataTestContext(starlightPagePathname), |
| 573 | + }); |
| 574 | + expect(data.entry.data.hero?.image).toBeDefined(); |
| 575 | + // @ts-expect-error — image’s type can be different shapes but we know it’s this one here |
| 576 | + expect(data.entry.data.hero?.image!['file']).toMatchInlineSnapshot(`[Function]`); |
| 577 | + // @ts-expect-error |
| 578 | + expect(data.entry.data.hero?.image!['file']).toHaveProperty('src'); |
| 579 | + // @ts-expect-error |
| 580 | + expect(data.entry.data.hero?.image!['file']).toHaveProperty('width'); |
| 581 | + // @ts-expect-error |
| 582 | + expect(data.entry.data.hero?.image!['file']).toHaveProperty('height'); |
| 583 | + // @ts-expect-error |
| 584 | + expect(data.entry.data.hero?.image!['file']).toHaveProperty('format'); |
| 585 | +}); |
| 586 | + |
| 587 | +test('fails to parse an image without the expected metadata properties', async () => { |
| 588 | + await expect(() => |
| 589 | + generateStarlightPageRouteData({ |
| 590 | + props: { |
| 591 | + ...starlightPageProps, |
| 592 | + frontmatter: { |
| 593 | + ...starlightPageProps.frontmatter, |
| 594 | + hero: { |
| 595 | + image: { |
| 596 | + // @ts-expect-error intentionally incorrect input |
| 597 | + file: () => {}, |
| 598 | + }, |
| 599 | + }, |
| 600 | + }, |
| 601 | + }, |
| 602 | + context: getRouteDataTestContext(starlightPagePathname), |
| 603 | + }) |
| 604 | + ).rejects.toThrowErrorMatchingInlineSnapshot(` |
| 605 | + "[AstroUserError]: |
| 606 | + Invalid frontmatter props passed to the \`<StarlightPage/>\` component. |
| 607 | + Hint: |
| 608 | + **hero.image**: Did not match union. |
| 609 | + > Expected type \`file | { dark; light } | { html: string }\` |
| 610 | + > Received \`{}\`" |
| 611 | + `); |
| 612 | +}); |
0 commit comments