Skip to content

Commit d1bc553

Browse files
committed
Add tests for speakerImageUrl function and improve mock image URL builder
1 parent 9676aff commit d1bc553

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { speakerImageUrl } from '@/lib/sanity/client'
3+
4+
describe('speakerImageUrl', () => {
5+
const sanityCdnUrl =
6+
'https://cdn.sanity.io/images/testproject/production/abc123-200x200.jpg'
7+
const githubAvatarUrl = 'https://avatars.githubusercontent.com/u/12345?v=4'
8+
const linkedinAvatarUrl =
9+
'https://media.licdn.com/dms/image/v2/abc123/profile-photo.jpg'
10+
11+
it('should transform Sanity CDN URLs through the image builder', () => {
12+
const result = speakerImageUrl(sanityCdnUrl)
13+
14+
expect(result).toContain('cdn.sanity.io')
15+
})
16+
17+
it('should return GitHub avatar URLs unchanged', () => {
18+
expect(speakerImageUrl(githubAvatarUrl)).toBe(githubAvatarUrl)
19+
})
20+
21+
it('should return LinkedIn avatar URLs unchanged', () => {
22+
expect(speakerImageUrl(linkedinAvatarUrl)).toBe(linkedinAvatarUrl)
23+
})
24+
25+
it('should not transform non-Sanity URLs even with custom options', () => {
26+
const result = speakerImageUrl(githubAvatarUrl, {
27+
width: 800,
28+
height: 800,
29+
fit: 'crop',
30+
})
31+
32+
expect(result).toBe(githubAvatarUrl)
33+
})
34+
35+
it('should not transform plain http URLs', () => {
36+
const httpUrl = 'http://example.com/photo.jpg'
37+
expect(speakerImageUrl(httpUrl)).toBe(httpUrl)
38+
})
39+
})
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { vi } from 'vitest'
22

3-
export const createImageUrlBuilder = vi.fn(() => ({
4-
image: vi.fn(() => ({
5-
url: vi.fn(() => 'https://example.com/image.png'),
6-
})),
7-
}))
3+
function createChainableBuilder() {
4+
const builder: Record<string, any> = {}
5+
const chainMethods = ['image', 'width', 'height', 'fit', 'quality']
6+
for (const method of chainMethods) {
7+
builder[method] = vi.fn(() => builder)
8+
}
9+
builder.url = vi.fn(() => 'https://cdn.sanity.io/images/mock/image.png')
10+
return builder
11+
}
12+
13+
export const createImageUrlBuilder = vi.fn(() => createChainableBuilder())

0 commit comments

Comments
 (0)