-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathnext-image-new.test.ts
100 lines (97 loc) · 2.9 KB
/
next-image-new.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
/* eslint-env jest */
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import Image from 'next/image'
import cheerio from 'cheerio'
describe('Image rendering', () => {
it('should render Image on its own', async () => {
const element = React.createElement(Image, {
alt: 'a nice image',
id: 'unit-image',
src: '/test.png',
width: 100,
height: 100,
loading: 'eager',
})
const html = ReactDOMServer.renderToString(element)
const $ = cheerio.load(html)
const img = $('#unit-image')
// order matters here
expect(img.attr()).toStrictEqual({
alt: 'a nice image',
id: 'unit-image',
loading: 'eager',
width: '100',
height: '100',
decoding: 'async',
'data-nimg': '1',
style: 'color:transparent',
srcset:
'/_next/image?url=%2Ftest.png&w=128&q=75 1x, /_next/image?url=%2Ftest.png&w=256&q=75 2x',
src: '/_next/image?url=%2Ftest.png&w=256&q=75',
})
})
it('should only render noscript element when lazy loading', async () => {
const element = React.createElement(Image, {
alt: 'test',
src: '/test.png',
width: 100,
height: 100,
loading: 'eager',
})
const element2 = React.createElement(Image, {
alt: 'test',
src: '/test.png',
width: 100,
height: 100,
priority: true,
})
const elementLazy = React.createElement(Image, {
alt: 'test',
src: '/test.png',
width: 100,
height: 100,
})
const $ = cheerio.load(ReactDOMServer.renderToString(element))
const $2 = cheerio.load(ReactDOMServer.renderToString(element2))
const $lazy = cheerio.load(ReactDOMServer.renderToString(elementLazy))
expect($('noscript').length).toBe(0)
expect($2('noscript').length).toBe(0)
expect($lazy('noscript').length).toBe(0)
})
it('should not render noscript', async () => {
const element1 = React.createElement(Image, {
alt: 'test',
src: '/test.png',
width: 100,
height: 100,
loading: 'eager',
placeholder: 'blur',
blurDataURL: 'data:image/png;base64',
})
const element2 = React.createElement(Image, {
alt: 'test',
src: '/test.png',
width: 100,
height: 100,
placeholder: 'blur',
blurDataURL: 'data:image/png;base64',
loading: 'eager',
})
const element3 = React.createElement(Image, {
alt: 'test',
src: '/test.png',
width: 100,
height: 100,
placeholder: 'blur',
blurDataURL: 'data:image/png;base64',
priority: true,
})
const $1 = cheerio.load(ReactDOMServer.renderToString(element1))
const $2 = cheerio.load(ReactDOMServer.renderToString(element2))
const $3 = cheerio.load(ReactDOMServer.renderToString(element3))
expect($1('noscript').length).toBe(0)
expect($2('noscript').length).toBe(0)
expect($3('noscript').length).toBe(0)
})
})