62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
/**
|
|
* Unit tests for the published-catalogue image cache-busting helper
|
|
* (`src/lib/product-image-cache.ts`).
|
|
*
|
|
* Clients key their image disk caches by URL and the R2 object keys never
|
|
* change, so the published `products.json` has to carry a different URL after
|
|
* an image file is replaced. The value is static on purpose; these tests pin
|
|
* that contract so nobody swaps in a per-request random.
|
|
*/
|
|
import {
|
|
PRODUCT_IMAGE_CACHE_BUST,
|
|
applyImageCacheBust,
|
|
withImageCacheBust,
|
|
} from '@/src/lib/product-image-cache'
|
|
|
|
const URL = 'https://assets.freshyo.in/product-images/1768718685357-0'
|
|
|
|
describe('products.json image cache busting', () => {
|
|
it('appends the static query to a plain URL', () => {
|
|
expect(withImageCacheBust(URL)).toBe(`${URL}?${PRODUCT_IMAGE_CACHE_BUST}`)
|
|
})
|
|
|
|
it('uses & when the URL already carries a query', () => {
|
|
expect(withImageCacheBust(`${URL}?foo=1`)).toBe(
|
|
`${URL}?foo=1&${PRODUCT_IMAGE_CACHE_BUST}`
|
|
)
|
|
})
|
|
|
|
it('leaves an empty URL alone', () => {
|
|
expect(withImageCacheBust('')).toBe('')
|
|
})
|
|
|
|
it('rewrites product images and tag imageUrl, leaving everything else untouched', () => {
|
|
const catalogue = {
|
|
count: 1,
|
|
products: [{ id: 7, name: 'Coconut', images: [URL, `${URL}?foo=1`] }],
|
|
tags: [
|
|
{ id: 1, tagName: 'Popular', imageUrl: 'https://assets.freshyo.in/tags/1.png' },
|
|
{ id: 2, tagName: 'No image', imageUrl: null },
|
|
],
|
|
}
|
|
|
|
const published = applyImageCacheBust(catalogue)
|
|
|
|
expect(published.count).toBe(1)
|
|
expect(published.products[0].name).toBe('Coconut')
|
|
expect(published.products[0].images).toEqual([
|
|
`${URL}?${PRODUCT_IMAGE_CACHE_BUST}`,
|
|
`${URL}?foo=1&${PRODUCT_IMAGE_CACHE_BUST}`,
|
|
])
|
|
expect(published.tags[0].imageUrl).toBe(
|
|
`https://assets.freshyo.in/tags/1.png?${PRODUCT_IMAGE_CACHE_BUST}`
|
|
)
|
|
expect(published.tags[1].imageUrl).toBeNull()
|
|
})
|
|
|
|
it('does not mutate the input catalogue', () => {
|
|
const catalogue = { products: [{ images: [URL] }], tags: [{ imageUrl: null }] }
|
|
applyImageCacheBust(catalogue)
|
|
expect(catalogue.products[0].images).toEqual([URL])
|
|
})
|
|
})
|