Skip to content

Commit 8eff2be

Browse files
committed
Fix to flaky Download CTA unit test, removing hard-coded content dependency
1 parent af7cc5f commit 8eff2be

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

src/components/CallToAction/Download/__tests__/index.spec.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
1+
import type { CollectionEntry } from 'astro:content'
2+
import { getCollection } from 'astro:content'
13
import { beforeEach, describe, expect, test } from 'vitest'
24
import { experimental_AstroContainer as AstroContainer } from 'astro/container'
35
import { 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(/\/articles\/(.+)\/download\.[^/.]+$/)
15+
16+
if (filePathMatch?.[1]) {
17+
return filePathMatch[1]
18+
}
19+
20+
const normalizedId = download.id.replace(/\\/g, '/')
21+
const idWithoutDownloadSuffix = normalizedId.replace(/\/download$/, '')
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

748
describe('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

src/components/Pages/Resume/index.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const {
9595

9696
<style>
9797
{/** DO NOT ADD STYLES HERE - Use Tailwind classes on HTML elements, this are resets */}
98+
9899
*, *::before, *::after {
99100
box-sizing: border-box;
100101
}

src/components/Pages/Resume/partials/Controls.astro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import Tooltip from '@components/Tooltip/index.astro'
77
<li>
88
<Tooltip text="Download PDF">
99
<a
10-
class="inline-flex h-7 w-7 items-center justify-center hover:text-primary transition-colors duration-200"
10+
class="inline-flex h-7 w-7 items-center justify-center text-primary-offset hover:text-primary transition-colors duration-200"
1111
href="pdf/resume.pdf"
1212
aria-label="Download PDF"
1313
>
14-
<Icon icon="download" color="primary-offset" size={7} />
14+
<Icon icon="download" color="inherit" size={7} />
1515
</a>
1616
</Tooltip>
1717
</li>
1818
<li>
1919
<Tooltip text="Print Resume">
2020
<button
21-
class="inline-flex h-7 w-7 cursor-pointer items-center justify-center appearance-none border-0 bg-transparent p-0 leading-none hover:text-primary transition-colors duration-200"
21+
class="inline-flex h-7 w-7 cursor-pointer items-center justify-center appearance-none border-0 bg-transparent p-0 leading-none text-primary-offset hover:text-primary transition-colors duration-200"
2222
onclick="window.print()"
2323
aria-label="Print Resume"
2424
>
25-
<Icon icon="print" color="primary-offset" size={6} />
25+
<Icon icon="print" color="inherit" size={6} />
2626
</button>
2727
</Tooltip>
2828
</li>

0 commit comments

Comments
 (0)