-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathindex.test.jsx
148 lines (125 loc) · 4.22 KB
/
index.test.jsx
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
import React from 'react'
import {Img} from './'
import {render, act, cleanup, waitFor} from '@testing-library/react'
import ReactDOMServer from 'react-dom/server'
afterEach(cleanup)
const imgPromise = (decode) => (src) => {
return new Promise((resolve, reject) => {
const img = new Image()
images.push(img)
img.decode = () => Promise.resolve()
img.onload = () => {
decode ? img.decode().then(resolve).catch(reject) : resolve()
}
img.onerror = reject
img.src = src
// mock loading
src.endsWith('LOAD') ? img.onload() : img.onerror()
})
}
describe('Img', () => {
test('render with src string, after load', () => {
const {getByAltText} = render(
<Img src="fooLOAD" imgPromise={imgPromise} alt="" />,
)
waitFor(() => expect(getByAltText('').src).toEqual(location.href + 'foo'))
})
test('render with src array', () => {
const {getByAltText} = render(
<Img src={['fooLOAD']} imgPromise={imgPromise} alt="" />,
)
waitFor(() => expect(getByAltText('').src).toEqual(location.href + 'foo'))
})
// https://github.com/kentcdodds/react-testing-library/issues/281
test('render with decode=true', () => {
const {getByAltText} = render(
<Img src="fooDecode" imgPromise={imgPromise} alt="" />,
)
waitFor(() =>
expect(getByAltText('').src).toEqual(location.href + 'fooDecode'),
)
})
test('fallback to next image', () => {
const {getByAltText} = render(
<Img src={['foo', 'barLOAD']} imgPromise={imgPromise} alt="" />,
)
waitFor(() => expect(getByAltText('').src).toEqual(location.href + 'bar'))
})
test('ensure missing image isnt rendered to browser', () => {
const {container} = render(
<Img src={['foo', 'bar']} imgPromise={imgPromise} alt="" />,
)
expect(container.innerHTML).toEqual('')
})
test('show loader', () => {
const {container} = render(
<Img src="foo" loader={<span>Loading...</span>} alt="" />,
)
waitFor(() =>
expect(container.innerHTML).toEqual('<span>Loading...</span>'),
)
})
test('clear loader after load', async () => {
const {container, getByAltText} = render(
<Img
src="foo"
loader={<span>Loading...</span>}
alt=""
imgPromise={imgPromise}
/>,
)
waitFor(() => expect(getByAltText('').src).toEqual(location.href + 'foo'))
})
test('show unloader', async () => {
const {container} = render(
<Img
unloader={<span>Could not load image!</span>}
imgPromise={imgPromise}
/>,
)
waitFor(() =>
expect(container.innerHTML).toEqual('<span>Could not load image!</span>'),
)
})
test('update image on src prop change', () => {
const {rerender, getByAltText} = render(
<Img src="fooLOAD" imgPromise={imgPromise} alt="" />,
)
rerender(<Img src="barLOAD" imgPromise={imgPromise} alt="" />)
waitFor(() => expect(getByAltText('').src).toEqual(location.href + 'bar'))
})
test('start over on src prop change', async () => {
const {getByAltText, rerender} = render(
<Img src={['foo', 'bar']} imgPromise={imgPromise} alt="" />,
)
waitFor(() => expect(getByAltText('').src).toEqual(location.href + 'bar'))
rerender(<Img src="bazLOAD" imgPromise={imgPromise} alt="" />)
waitFor(() => expect(getByAltText('').src).toEqual(location.href + 'baz'))
})
test('updated props no src', async () => {
const {container, rerender} = render(
<Img src="fooLOAD" imgPromise={imgPromise} />,
)
rerender(<Img src="" imgPromise={imgPromise} />)
waitFor(() => expect(container.innerHTML).toEqual(''))
})
test('onError does nothing if unmounted', async () => {
const {unmount} = render(<Img src="foo5" imgPromise={imgPromise} />)
//unmount()
setTimeout(() => unmount(), 1)
})
})
describe('ssr', () => {
test('should ssr a loader', () => {
const html = ReactDOMServer.renderToStaticMarkup(
<Img src="foo10" loader={<span>Loading...</span>} mockImage={{}} />,
)
expect(html).toEqual('<span>Loading...</span>')
})
test('should ssr nothing if only src is set', () => {
const html = ReactDOMServer.renderToStaticMarkup(
<Img src="foo11" mockImage={{}} />,
)
expect(html).toEqual('')
})
})