-
Notifications
You must be signed in to change notification settings - Fork 253
/
image.test.ts
352 lines (309 loc) · 12.7 KB
/
image.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// @vitest-environment nuxt
import { beforeEach, describe, it, expect, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import type { ComponentMountingOptions, VueWrapper } from '@vue/test-utils'
// @ts-expect-error virtual file
import { imageOptions } from '#build/image-options'
import { NuxtImg } from '#components'
import { createImage } from '#image'
describe('Renders simple image', () => {
let wrapper: VueWrapper<any>
const src = '/image.png'
beforeEach(() => {
wrapper = mountImage({
width: 200,
height: 200,
sizes: '200,500:500,900:900',
src,
})
})
it('Matches snapshot', () => {
expect(wrapper.html()).toMatchInlineSnapshot(`"<img width="200" height="200" data-nuxt-img="" sizes="(max-width: 500px) 200px, (max-width: 900px) 500px, 900px" srcset="/_ipx/s_200x200/image.png 200w, /_ipx/s_400x400/image.png 400w, /_ipx/s_500x500/image.png 500w, /_ipx/s_900x900/image.png 900w, /_ipx/s_1000x1000/image.png 1000w, /_ipx/s_1800x1800/image.png 1800w" src="/_ipx/s_1800x1800/image.png">"`)
})
it('props.src is picked up by getImage()', () => {
const domSrc = wrapper.element.getAttribute('src')
expect(domSrc).toMatchInlineSnapshot('"/_ipx/s_1800x1800/image.png"')
})
it('props.src is reactive', async () => {
const newSource = '/image.jpeg'
wrapper.setProps({ src: newSource })
await nextTick()
const domSrc = wrapper.find('img').element.getAttribute('src')
expect(domSrc).toMatchInlineSnapshot('"/_ipx/s_1800x1800/image.jpeg"')
})
it('sizes', () => {
const sizes = wrapper.find('img').element.getAttribute('sizes')
expect(sizes).toMatchInlineSnapshot('"(max-width: 500px) 200px, (max-width: 900px) 500px, 900px"')
})
it('applies densities', () => {
const img = mountImage({
width: 200,
height: 300,
sizes: '300:300px,400:400px',
densities: '1x 2x 3x',
src: 'image.png',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="300" data-nuxt-img="" sizes="(max-width: 400px) 300px, 400px" srcset="/_ipx/s_300x450/image.png 300w, /_ipx/s_400x600/image.png 400w, /_ipx/s_600x900/image.png 600w, /_ipx/s_800x1200/image.png 800w, /_ipx/s_900x1350/image.png 900w, /_ipx/s_1200x1800/image.png 1200w" src="/_ipx/s_1200x1800/image.png">"`)
})
it('empty densities (fallback to global)', () => {
const img = mountImage({
width: 200,
height: 300,
sizes: '300:300px,400:400px',
densities: '',
src: 'image.png',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="300" data-nuxt-img="" sizes="(max-width: 400px) 300px, 400px" srcset="/_ipx/s_300x450/image.png 300w, /_ipx/s_400x600/image.png 400w, /_ipx/s_600x900/image.png 600w, /_ipx/s_800x1200/image.png 800w" src="/_ipx/s_800x1200/image.png">"`)
})
it('empty string densities (fallback to global)', () => {
const img = mountImage({
width: 200,
height: 300,
sizes: '300:300px,400:400px',
densities: ' ',
src: 'image.png',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="300" data-nuxt-img="" sizes="(max-width: 400px) 300px, 400px" srcset="/_ipx/s_300x450/image.png 300w, /_ipx/s_400x600/image.png 400w, /_ipx/s_600x900/image.png 600w, /_ipx/s_800x1200/image.png 800w" src="/_ipx/s_800x1200/image.png">"`)
})
it('error on invalid densities', () => {
expect(() => mountImage({
width: 200,
height: 300,
densities: 'x',
src: 'image.png',
})).toThrow(Error)
})
it('with single sizes entry', () => {
const img = mountImage({
src: '/image.png',
width: 300,
height: 400,
sizes: '150',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="300" height="400" data-nuxt-img="" sizes="150px" srcset="/_ipx/s_150x200/image.png 150w, /_ipx/s_300x400/image.png 300w" src="/_ipx/s_300x400/image.png">"`)
})
it('with single sizes entry (responsive)', () => {
const img = mountImage({
src: '/image.png',
width: 300,
height: 400,
sizes: 'sm:150',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="300" height="400" data-nuxt-img="" sizes="150px" srcset="/_ipx/s_150x200/image.png 150w, /_ipx/s_300x400/image.png 300w" src="/_ipx/s_300x400/image.png">"`)
})
it('de-duplicates sizes & srcset', () => {
const img = mountImage({
width: 200,
height: 300,
sizes: '200:200px,300:200px,400:400px,400:400px,500:500px,800:800px',
src: 'image.png',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="300" data-nuxt-img="" sizes="(max-width: 300px) 200px, (max-width: 400px) 200px, (max-width: 500px) 400px, (max-width: 800px) 500px, 800px" srcset="/_ipx/s_200x300/image.png 200w, /_ipx/s_400x600/image.png 400w, /_ipx/s_500x750/image.png 500w, /_ipx/s_800x1200/image.png 800w, /_ipx/s_1000x1500/image.png 1000w, /_ipx/s_1600x2400/image.png 1600w" src="/_ipx/s_1600x2400/image.png">"`)
})
it('encodes characters', () => {
const img = mountImage({
width: 200,
height: 200,
sizes: '200,500:500,900:900',
src: '/汉字.png',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="200" data-nuxt-img="" sizes="(max-width: 500px) 200px, (max-width: 900px) 500px, 900px" srcset="/_ipx/s_200x200/%E6%B1%89%E5%AD%97.png 200w, /_ipx/s_400x400/%E6%B1%89%E5%AD%97.png 400w, /_ipx/s_500x500/%E6%B1%89%E5%AD%97.png 500w, /_ipx/s_900x900/%E6%B1%89%E5%AD%97.png 900w, /_ipx/s_1000x1000/%E6%B1%89%E5%AD%97.png 1000w, /_ipx/s_1800x1800/%E6%B1%89%E5%AD%97.png 1800w" src="/_ipx/s_1800x1800/%E6%B1%89%E5%AD%97.png">"`)
})
it('correctly sets crop', () => {
const img = mountImage({
src: '/image.png',
width: 1000,
height: 2000,
sizes: 'xs:100vw sm:100vw md:300px lg:350px xl:350px 2xl:350px',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="1000" height="2000" data-nuxt-img="" sizes="(max-width: 640px) 100vw, (max-width: 768px) 100vw, (max-width: 1024px) 300px, (max-width: 1280px) 350px, (max-width: 1536px) 350px, 350px" srcset="/_ipx/s_300x600/image.png 300w, /_ipx/s_320x640/image.png 320w, /_ipx/s_350x700/image.png 350w, /_ipx/s_600x1200/image.png 600w, /_ipx/s_640x1280/image.png 640w, /_ipx/s_700x1400/image.png 700w, /_ipx/s_1280x2560/image.png 1280w" src="/_ipx/s_1280x2560/image.png">"`)
})
it('without sizes, but densities', () => {
const img = mountImage({
src: '/image.png',
width: 300,
height: 400,
densities: '1x 2x 3x',
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="300" height="400" data-nuxt-img="" srcset="/_ipx/s_300x400/image.png 1x, /_ipx/s_600x800/image.png 2x, /_ipx/s_900x1200/image.png 3x" src="/_ipx/s_300x400/image.png">"`)
})
it('with nonce', () => {
const img = mountImage({
src: '/image.png',
width: 300,
height: 400,
nonce: 'stub-nonce',
})
const domNonce = img.element.getAttribute('nonce')
expect(domNonce).toBe('stub-nonce')
})
})
const getImageLoad = (cb = () => {}) => {
let resolve = () => {}
let image = {} as HTMLImageElement
const loadEvent = Symbol('loadEvent')
const ImageMock = vi.fn(() => {
const _image = {
onload: () => {},
} as unknown as HTMLImageElement
image = _image
// @ts-expect-error not valid argument for onload
resolve = () => _image.onload?.(loadEvent)
return _image
})
vi.stubGlobal('Image', ImageMock)
cb()
vi.unstubAllGlobals()
return {
resolve,
image,
loadEvent,
}
}
describe('Renders placeholder image', () => {
let wrapper: VueWrapper<any>
const src = '/image.png'
it('props.placeholder with src', async () => {
const {
resolve: resolveImage,
image: placeholderImage,
loadEvent,
} = getImageLoad(() => {
wrapper = mount(NuxtImg, {
propsData: {
width: 200,
height: 200,
src,
placeholder: true,
},
})
})
let domSrc = wrapper.find('img').element.getAttribute('src')
expect(domSrc).toMatchInlineSnapshot('"/_ipx/q_50&blur_3&s_10x10/image.png"')
expect(placeholderImage.src).toMatchInlineSnapshot('"/_ipx/s_200x200/image.png"')
resolveImage()
await nextTick()
domSrc = wrapper.find('img').element.getAttribute('src')
expect(domSrc).toMatchInlineSnapshot('"/_ipx/s_200x200/image.png"')
expect(wrapper.emitted().load[0]).toStrictEqual([loadEvent])
})
it('props.placeholder with sizes', async () => {
const {
resolve: resolveImage,
image: placeholderImage,
loadEvent,
} = getImageLoad(() => {
wrapper = mount(NuxtImg, {
propsData: {
width: 200,
height: 200,
src,
sizes: '200,500:500,900:900',
placeholder: true,
},
})
})
let sizes = wrapper.find('img').element.getAttribute('sizes')
expect(sizes).toMatchInlineSnapshot('null')
expect(placeholderImage.sizes).toMatchInlineSnapshot('"(max-width: 500px) 200px, (max-width: 900px) 500px, 900px"')
resolveImage()
await nextTick()
sizes = wrapper.find('img').element.getAttribute('sizes')
expect(sizes).toMatchInlineSnapshot('"(max-width: 500px) 200px, (max-width: 900px) 500px, 900px"')
expect(wrapper.emitted().load[0]).toStrictEqual([loadEvent])
})
it('placeholder class can be set', async () => {
const { resolve: resolveImage } = getImageLoad(() => {
wrapper = mount(NuxtImg, {
propsData: {
src,
placeholder: true,
placeholderClass: 'placeholder',
},
attrs: {
class: [{ test: true }, 'other'],
},
})
})
expect([...wrapper.element.classList]).toMatchInlineSnapshot(`
[
"placeholder",
"test",
"other",
]
`)
expect(wrapper.element.getAttribute('src')).toMatchInlineSnapshot('"/_ipx/q_50&blur_3&s_10x10/image.png"')
resolveImage()
await nextTick()
expect([...wrapper.element.classList]).toMatchInlineSnapshot(`
[
"test",
"other",
]
`)
expect(wrapper.element.getAttribute('src')).toMatchInlineSnapshot(`"/_ipx/_/image.png"`)
})
})
describe('Renders image, applies module config', () => {
const nuxtApp = useNuxtApp()
const config = useRuntimeConfig()
const src = '/image.png'
beforeEach(() => {
delete nuxtApp._img
})
it('Module config .quality applies', () => {
nuxtApp._img = createImage({
...imageOptions,
nuxt: {
baseURL: config.app.baseURL,
},
quality: 75,
})
const img = mount(NuxtImg, {
propsData: {
src,
width: 200,
height: 200,
sizes: '200,500:500,900:900',
},
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="200" data-nuxt-img="" sizes="(max-width: 500px) 200px, (max-width: 900px) 500px, 900px" srcset="/_ipx/q_75&s_200x200/image.png 200w, /_ipx/q_75&s_400x400/image.png 400w, /_ipx/q_75&s_500x500/image.png 500w, /_ipx/q_75&s_900x900/image.png 900w, /_ipx/q_75&s_1000x1000/image.png 1000w, /_ipx/q_75&s_1800x1800/image.png 1800w" src="/_ipx/q_75&s_1800x1800/image.png">"`)
})
it('Module config .quality + props.quality => props.quality applies', () => {
nuxtApp._img = createImage({
...imageOptions,
nuxt: {
baseURL: config.app.baseURL,
},
quality: 75,
})
const img = mount(NuxtImg, {
propsData: {
src,
width: 200,
height: 200,
sizes: '200,500:500,900:900',
quality: 90,
},
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="200" data-nuxt-img="" sizes="(max-width: 500px) 200px, (max-width: 900px) 500px, 900px" srcset="/_ipx/q_90&s_200x200/image.png 200w, /_ipx/q_90&s_400x400/image.png 400w, /_ipx/q_90&s_500x500/image.png 500w, /_ipx/q_90&s_900x900/image.png 900w, /_ipx/q_90&s_1000x1000/image.png 1000w, /_ipx/q_90&s_1800x1800/image.png 1800w" src="/_ipx/q_90&s_1800x1800/image.png">"`)
})
it('Without quality config => default image', () => {
nuxtApp._img = createImage({
...imageOptions,
nuxt: {
baseURL: config.app.baseURL,
},
})
const img = mount(NuxtImg, {
propsData: {
src,
width: 200,
height: 200,
sizes: '200,500:500,900:900',
},
})
expect(img.html()).toMatchInlineSnapshot(`"<img width="200" height="200" data-nuxt-img="" sizes="(max-width: 500px) 200px, (max-width: 900px) 500px, 900px" srcset="/_ipx/s_200x200/image.png 200w, /_ipx/s_400x400/image.png 400w, /_ipx/s_500x500/image.png 500w, /_ipx/s_900x900/image.png 900w, /_ipx/s_1000x1000/image.png 1000w, /_ipx/s_1800x1800/image.png 1800w" src="/_ipx/s_1800x1800/image.png">"`)
})
})
const mountImage = (props: ComponentMountingOptions<typeof NuxtImg>['props']) => mount(NuxtImg, { props })