1
+ import "@testing-library/jest-dom" ;
2
+ import { render } from "@testing-library/react" ;
3
+ import userEvent , { UserEvent } from "@testing-library/user-event" ;
1
4
import { createElement } from "react" ;
2
- import { mount , render } from "enzyme " ;
5
+ import { ModalProps } from "react-overlays/esm/Modal " ;
3
6
import { Image , ImageProps } from "../Image/Image" ;
4
7
import { Lightbox } from "../Lightbox" ;
5
- import { ModalProps } from "react-overlays/esm/Modal" ;
6
8
7
9
jest . mock ( "../../assets/ic24-close.svg" , ( ) => "close-button-icon-svg" ) ;
8
10
@@ -70,33 +72,38 @@ const iconProps: ImageProps = {
70
72
71
73
describe ( "Image" , ( ) => {
72
74
it ( "renders the structure with an image" , ( ) => {
73
- expect ( render ( < Image { ...imageProps } /> ) ) . toMatchSnapshot ( ) ;
75
+ const image = render ( < Image { ...imageProps } /> ) ;
76
+ expect ( image . asFragment ( ) ) . toMatchSnapshot ( ) ;
74
77
} ) ;
75
78
76
79
it ( "renders the structure with an image and percentage dimensions" , ( ) => {
77
- expect (
78
- render ( < Image { ...imageProps } height = { 100 } width = { 100 } heightUnit = "auto" widthUnit = "percentage" /> )
79
- ) . toMatchSnapshot ( ) ;
80
+ const image = render (
81
+ < Image { ...imageProps } height = { 100 } width = { 100 } heightUnit = "auto" widthUnit = "percentage" />
82
+ ) ;
83
+ expect ( image . asFragment ( ) ) . toMatchSnapshot ( ) ;
80
84
} ) ;
81
85
82
86
it ( "renders the structure with a glyph icon" , ( ) => {
83
- expect ( render ( < Image { ...glyphiconProps } /> ) ) . toMatchSnapshot ( ) ;
87
+ const image = render ( < Image { ...glyphiconProps } /> ) ;
88
+ expect ( image . asFragment ( ) ) . toMatchSnapshot ( ) ;
84
89
} ) ;
85
90
86
91
it ( "renders the structure with an icon" , ( ) => {
87
- expect ( render ( < Image { ...iconProps } /> ) ) . toMatchSnapshot ( ) ;
92
+ const image = render ( < Image { ...iconProps } /> ) ;
93
+ expect ( image . asFragment ( ) ) . toMatchSnapshot ( ) ;
88
94
} ) ;
89
95
90
96
it ( "renders the structure as a background image" , ( ) => {
91
- expect (
92
- render ( < Image { ...imageProps } renderAsBackground backgroundImageContent = { < div > Image content</ div > } /> )
93
- ) . toMatchSnapshot ( ) ;
97
+ const image = render (
98
+ < Image { ...imageProps } renderAsBackground backgroundImageContent = { < div > Image content</ div > } />
99
+ ) ;
100
+ expect ( image . asFragment ( ) ) . toMatchSnapshot ( ) ;
94
101
} ) ;
95
102
96
103
describe ( "when the onClickType is action" , ( ) => {
97
104
it ( "calls the onClick when clicking on an image" , ( ) => {
98
105
const onClickMock = jest . fn ( ) ;
99
- const imageRender = mount ( < Image { ...imageProps } onClick = { onClickMock } onClickType = "action" /> ) ;
106
+ const imageRender = render ( < Image { ...imageProps } onClick = { onClickMock } onClickType = "action" /> ) ;
100
107
101
108
const image = imageRender . find ( "img" ) ;
102
109
expect ( image ) . toHaveLength ( 1 ) ;
@@ -107,7 +114,7 @@ describe("Image", () => {
107
114
108
115
it ( "has tabindex if there is an action with OnClick" , ( ) => {
109
116
const onClickMock = jest . fn ( ) ;
110
- const imageRender = mount (
117
+ const imageRender = render (
111
118
< Image { ...imageProps } onClick = { onClickMock } onClickType = "action" tabIndex = { 1 } />
112
119
) ;
113
120
const image = imageRender . find ( "img" ) ;
@@ -117,15 +124,15 @@ describe("Image", () => {
117
124
} ) ;
118
125
119
126
it ( "has no tabindex if there is no action with OnClick" , ( ) => {
120
- const imageRender = mount ( < Image { ...imageProps } /> ) ;
127
+ const imageRender = render ( < Image { ...imageProps } /> ) ;
121
128
const image = imageRender . find ( "img" ) ;
122
129
123
130
expect ( image . prop ( "tabIndex" ) ) . toBeUndefined ( ) ;
124
131
} ) ;
125
132
126
133
it ( "calls the onClick when clicking on a glyph icon" , ( ) => {
127
134
const onClickMock = jest . fn ( ) ;
128
- const imageRender = mount ( < Image { ...glyphiconProps } onClick = { onClickMock } onClickType = "action" /> ) ;
135
+ const imageRender = render ( < Image { ...glyphiconProps } onClick = { onClickMock } onClickType = "action" /> ) ;
129
136
130
137
const glyphicon = imageRender . find ( "span" ) ;
131
138
expect ( glyphicon ) . toHaveLength ( 1 ) ;
@@ -136,7 +143,7 @@ describe("Image", () => {
136
143
137
144
it ( "calls the onClick when clicking on an icon" , ( ) => {
138
145
const onClickMock = jest . fn ( ) ;
139
- const imageRender = mount ( < Image { ...iconProps } onClick = { onClickMock } onClickType = "action" /> ) ;
146
+ const imageRender = render ( < Image { ...iconProps } onClick = { onClickMock } onClickType = "action" /> ) ;
140
147
141
148
const glyphicon = imageRender . find ( "span" ) ;
142
149
expect ( glyphicon ) . toHaveLength ( 1 ) ;
@@ -148,7 +155,7 @@ describe("Image", () => {
148
155
149
156
describe ( "when the onClickType is enlarge" , ( ) => {
150
157
it ( "shows a lightbox when the user clicks on the image" , ( ) => {
151
- const imageRender = mount ( < Image { ...imageProps } onClickType = "enlarge" /> ) ;
158
+ const imageRender = render ( < Image { ...imageProps } onClickType = "enlarge" /> ) ;
152
159
expect ( imageRender . find ( Lightbox ) ) . toHaveLength ( 0 ) ;
153
160
154
161
const image = imageRender . find ( "img" ) ;
@@ -159,7 +166,7 @@ describe("Image", () => {
159
166
} ) ;
160
167
161
168
it ( "closes the lightbox when the user clicks on the close button after opening it" , ( ) => {
162
- const imageRender = mount ( < Image { ...imageProps } onClickType = "enlarge" /> ) ;
169
+ const imageRender = render ( < Image { ...imageProps } onClickType = "enlarge" /> ) ;
163
170
164
171
const image = imageRender . find ( "img" ) ;
165
172
expect ( image ) . toHaveLength ( 1 ) ;
@@ -178,7 +185,7 @@ describe("Image", () => {
178
185
it ( "does not trigger on clicks from containers if clicked on the image" , ( ) => {
179
186
const onClickOuterMock = jest . fn ( ) ;
180
187
const onClickImageMock = jest . fn ( ) ;
181
- const imageRender = mount (
188
+ const imageRender = render (
182
189
< div onClick = { onClickOuterMock } >
183
190
< Image { ...imageProps } onClickType = "action" onClick = { onClickImageMock } />
184
191
</ div >
@@ -194,20 +201,20 @@ describe("Image", () => {
194
201
195
202
describe ( "when there is an accessibility alt text" , ( ) => {
196
203
it ( "is set properly on an image" , ( ) => {
197
- const imageRender = mount ( < Image { ...imageProps } altText = "this is an awesome image" /> ) ;
204
+ const imageRender = render ( < Image { ...imageProps } altText = "this is an awesome image" /> ) ;
198
205
const image = imageRender . find ( "img" ) ;
199
206
expect ( image . prop ( "alt" ) ) . toBe ( "this is an awesome image" ) ;
200
207
} ) ;
201
208
202
209
it ( "is set properly on a glyphicon" , ( ) => {
203
- const imageRender = mount ( < Image { ...glyphiconProps } altText = "this is an awesome glyphicon" /> ) ;
210
+ const imageRender = render ( < Image { ...glyphiconProps } altText = "this is an awesome glyphicon" /> ) ;
204
211
const image = imageRender . find ( "span" ) ;
205
212
expect ( image . prop ( "aria-label" ) ) . toBe ( "this is an awesome glyphicon" ) ;
206
213
expect ( image . prop ( "role" ) ) . toBe ( "img" ) ;
207
214
} ) ;
208
215
209
216
it ( "is set properly on an icon" , ( ) => {
210
- const imageRender = mount ( < Image { ...iconProps } altText = "this is an awesome icon" /> ) ;
217
+ const imageRender = render ( < Image { ...iconProps } altText = "this is an awesome icon" /> ) ;
211
218
const image = imageRender . find ( "span" ) ;
212
219
expect ( image . prop ( "aria-label" ) ) . toBe ( "this is an awesome icon" ) ;
213
220
expect ( image . prop ( "role" ) ) . toBe ( "img" ) ;
@@ -216,20 +223,20 @@ describe("Image", () => {
216
223
217
224
describe ( "when there is no accessibility alt text" , ( ) => {
218
225
it ( "nothing is set on an image" , ( ) => {
219
- const imageRender = mount ( < Image { ...imageProps } /> ) ;
226
+ const imageRender = render ( < Image { ...imageProps } /> ) ;
220
227
const image = imageRender . find ( "img" ) ;
221
228
expect ( image . prop ( "alt" ) ) . toBe ( undefined ) ;
222
229
} ) ;
223
230
224
231
it ( "nothing is set on a glyphicon" , ( ) => {
225
- const imageRender = mount ( < Image { ...glyphiconProps } /> ) ;
232
+ const imageRender = render ( < Image { ...glyphiconProps } /> ) ;
226
233
const image = imageRender . find ( "span" ) ;
227
234
expect ( image ) . not . toHaveProperty ( "aria-label" ) ;
228
235
expect ( image ) . not . toHaveProperty ( "role" ) ;
229
236
} ) ;
230
237
231
238
it ( "nothing is set on an icon" , ( ) => {
232
- const imageRender = mount ( < Image { ...iconProps } /> ) ;
239
+ const imageRender = render ( < Image { ...iconProps } /> ) ;
233
240
const image = imageRender . find ( "span" ) ;
234
241
expect ( image ) . not . toHaveProperty ( "aria-label" ) ;
235
242
expect ( image ) . not . toHaveProperty ( "role" ) ;
@@ -238,13 +245,13 @@ describe("Image", () => {
238
245
239
246
describe ( "when showing an image as a thumbnail" , ( ) => {
240
247
it ( "includes the thumb=true URL param in the image" , ( ) => {
241
- const imageRender = mount ( < Image { ...imageProps } displayAs = "thumbnail" /> ) ;
248
+ const imageRender = render ( < Image { ...imageProps } displayAs = "thumbnail" /> ) ;
242
249
const image = imageRender . find ( "img" ) ;
243
250
expect ( image . prop ( "src" ) ) . toContain ( "thumb=true" ) ;
244
251
} ) ;
245
252
246
253
it ( "does not include the thumb=true URL param in the lightbox image" , ( ) => {
247
- const imageRender = mount ( < Image { ...imageProps } displayAs = "thumbnail" onClickType = "enlarge" /> ) ;
254
+ const imageRender = render ( < Image { ...imageProps } displayAs = "thumbnail" onClickType = "enlarge" /> ) ;
248
255
249
256
const image = imageRender . find ( "img" ) ;
250
257
expect ( image . prop ( "src" ) ) . toContain ( "thumb=true" ) ;
@@ -264,15 +271,15 @@ describe("Image", () => {
264
271
265
272
describe ( "when showing as a background image" , ( ) => {
266
273
it ( "shows the content" , ( ) => {
267
- const imageRender = mount (
274
+ const imageRender = render (
268
275
< Image { ...imageProps } renderAsBackground backgroundImageContent = { < div > Image content</ div > } />
269
276
) ;
270
277
expect ( imageRender . text ( ) ) . toContain ( "Image content" ) ;
271
278
} ) ;
272
279
273
280
it ( "properly handles on click event if configured by the user" , ( ) => {
274
281
const onClickMock = jest . fn ( ) ;
275
- const imageRender = mount (
282
+ const imageRender = render (
276
283
< Image
277
284
{ ...imageProps }
278
285
renderAsBackground
0 commit comments