Skip to content

Commit 514bd9d

Browse files
committed
Add unit test for new utility social share card helper and for refactored meta utilities
1 parent 80fa7c1 commit 514bd9d

3 files changed

Lines changed: 114 additions & 3 deletions

File tree

.cache/pages.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
"contact",
2323
"offline",
2424
{
25-
"privacy": ["my-data"]
25+
"privacy": [
26+
"my-data"
27+
]
2628
},
2729
{
28-
"services": ["consulting", "overview", "web-development"]
30+
"services": [
31+
"consulting",
32+
"overview",
33+
"web-development"
34+
]
2935
},
3036
{
3137
"tags": [
@@ -47,4 +53,4 @@
4753
"typescript"
4854
]
4955
}
50-
]
56+
]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { afterEach, describe, expect, it, vi } from 'vitest'
2+
const loadMetaModule = async () => {
3+
const module = await import('../meta')
4+
return module.getMetaThemeData
5+
}
6+
7+
const loadBuildError = async () => {
8+
const module = await import('@lib/errors/BuildError')
9+
return module.BuildError
10+
}
11+
12+
describe('getMetaThemeData', () => {
13+
afterEach(() => {
14+
vi.resetModules()
15+
vi.doUnmock('@content/themes.json')
16+
})
17+
18+
it('returns the theme that matches the configured default id', async () => {
19+
vi.doMock('@content/themes.json', () => ({
20+
default: {
21+
defaultTheme: { id: 'midnight', prefersDarkScheme: true },
22+
themes: [
23+
{ id: 'sunrise', colors: { backgroundOffset: '#ffeeaa' } },
24+
{ id: 'midnight', colors: { backgroundOffset: '#001133' } },
25+
],
26+
},
27+
}))
28+
29+
const getMetaThemeData = await loadMetaModule()
30+
const theme = getMetaThemeData()
31+
32+
expect(theme.id).toBe('midnight')
33+
expect(theme.colors?.backgroundOffset).toBe('#001133')
34+
})
35+
36+
it('falls back to the first configured theme when the default id is missing', async () => {
37+
vi.doMock('@content/themes.json', () => ({
38+
default: {
39+
defaultTheme: { id: 'nonexistent', prefersDarkScheme: false },
40+
themes: [
41+
{ id: 'primary', colors: { backgroundOffset: '#ffffff' } },
42+
{ id: 'secondary', colors: { backgroundOffset: '#000000' } },
43+
],
44+
},
45+
}))
46+
47+
const getMetaThemeData = await loadMetaModule()
48+
expect(getMetaThemeData().id).toBe('primary')
49+
})
50+
51+
it('throws a BuildError when no themes are configured', async () => {
52+
vi.doMock('@content/themes.json', () => ({
53+
default: {
54+
defaultTheme: { id: 'anything', prefersDarkScheme: false },
55+
themes: [],
56+
},
57+
}))
58+
59+
const getMetaThemeData = await loadMetaModule()
60+
const BuildError = await loadBuildError()
61+
const call = () => getMetaThemeData()
62+
63+
expect(call).toThrow(BuildError)
64+
expect(call).toThrow('No themes configured in themes.json; unable to set meta theme-color.')
65+
})
66+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { getSocialImageLink } from '../utils'
3+
import { getSiteUrl } from '@lib/config/siteUrlServer'
4+
5+
vi.mock('@lib/config/siteUrlServer', () => ({
6+
getSiteUrl: vi.fn(() => 'https://site.test'),
7+
}))
8+
9+
const mockedGetSiteUrl = vi.mocked(getSiteUrl)
10+
11+
describe('getSocialImageLink', () => {
12+
beforeEach(() => {
13+
mockedGetSiteUrl.mockClear()
14+
mockedGetSiteUrl.mockReturnValue('https://site.test')
15+
})
16+
17+
it('falls back to the home slug when no usable path is provided', () => {
18+
expect(getSocialImageLink()).toBe('https://site.test/og/home.png')
19+
expect(getSocialImageLink(' ')).toBe('https://site.test/og/home.png')
20+
expect(mockedGetSiteUrl).toHaveBeenCalledTimes(2)
21+
})
22+
23+
it('normalizes relative paths with redundant separators and whitespace', () => {
24+
const url = getSocialImageLink(' /Articles//My Feature / ')
25+
expect(url).toBe('https://site.test/og/Articles/My-Feature.png')
26+
})
27+
28+
it('parses absolute URLs and decodes their path segments', () => {
29+
const url = getSocialImageLink('https://preview.example.com/services/Launch%20Plan///?src=home')
30+
expect(url).toBe('https://site.test/og/services/Launch-Plan.png')
31+
})
32+
33+
it('uses the site url value returned at call time', () => {
34+
mockedGetSiteUrl.mockReturnValueOnce('https://custom.test')
35+
const url = getSocialImageLink('about/company')
36+
expect(url).toBe('https://custom.test/og/about/company.png')
37+
expect(mockedGetSiteUrl).toHaveBeenCalledTimes(1)
38+
})
39+
})

0 commit comments

Comments
 (0)