@@ -15,9 +15,8 @@ limitations under the License.
15
15
*/
16
16
17
17
import React from "react" ;
18
+ import { fireEvent , render , RenderResult } from "@testing-library/react" ;
18
19
import * as maplibregl from "maplibre-gl" ;
19
- // eslint-disable-next-line deprecate/import
20
- import { mount , ReactWrapper } from "enzyme" ;
21
20
import { act } from "react-dom/test-utils" ;
22
21
import { RoomMember } from "matrix-js-sdk/src/models/room-member" ;
23
22
import { MatrixClient } from "matrix-js-sdk/src/client" ;
@@ -28,7 +27,7 @@ import LocationPicker from "../../../../src/components/views/location/LocationPi
28
27
import { LocationShareType } from "../../../../src/components/views/location/shareLocation" ;
29
28
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext" ;
30
29
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg" ;
31
- import { findById , findByTestId , mockPlatformPeg } from "../../../test-utils" ;
30
+ import { getMockClientWithEventEmitter , mockPlatformPeg } from "../../../test-utils" ;
32
31
import { findMapStyleUrl , LocationShareError } from "../../../../src/utils/location" ;
33
32
34
33
jest . mock ( "../../../../src/utils/location/findMapStyleUrl" , ( ) => ( {
@@ -49,17 +48,15 @@ describe("LocationPicker", () => {
49
48
onChoose : jest . fn ( ) ,
50
49
onFinished : jest . fn ( ) ,
51
50
} ;
52
- const mockClient = {
53
- on : jest . fn ( ) ,
54
- removeListener : jest . fn ( ) ,
55
- off : jest . fn ( ) ,
51
+ const mockClient = getMockClientWithEventEmitter ( {
56
52
isGuest : jest . fn ( ) ,
57
53
getClientWellKnown : jest . fn ( ) ,
58
- } ;
59
- const getComponent = ( props = { } ) =>
60
- mount ( < LocationPicker { ...defaultProps } { ...props } /> , {
61
- wrappingComponent : MatrixClientContext . Provider ,
62
- wrappingComponentProps : { value : mockClient } ,
54
+ } ) ;
55
+ const getComponent = ( props = { } ) : RenderResult =>
56
+ render ( < LocationPicker { ...defaultProps } { ...props } /> , {
57
+ wrapper : ( { children } ) => (
58
+ < MatrixClientContext . Provider value = { mockClient } > { children } </ MatrixClientContext . Provider >
59
+ ) ,
63
60
} ) ;
64
61
65
62
const mapOptions = { container : { } as unknown as HTMLElement , style : "" } ;
@@ -94,18 +91,20 @@ describe("LocationPicker", () => {
94
91
it ( "displays error when map emits an error" , ( ) => {
95
92
// suppress expected error log
96
93
jest . spyOn ( logger , "error" ) . mockImplementation ( ( ) => { } ) ;
97
- const wrapper = getComponent ( ) ;
94
+ const { getByTestId , getByText } = getComponent ( ) ;
98
95
99
96
act ( ( ) => {
100
97
// @ts -ignore
101
98
mocked ( mockMap ) . emit ( "error" , { error : "Something went wrong" } ) ;
102
- wrapper . setProps ( { } ) ;
103
99
} ) ;
104
100
105
- expect ( findByTestId ( wrapper , "map-rendering-error" ) . find ( "p" ) . text ( ) ) . toEqual (
106
- "This homeserver is not configured correctly to display maps, " +
107
- "or the configured map server may be unreachable." ,
108
- ) ;
101
+ expect ( getByTestId ( "map-rendering-error" ) ) . toBeInTheDocument ( ) ;
102
+ expect (
103
+ getByText (
104
+ "This homeserver is not configured correctly to display maps, " +
105
+ "or the configured map server may be unreachable." ,
106
+ ) ,
107
+ ) . toBeInTheDocument ( ) ;
109
108
} ) ;
110
109
111
110
it ( "displays error when map display is not configured properly" , ( ) => {
@@ -115,12 +114,9 @@ describe("LocationPicker", () => {
115
114
throw new Error ( LocationShareError . MapStyleUrlNotConfigured ) ;
116
115
} ) ;
117
116
118
- const wrapper = getComponent ( ) ;
119
- wrapper . setProps ( { } ) ;
117
+ const { getByText } = getComponent ( ) ;
120
118
121
- expect ( findByTestId ( wrapper , "map-rendering-error" ) . find ( "p" ) . text ( ) ) . toEqual (
122
- "This homeserver is not configured to display maps." ,
123
- ) ;
119
+ expect ( getByText ( "This homeserver is not configured to display maps." ) ) . toBeInTheDocument ( ) ;
124
120
} ) ;
125
121
126
122
it ( "displays error when map setup throws" , ( ) => {
@@ -132,13 +128,14 @@ describe("LocationPicker", () => {
132
128
throw new Error ( "oups" ) ;
133
129
} ) ;
134
130
135
- const wrapper = getComponent ( ) ;
136
- wrapper . setProps ( { } ) ;
131
+ const { getByText } = getComponent ( ) ;
137
132
138
- expect ( findByTestId ( wrapper , "map-rendering-error" ) . find ( "p" ) . text ( ) ) . toEqual (
139
- "This homeserver is not configured correctly to display maps, " +
140
- "or the configured map server may be unreachable." ,
141
- ) ;
133
+ expect (
134
+ getByText (
135
+ "This homeserver is not configured correctly to display maps, " +
136
+ "or the configured map server may be unreachable." ,
137
+ ) ,
138
+ ) . toBeInTheDocument ( ) ;
142
139
} ) ;
143
140
144
141
it ( "initiates map with geolocation" , ( ) => {
@@ -174,57 +171,49 @@ describe("LocationPicker", () => {
174
171
} ) ;
175
172
176
173
it ( "sets position on geolocate event" , ( ) => {
177
- const wrapper = getComponent ( { shareType } ) ;
174
+ const { container , getByTestId } = getComponent ( { shareType } ) ;
178
175
act ( ( ) => {
179
176
// @ts -ignore
180
177
mocked ( mockGeolocate ) . emit ( "geolocate" , mockGeolocationPosition ) ;
181
- wrapper . setProps ( { } ) ;
182
178
} ) ;
183
179
184
180
// marker added
185
181
expect ( maplibregl . Marker ) . toHaveBeenCalled ( ) ;
186
182
expect ( mockMarker . setLngLat ) . toHaveBeenCalledWith ( new maplibregl . LngLat ( 12.4 , 43.2 ) ) ;
187
183
// submit button is enabled when position is truthy
188
- expect ( findByTestId ( wrapper , "location-picker-submit-button" ) . at ( 0 ) . props ( ) . disabled ) . toBeFalsy ( ) ;
189
- expect ( wrapper . find ( "MemberAvatar" ) . length ) . toBeTruthy ( ) ;
184
+ expect ( getByTestId ( "location-picker-submit-button" ) ) . not . toBeDisabled ( ) ;
185
+ expect ( container . querySelector ( ".mx_BaseAvatar" ) ) . toBeInTheDocument ( ) ;
190
186
} ) ;
191
187
192
188
it ( "disables submit button until geolocation completes" , ( ) => {
193
189
const onChoose = jest . fn ( ) ;
194
- const wrapper = getComponent ( { shareType, onChoose } ) ;
190
+ const { getByTestId } = getComponent ( { shareType, onChoose } ) ;
195
191
196
- // submit button is enabled when position is truthy
197
- expect ( findByTestId ( wrapper , "location-picker-submit-button" ) . at ( 0 ) . props ( ) . disabled ) . toBeTruthy ( ) ;
198
- act ( ( ) => {
199
- findByTestId ( wrapper , "location-picker-submit-button" ) . at ( 0 ) . simulate ( "click" ) ;
200
- } ) ;
192
+ // button is disabled
193
+ expect ( getByTestId ( "location-picker-submit-button" ) ) . toBeDisabled ( ) ;
194
+ fireEvent . click ( getByTestId ( "location-picker-submit-button" ) ) ;
201
195
// nothing happens on button click
202
196
expect ( onChoose ) . not . toHaveBeenCalled ( ) ;
203
197
204
198
act ( ( ) => {
205
199
// @ts -ignore
206
200
mocked ( mockGeolocate ) . emit ( "geolocate" , mockGeolocationPosition ) ;
207
- wrapper . setProps ( { } ) ;
208
201
} ) ;
209
202
210
203
// submit button is enabled when position is truthy
211
- expect ( findByTestId ( wrapper , "location-picker-submit-button" ) . at ( 0 ) . props ( ) . disabled ) . toBeFalsy ( ) ;
204
+ expect ( getByTestId ( "location-picker-submit-button" ) ) . not . toBeDisabled ( ) ;
212
205
} ) ;
213
206
214
207
it ( "submits location" , ( ) => {
215
208
const onChoose = jest . fn ( ) ;
216
- const wrapper = getComponent ( { onChoose, shareType } ) ;
209
+ const { getByTestId } = getComponent ( { onChoose, shareType } ) ;
217
210
act ( ( ) => {
218
211
// @ts -ignore
219
212
mocked ( mockGeolocate ) . emit ( "geolocate" , mockGeolocationPosition ) ;
220
213
// make sure button is enabled
221
- wrapper . setProps ( { } ) ;
222
- } ) ;
223
-
224
- act ( ( ) => {
225
- findByTestId ( wrapper , "location-picker-submit-button" ) . at ( 0 ) . simulate ( "click" ) ;
226
214
} ) ;
227
215
216
+ fireEvent . click ( getByTestId ( "location-picker-submit-button" ) ) ;
228
217
// content of this call is tested in LocationShareMenu-test
229
218
expect ( onChoose ) . toHaveBeenCalled ( ) ;
230
219
} ) ;
@@ -239,34 +228,21 @@ describe("LocationPicker", () => {
239
228
const shareType = LocationShareType . Live ;
240
229
testUserLocationShareTypes ( shareType ) ;
241
230
242
- const getOption = ( wrapper : ReactWrapper , timeout : number ) =>
243
- findById ( wrapper , `live-duration__${ timeout } ` ) . at ( 0 ) ;
244
- const getDropdown = ( wrapper : ReactWrapper ) => findByTestId ( wrapper , "live-duration-dropdown" ) ;
245
- const getSelectedOption = ( wrapper : ReactWrapper ) => findById ( wrapper , "live-duration_value" ) ;
246
-
247
- const openDropdown = ( wrapper : ReactWrapper ) =>
248
- act ( ( ) => {
249
- const dropdown = getDropdown ( wrapper ) ;
250
- dropdown . find ( '[role="button"]' ) . at ( 0 ) . simulate ( "click" ) ;
251
- wrapper . setProps ( { } ) ;
252
- } ) ;
253
-
254
231
it ( "renders live duration dropdown with default option" , ( ) => {
255
- const wrapper = getComponent ( { shareType } ) ;
256
- expect ( getSelectedOption ( getDropdown ( wrapper ) ) . text ( ) ) . toEqual ( "Share for 15m" ) ;
232
+ const { getByText } = getComponent ( { shareType } ) ;
233
+ expect ( getByText ( "Share for 15m" ) ) . toBeInTheDocument ( ) ;
257
234
} ) ;
258
235
259
236
it ( "updates selected duration" , ( ) => {
260
- const wrapper = getComponent ( { shareType } ) ;
237
+ const { getByText , getByLabelText } = getComponent ( { shareType } ) ;
261
238
262
- openDropdown ( wrapper ) ;
263
- const dropdown = getDropdown ( wrapper ) ;
264
- act ( ( ) => {
265
- getOption ( dropdown , 3600000 ) . simulate ( "click" ) ;
266
- } ) ;
239
+ // open dropdown
240
+ fireEvent . click ( getByLabelText ( "Share for 15m" ) ) ;
241
+
242
+ fireEvent . click ( getByText ( "Share for 1h" ) ) ;
267
243
268
244
// value updated
269
- expect ( getSelectedOption ( getDropdown ( wrapper ) ) . text ( ) ) . toEqual ( "Share for 1h" ) ;
245
+ expect ( getByText ( "Share for 1h" ) ) . toMatchSnapshot ( ) ;
270
246
} ) ;
271
247
} ) ;
272
248
@@ -303,44 +279,40 @@ describe("LocationPicker", () => {
303
279
304
280
it ( "does not set position on geolocate event" , ( ) => {
305
281
mocked ( maplibregl . Marker ) . mockClear ( ) ;
306
- const wrapper = getComponent ( { shareType } ) ;
282
+ const { container } = getComponent ( { shareType } ) ;
307
283
act ( ( ) => {
308
284
// @ts -ignore
309
285
mocked ( mockGeolocate ) . emit ( "geolocate" , mockGeolocationPosition ) ;
310
286
} ) ;
311
287
312
288
// marker not added
313
- expect ( wrapper . find ( "Marker" ) . length ) . toBeFalsy ( ) ;
289
+ expect ( container . querySelector ( "mx_Marker" ) ) . not . toBeInTheDocument ( ) ;
314
290
} ) ;
315
291
316
292
it ( "sets position on click event" , ( ) => {
317
- const wrapper = getComponent ( { shareType } ) ;
293
+ const { container } = getComponent ( { shareType } ) ;
318
294
act ( ( ) => {
319
295
// @ts -ignore
320
296
mocked ( mockMap ) . emit ( "click" , mockClickEvent ) ;
321
- wrapper . setProps ( { } ) ;
322
297
} ) ;
323
298
324
299
// marker added
325
300
expect ( maplibregl . Marker ) . toHaveBeenCalled ( ) ;
326
301
expect ( mockMarker . setLngLat ) . toHaveBeenCalledWith ( new maplibregl . LngLat ( 12.4 , 43.2 ) ) ;
327
302
328
303
// marker is set, icon not avatar
329
- expect ( wrapper . find ( ".mx_Marker_icon" ) . length ) . toBeTruthy ( ) ;
304
+ expect ( container . querySelector ( ".mx_Marker_icon" ) ) . toBeInTheDocument ( ) ;
330
305
} ) ;
331
306
332
307
it ( "submits location" , ( ) => {
333
308
const onChoose = jest . fn ( ) ;
334
- const wrapper = getComponent ( { onChoose, shareType } ) ;
309
+ const { getByTestId } = getComponent ( { onChoose, shareType } ) ;
335
310
act ( ( ) => {
336
311
// @ts -ignore
337
312
mocked ( mockMap ) . emit ( "click" , mockClickEvent ) ;
338
- wrapper . setProps ( { } ) ;
339
313
} ) ;
340
314
341
- act ( ( ) => {
342
- findByTestId ( wrapper , "location-picker-submit-button" ) . at ( 0 ) . simulate ( "click" ) ;
343
- } ) ;
315
+ fireEvent . click ( getByTestId ( "location-picker-submit-button" ) ) ;
344
316
345
317
// content of this call is tested in LocationShareMenu-test
346
318
expect ( onChoose ) . toHaveBeenCalled ( ) ;
0 commit comments