1+ import type { CollectionEntry } from 'astro:content'
2+ import { getCollection } from 'astro:content'
13import { beforeEach , describe , expect , test } from 'vitest'
24import { experimental_AstroContainer as AstroContainer } from 'astro/container'
35import { withJsdomEnvironment } from '@test/unit/helpers/litRuntime'
46
5- const existingResource = 'performance-testing-load-models-benchmark-accuracy'
7+ type DownloadFixture = {
8+ directDownloadUrl : string
9+ resource : string
10+ }
11+
12+ const getDownloadResource = ( download : CollectionEntry < 'downloads' > ) : string | null => {
13+ const normalizedFilePath = download . filePath ?. replace ( / \\ / g, '/' )
14+ const filePathMatch = normalizedFilePath ?. match ( / \/ a r t i c l e s \/ ( .+ ) \/ d o w n l o a d \. [ ^ / . ] + $ / )
15+
16+ if ( filePathMatch ?. [ 1 ] ) {
17+ return filePathMatch [ 1 ]
18+ }
19+
20+ const normalizedId = download . id . replace ( / \\ / g, '/' )
21+ const idWithoutDownloadSuffix = normalizedId . replace ( / \/ d o w n l o a d $ / , '' )
22+ return idWithoutDownloadSuffix . split ( '/' ) [ 0 ] ?? null
23+ }
24+
25+ const getDownloadFixture = async ( ) : Promise < DownloadFixture | null > => {
26+ const [ download ] = await getCollection ( 'downloads' )
27+
28+ if ( ! download ) {
29+ return null
30+ }
31+
32+ const resource = getDownloadResource ( download )
33+ const fileName = download . data . fileName ?. trim ( )
34+
35+ if ( ! resource || ! fileName ) {
36+ return null
37+ }
38+
39+ return {
40+ directDownloadUrl : `/downloads/${ fileName } ` ,
41+ resource,
42+ }
43+ }
44+
45+ const downloadFixture = await getDownloadFixture ( )
46+ const existingDownloadTest = downloadFixture ? test : test . skip
647
748describe ( 'Download CallToAction (Astro)' , ( ) => {
849 let container : AstroContainer
@@ -42,12 +83,16 @@ describe('Download CallToAction (Astro)', () => {
4283 } )
4384 } )
4485
45- test ( 'renders direct and landing download URLs for the web component host' , async ( ) => {
86+ existingDownloadTest ( 'renders direct and landing download URLs for the web component host' , async ( ) => {
87+ if ( ! downloadFixture ) {
88+ throw new Error ( 'Expected an existing download fixture' )
89+ }
90+
4691 const Download = ( await import ( '@components/CallToAction/Download/index.astro' ) ) . default
4792
4893 const renderedHtml = await container . renderToString ( Download , {
4994 props : {
50- resource : existingResource ,
95+ resource : downloadFixture . resource ,
5196 } ,
5297 } )
5398
@@ -58,12 +103,12 @@ describe('Download CallToAction (Astro)', () => {
58103 const primaryLink = window . document . querySelector ( '[data-download-cta-primary]' )
59104
60105 expect ( host ?. getAttribute ( 'data-landing-url' ) ) . toBe (
61- `/downloads/${ existingResource } `
106+ `/downloads/${ downloadFixture . resource } `
62107 )
63108 expect ( host ?. getAttribute ( 'data-direct-download-url' ) ) . toBe (
64- '/downloads/performance-testing-load-models-benchmark-accuracy.pdf'
109+ downloadFixture . directDownloadUrl
65110 )
66- expect ( primaryLink ?. getAttribute ( 'href' ) ) . toBe ( `/downloads/${ existingResource } ` )
111+ expect ( primaryLink ?. getAttribute ( 'href' ) ) . toBe ( `/downloads/${ downloadFixture . resource } ` )
67112 } )
68113 } )
69114
0 commit comments