@@ -15,17 +15,14 @@ limitations under the License.
15
15
*/
16
16
17
17
import React from "react" ;
18
- // eslint-disable-next-line deprecate/import
19
- import { mount , ReactWrapper } from "enzyme" ;
20
18
import { act } from "react-dom/test-utils" ;
19
+ import { fireEvent , render , RenderResult } from "@testing-library/react" ;
21
20
import { MatrixClient , MatrixEvent , Room , RoomMember , getBeaconInfoIdentifier } from "matrix-js-sdk/src/matrix" ;
22
21
import * as maplibregl from "maplibre-gl" ;
23
22
import { mocked } from "jest-mock" ;
24
23
25
24
import BeaconViewDialog from "../../../../src/components/views/beacon/BeaconViewDialog" ;
26
25
import {
27
- findByAttr ,
28
- findByTestId ,
29
26
getMockClientWithEventEmitter ,
30
27
makeBeaconEvent ,
31
28
makeBeaconInfoEvent ,
@@ -34,8 +31,6 @@ import {
34
31
} from "../../../test-utils" ;
35
32
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils" ;
36
33
import { OwnBeaconStore } from "../../../../src/stores/OwnBeaconStore" ;
37
- import { BeaconDisplayStatus } from "../../../../src/components/views/beacon/displayStatus" ;
38
- import BeaconListItem from "../../../../src/components/views/beacon/BeaconListItem" ;
39
34
40
35
describe ( "<BeaconViewDialog />" , ( ) => {
41
36
// 14.03.2022 16:15
@@ -60,6 +55,7 @@ describe("<BeaconViewDialog />", () => {
60
55
61
56
const mapOptions = { container : { } as unknown as HTMLElement , style : "" } ;
62
57
const mockMap = new maplibregl . Map ( mapOptions ) ;
58
+ const mockMarker = new maplibregl . Marker ( ) ;
63
59
64
60
// make fresh rooms every time
65
61
// as we update room state
@@ -84,13 +80,11 @@ describe("<BeaconViewDialog />", () => {
84
80
matrixClient : mockClient as MatrixClient ,
85
81
} ;
86
82
87
- const getComponent = ( props = { } ) => mount ( < BeaconViewDialog { ...defaultProps } { ...props } /> ) ;
83
+ const getComponent = ( props = { } ) : RenderResult => render ( < BeaconViewDialog { ...defaultProps } { ...props } /> ) ;
88
84
89
- const openSidebar = ( component : ReactWrapper ) =>
90
- act ( ( ) => {
91
- findByTestId ( component , "beacon-view-dialog-open-sidebar" ) . at ( 0 ) . simulate ( "click" ) ;
92
- component . setProps ( { } ) ;
93
- } ) ;
85
+ const openSidebar = ( getByTestId : RenderResult [ "getByTestId" ] ) => {
86
+ fireEvent . click ( getByTestId ( "beacon-view-dialog-open-sidebar" ) ) ;
87
+ } ;
94
88
95
89
beforeEach ( ( ) => {
96
90
jest . spyOn ( OwnBeaconStore . instance , "getLiveBeaconIds" ) . mockRestore ( ) ;
@@ -103,23 +97,23 @@ describe("<BeaconViewDialog />", () => {
103
97
const room = setupRoom ( [ defaultEvent ] ) ;
104
98
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ! ;
105
99
beacon . addLocations ( [ location1 ] ) ;
106
- const component = getComponent ( ) ;
107
- expect ( component . find ( "Map" ) . props ( ) ) . toEqual (
108
- expect . objectContaining ( {
109
- centerGeoUri : "geo:51,41" ,
110
- interactive : true ,
111
- } ) ,
112
- ) ;
113
- expect ( component . find ( "SmartMarker" ) . length ) . toEqual ( 1 ) ;
100
+ getComponent ( ) ;
101
+ // centered on default event
102
+ expect ( mockMap . setCenter ) . toHaveBeenCalledWith ( {
103
+ lon : 41 ,
104
+ lat : 51 ,
105
+ } ) ;
106
+ // marker added
107
+ expect ( mockMarker . addTo ) . toHaveBeenCalledWith ( mockMap ) ;
114
108
} ) ;
115
109
116
110
it ( "does not render any own beacon status when user is not live sharing" , ( ) => {
117
111
// default event belongs to alice, we are bob
118
112
const room = setupRoom ( [ defaultEvent ] ) ;
119
113
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ! ;
120
114
beacon . addLocations ( [ location1 ] ) ;
121
- const component = getComponent ( ) ;
122
- expect ( component . find ( "DialogOwnBeaconStatus" ) . html ( ) ) . toBeNull ( ) ;
115
+ const { queryByText } = getComponent ( ) ;
116
+ expect ( queryByText ( "Live location enabled" ) ) . not . toBeInTheDocument ( ) ;
123
117
} ) ;
124
118
125
119
it ( "renders own beacon status when user is live sharing" , ( ) => {
@@ -130,52 +124,47 @@ describe("<BeaconViewDialog />", () => {
130
124
// mock own beacon store to show default event as alice's live beacon
131
125
jest . spyOn ( OwnBeaconStore . instance , "getLiveBeaconIds" ) . mockReturnValue ( [ beacon . identifier ] ) ;
132
126
jest . spyOn ( OwnBeaconStore . instance , "getBeaconById" ) . mockReturnValue ( beacon ) ;
133
- const component = getComponent ( ) ;
134
- expect ( component . find ( "MemberAvatar" ) . length ) . toBeTruthy ( ) ;
135
- expect ( component . find ( "OwnBeaconStatus" ) . props ( ) ) . toEqual ( {
136
- beacon,
137
- displayStatus : BeaconDisplayStatus . Active ,
138
- className : "mx_DialogOwnBeaconStatus_status" ,
139
- } ) ;
127
+ const { container } = getComponent ( ) ;
128
+ expect ( container . querySelector ( ".mx_DialogOwnBeaconStatus" ) ) . toMatchSnapshot ( ) ;
140
129
} ) ;
141
130
142
- it ( "updates markers on changes to beacons" , ( ) => {
131
+ it ( "updates markers on changes to beacons" , async ( ) => {
143
132
const room = setupRoom ( [ defaultEvent ] ) ;
144
133
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ! ;
145
134
beacon . addLocations ( [ location1 ] ) ;
146
- const component = getComponent ( ) ;
147
- expect ( component . find ( "BeaconMarker" ) . length ) . toEqual ( 1 ) ;
135
+ const { container } = getComponent ( ) ;
148
136
149
- const anotherBeaconEvent = makeBeaconInfoEvent ( bobId , roomId , { isLive : true } , "$bob-room1-1" ) ;
137
+ // one marker
138
+ expect ( mockMarker . addTo ) . toHaveBeenCalledTimes ( 1 ) ;
139
+ expect ( container . getElementsByClassName ( "mx_Marker" ) . length ) . toEqual ( 1 ) ;
150
140
141
+ const anotherBeaconEvent = makeBeaconInfoEvent ( bobId , roomId , { isLive : true } , "$bob-room1-1" ) ;
151
142
act ( ( ) => {
152
143
// emits RoomStateEvent.BeaconLiveness
153
144
room . currentState . setStateEvents ( [ anotherBeaconEvent ] ) ;
145
+ const beacon2 = room . currentState . beacons . get ( getBeaconInfoIdentifier ( anotherBeaconEvent ) ) ! ;
146
+ beacon2 . addLocations ( [ location1 ] ) ;
154
147
} ) ;
155
148
156
- component . setProps ( { } ) ;
157
-
158
149
// two markers now!
159
- expect ( component . find ( "BeaconMarker ") . length ) . toEqual ( 2 ) ;
150
+ expect ( container . getElementsByClassName ( "mx_Marker ") . length ) . toEqual ( 2 ) ;
160
151
} ) ;
161
152
162
153
it ( "does not update bounds or center on changing beacons" , ( ) => {
163
154
const room = setupRoom ( [ defaultEvent ] ) ;
164
155
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ! ;
165
156
beacon . addLocations ( [ location1 ] ) ;
166
- const component = getComponent ( ) ;
167
- expect ( component . find ( "BeaconMarker ") . length ) . toEqual ( 1 ) ;
157
+ const { container } = getComponent ( ) ;
158
+ expect ( container . getElementsByClassName ( "mx_Marker ") . length ) . toEqual ( 1 ) ;
168
159
169
160
const anotherBeaconEvent = makeBeaconInfoEvent ( bobId , roomId , { isLive : true } , "$bob-room1-1" ) ;
170
-
171
161
act ( ( ) => {
172
162
// emits RoomStateEvent.BeaconLiveness
173
163
room . currentState . setStateEvents ( [ anotherBeaconEvent ] ) ;
164
+ const beacon2 = room . currentState . beacons . get ( getBeaconInfoIdentifier ( anotherBeaconEvent ) ) ! ;
165
+ beacon2 . addLocations ( [ location1 ] ) ;
174
166
} ) ;
175
-
176
- component . setProps ( { } ) ;
177
-
178
- // two markers now!
167
+ // called once on init
179
168
expect ( mockMap . setCenter ) . toHaveBeenCalledTimes ( 1 ) ;
180
169
expect ( mockMap . fitBounds ) . toHaveBeenCalledTimes ( 1 ) ;
181
170
} ) ;
@@ -185,14 +174,12 @@ describe("<BeaconViewDialog />", () => {
185
174
const onFinished = jest . fn ( ) ;
186
175
const room = setupRoom ( [ defaultEvent ] ) ;
187
176
room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ;
188
- const component = getComponent ( { onFinished } ) ;
177
+ const { getByTestId } = getComponent ( { onFinished } ) ;
189
178
190
179
// map placeholder
191
- expect ( findByTestId ( component , "beacon-view-dialog-map-fallback" ) ) . toMatchSnapshot ( ) ;
180
+ expect ( getByTestId ( "beacon-view-dialog-map-fallback" ) ) . toMatchSnapshot ( ) ;
192
181
193
- act ( ( ) => {
194
- findByTestId ( component , "beacon-view-dialog-fallback-close" ) . at ( 0 ) . simulate ( "click" ) ;
195
- } ) ;
182
+ fireEvent . click ( getByTestId ( "beacon-view-dialog-fallback-close" ) ) ;
196
183
197
184
expect ( onFinished ) . toHaveBeenCalled ( ) ;
198
185
} ) ;
@@ -202,8 +189,8 @@ describe("<BeaconViewDialog />", () => {
202
189
const room = setupRoom ( [ defaultEvent ] ) ;
203
190
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ! ;
204
191
beacon . addLocations ( [ location1 ] ) ;
205
- const component = getComponent ( { onFinished } ) ;
206
- expect ( component . find ( "BeaconMarker ") . length ) . toEqual ( 1 ) ;
192
+ const { container } = getComponent ( { onFinished } ) ;
193
+ expect ( container . getElementsByClassName ( "mx_Marker ") . length ) . toEqual ( 1 ) ;
207
194
208
195
// this will replace the defaultEvent
209
196
// leading to no more live beacons
@@ -219,12 +206,10 @@ describe("<BeaconViewDialog />", () => {
219
206
room . currentState . setStateEvents ( [ anotherBeaconEvent ] ) ;
220
207
} ) ;
221
208
222
- component . setProps ( { } ) ;
223
-
224
209
// no more avatars
225
- expect ( component . find ( "MemberAvatar ") . length ) . toBeFalsy ( ) ;
210
+ expect ( container . getElementsByClassName ( "mx_Marker ") . length ) . toEqual ( 0 ) ;
226
211
// map still rendered
227
- expect ( component . find ( "Map" ) . length ) . toBeTruthy ( ) ;
212
+ expect ( container . querySelector ( "#mx_Map_mx_BeaconViewDialog" ) ) . toBeInTheDocument ( ) ;
228
213
// map location unchanged
229
214
expect ( mockMap . setCenter ) . not . toHaveBeenCalled ( ) ;
230
215
expect ( mockMap . fitBounds ) . not . toHaveBeenCalled ( ) ;
@@ -235,31 +220,28 @@ describe("<BeaconViewDialog />", () => {
235
220
const room = setupRoom ( [ defaultEvent ] ) ;
236
221
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ! ;
237
222
beacon . addLocations ( [ location1 ] ) ;
238
- const component = getComponent ( ) ;
223
+ const { container , getByTestId } = getComponent ( ) ;
239
224
240
- openSidebar ( component ) ;
225
+ openSidebar ( getByTestId ) ;
241
226
242
- expect ( component . find ( "DialogSidebar" ) . length ) . toBeTruthy ( ) ;
227
+ expect ( container . querySelector ( ".mx_DialogSidebar" ) ) . toBeInTheDocument ( ) ;
243
228
} ) ;
244
229
245
230
it ( "closes sidebar on close button click" , ( ) => {
246
231
const room = setupRoom ( [ defaultEvent ] ) ;
247
232
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ! ;
248
233
beacon . addLocations ( [ location1 ] ) ;
249
- const component = getComponent ( ) ;
234
+ const { container , getByTestId } = getComponent ( ) ;
250
235
251
236
// open the sidebar
252
- openSidebar ( component ) ;
237
+ openSidebar ( getByTestId ) ;
253
238
254
- expect ( component . find ( "DialogSidebar" ) . length ) . toBeTruthy ( ) ;
239
+ expect ( container . querySelector ( ".mx_DialogSidebar" ) ) . toBeInTheDocument ( ) ;
255
240
256
241
// now close it
257
- act ( ( ) => {
258
- findByAttr ( "data-testid" ) ( component , "dialog-sidebar-close" ) . at ( 0 ) . simulate ( "click" ) ;
259
- component . setProps ( { } ) ;
260
- } ) ;
242
+ fireEvent . click ( getByTestId ( "dialog-sidebar-close" ) ) ;
261
243
262
- expect ( component . find ( "DialogSidebar" ) . length ) . toBeFalsy ( ) ;
244
+ expect ( container . querySelector ( ".mx_DialogSidebar" ) ) . not . toBeInTheDocument ( ) ;
263
245
} ) ;
264
246
} ) ;
265
247
@@ -326,16 +308,17 @@ describe("<BeaconViewDialog />", () => {
326
308
[ location1 , location2 ] ,
327
309
) ;
328
310
329
- const component = getComponent ( { beacons : [ beacon1 , beacon2 ] } ) ;
311
+ const { container , getByTestId } = getComponent ( { beacons : [ beacon1 , beacon2 ] } ) ;
330
312
331
313
// reset call counts on map mocks after initial render
332
314
jest . clearAllMocks ( ) ;
333
315
334
- openSidebar ( component ) ;
316
+ openSidebar ( getByTestId ) ;
335
317
336
318
act ( ( ) => {
319
+ const listItems = container . querySelectorAll ( ".mx_BeaconListItem" ) ;
337
320
// click on the first beacon in the list
338
- component . find ( BeaconListItem ) . at ( 0 ) . simulate ( "click" ) ;
321
+ fireEvent . click ( listItems [ 0 ] ! ) ;
339
322
} ) ;
340
323
341
324
// centered on clicked beacon
@@ -359,16 +342,17 @@ describe("<BeaconViewDialog />", () => {
359
342
[ location1 , location2 ] ,
360
343
) ;
361
344
362
- const component = getComponent ( { beacons : [ beacon1 , beacon2 ] } ) ;
345
+ const { container , getByTestId } = getComponent ( { beacons : [ beacon1 , beacon2 ] } ) ;
363
346
364
347
// reset call counts on map mocks after initial render
365
348
jest . clearAllMocks ( ) ;
366
349
367
- openSidebar ( component ) ;
350
+ openSidebar ( getByTestId ) ;
368
351
369
352
act ( ( ) => {
370
353
// click on the second beacon in the list
371
- component . find ( BeaconListItem ) . at ( 1 ) . simulate ( "click" ) ;
354
+ const listItems = container . querySelectorAll ( ".mx_BeaconListItem" ) ;
355
+ fireEvent . click ( listItems [ 1 ] ! ) ;
372
356
} ) ;
373
357
374
358
const expectedBounds = new maplibregl . LngLatBounds ( [ 22 , 33 ] , [ 22 , 33 ] ) ;
@@ -378,7 +362,8 @@ describe("<BeaconViewDialog />", () => {
378
362
379
363
act ( ( ) => {
380
364
// click on the second beacon in the list
381
- component . find ( BeaconListItem ) . at ( 1 ) . simulate ( "click" ) ;
365
+ const listItems = container . querySelectorAll ( ".mx_BeaconListItem" ) ;
366
+ fireEvent . click ( listItems [ 1 ] ! ) ;
382
367
} ) ;
383
368
384
369
// centered on clicked beacon
0 commit comments