fix: unifier la logique de choix de taille d'image via formatKuveti
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import {describe, it, expect} from 'vitest'
|
||||
import {formatKuveti} from '../kuveti'
|
||||
|
||||
const kuveti = {
|
||||
url: '/uploads/original.jpg',
|
||||
formats: {
|
||||
thumbnail: {url: '/uploads/thumbnail.jpg', width: 156, height: 156},
|
||||
small: {url: '/uploads/small.jpg', width: 500, height: 500},
|
||||
medium: {url: '/uploads/medium.jpg', width: 750, height: 750},
|
||||
large: {url: '/uploads/large.jpg', width: 1000, height: 1000},
|
||||
}
|
||||
}
|
||||
|
||||
describe('formatKuveti', () => {
|
||||
it('returns null when there is no image', () => {
|
||||
expect(formatKuveti(null)).toBeNull()
|
||||
expect(formatKuveti(undefined)).toBeNull()
|
||||
})
|
||||
|
||||
it('defaults to the large format', () => {
|
||||
expect(formatKuveti(kuveti)).toBe(kuveti.formats.large)
|
||||
})
|
||||
|
||||
it('prefers the thumbnail format when asked', () => {
|
||||
expect(formatKuveti(kuveti, 'thumbnail')).toBe(kuveti.formats.thumbnail)
|
||||
})
|
||||
|
||||
it('falls back to small then medium then large when thumbnail is missing', () => {
|
||||
const {thumbnail, ...rest} = kuveti.formats
|
||||
const withoutThumbnail = {...kuveti, formats: rest}
|
||||
expect(formatKuveti(withoutThumbnail, 'thumbnail')).toBe(rest.small)
|
||||
})
|
||||
|
||||
it('falls back to the original file when no format matches', () => {
|
||||
const noFormats = {url: '/uploads/original.jpg', formats: {}}
|
||||
expect(formatKuveti(noFormats, 'thumbnail')).toBe(noFormats)
|
||||
})
|
||||
})
|
||||
+4
-3
@@ -1,7 +1,8 @@
|
||||
const SIZE_ORDER = {
|
||||
large: ['large', 'medium', 'small'],
|
||||
medium: ['medium', 'small', 'large'],
|
||||
small: ['small', 'medium', 'large'],
|
||||
large: ['large', 'medium', 'small'],
|
||||
medium: ['medium', 'small', 'large'],
|
||||
small: ['small', 'medium', 'large'],
|
||||
thumbnail: ['thumbnail', 'small', 'medium', 'large'],
|
||||
}
|
||||
|
||||
export const formatKuveti = (kuveti, preferred = 'large') => {
|
||||
|
||||
Reference in New Issue
Block a user