@@ -15,8 +15,7 @@ limitations under the License.
15
15
*/
16
16
17
17
import React from "react" ;
18
- // eslint-disable-next-line deprecate/import
19
- import { mount } from "enzyme" ;
18
+ import { render , screen } from "@testing-library/react" ;
20
19
import * as maplibregl from "maplibre-gl" ;
21
20
import { act } from "react-dom/test-utils" ;
22
21
import { Beacon , Room , RoomMember , MatrixEvent , getBeaconInfoIdentifier } from "matrix-js-sdk/src/matrix" ;
@@ -43,6 +42,7 @@ describe("<BeaconMarker />", () => {
43
42
44
43
const mapOptions = { container : { } as unknown as HTMLElement , style : "" } ;
45
44
const mockMap = new maplibregl . Map ( mapOptions ) ;
45
+ const mockMarker = new maplibregl . Marker ( ) ;
46
46
47
47
const mockClient = getMockClientWithEventEmitter ( {
48
48
getClientWellKnown : jest . fn ( ) . mockReturnValue ( {
@@ -64,14 +64,16 @@ describe("<BeaconMarker />", () => {
64
64
const defaultEvent = makeBeaconInfoEvent ( aliceId , roomId , { isLive : true } , "$alice-room1-1" ) ;
65
65
const notLiveEvent = makeBeaconInfoEvent ( aliceId , roomId , { isLive : false } , "$alice-room1-2" ) ;
66
66
67
+ const geoUri1 = "geo:51,41" ;
67
68
const location1 = makeBeaconEvent ( aliceId , {
68
69
beaconInfoId : defaultEvent . getId ( ) ,
69
- geoUri : "geo:51,41" ,
70
+ geoUri : geoUri1 ,
70
71
timestamp : now + 1 ,
71
72
} ) ;
73
+ const geoUri2 = "geo:52,42" ;
72
74
const location2 = makeBeaconEvent ( aliceId , {
73
75
beaconInfoId : defaultEvent . getId ( ) ,
74
- geoUri : "geo:52,42" ,
76
+ geoUri : geoUri2 ,
75
77
timestamp : now + 10000 ,
76
78
} ) ;
77
79
@@ -80,11 +82,15 @@ describe("<BeaconMarker />", () => {
80
82
beacon : new Beacon ( defaultEvent ) ,
81
83
} ;
82
84
83
- const getComponent = ( props = { } ) =>
84
- mount ( < BeaconMarker { ...defaultProps } { ...props } /> , {
85
- wrappingComponent : MatrixClientContext . Provider ,
86
- wrappingComponentProps : { value : mockClient } ,
85
+ const renderComponent = ( props = { } ) => {
86
+ const Wrapper = ( wrapperProps = { } ) => {
87
+ return < MatrixClientContext . Provider value = { mockClient } { ...wrapperProps } /> ;
88
+ } ;
89
+
90
+ return render ( < BeaconMarker { ...defaultProps } { ...props } /> , {
91
+ wrapper : Wrapper ,
87
92
} ) ;
93
+ } ;
88
94
89
95
beforeEach ( ( ) => {
90
96
jest . clearAllMocks ( ) ;
@@ -93,38 +99,45 @@ describe("<BeaconMarker />", () => {
93
99
it ( "renders nothing when beacon is not live" , ( ) => {
94
100
const room = setupRoom ( [ notLiveEvent ] ) ;
95
101
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( notLiveEvent ) ) ;
96
- const component = getComponent ( { beacon } ) ;
97
- expect ( component . html ( ) ) . toBe ( null ) ;
102
+ const { asFragment } = renderComponent ( { beacon } ) ;
103
+ expect ( asFragment ( ) ) . toMatchInlineSnapshot ( `<DocumentFragment />` ) ;
104
+ expect ( screen . queryByTestId ( "avatar-img" ) ) . not . toBeInTheDocument ( ) ;
98
105
} ) ;
99
106
100
107
it ( "renders nothing when beacon has no location" , ( ) => {
101
108
const room = setupRoom ( [ defaultEvent ] ) ;
102
109
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ;
103
- const component = getComponent ( { beacon } ) ;
104
- expect ( component . html ( ) ) . toBe ( null ) ;
110
+ const { asFragment } = renderComponent ( { beacon } ) ;
111
+ expect ( asFragment ( ) ) . toMatchInlineSnapshot ( `<DocumentFragment />` ) ;
112
+ expect ( screen . queryByTestId ( "avatar-img" ) ) . not . toBeInTheDocument ( ) ;
105
113
} ) ;
106
114
107
115
it ( "renders marker when beacon has location" , ( ) => {
108
116
const room = setupRoom ( [ defaultEvent ] ) ;
109
117
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ;
110
- beacon . addLocations ( [ location1 ] ) ;
111
- const component = getComponent ( { beacon } ) ;
112
- expect ( component ) . toMatchSnapshot ( ) ;
118
+ beacon ?. addLocations ( [ location1 ] ) ;
119
+ const { asFragment } = renderComponent ( { beacon } ) ;
120
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
121
+ expect ( screen . getByTestId ( "avatar-img" ) ) . toBeInTheDocument ( ) ;
113
122
} ) ;
114
123
115
124
it ( "updates with new locations" , ( ) => {
125
+ const lonLat1 = { lon : 41 , lat : 51 } ;
126
+ const lonLat2 = { lon : 42 , lat : 52 } ;
116
127
const room = setupRoom ( [ defaultEvent ] ) ;
117
128
const beacon = room . currentState . beacons . get ( getBeaconInfoIdentifier ( defaultEvent ) ) ;
118
- beacon . addLocations ( [ location1 ] ) ;
119
- const component = getComponent ( { beacon } ) ;
120
- expect ( component . find ( "SmartMarker" ) . props ( ) [ "geoUri" ] ) . toEqual ( "geo:51,41" ) ;
129
+ beacon ?. addLocations ( [ location1 ] ) ;
121
130
131
+ // render the component then add a new location, check mockMarker called as expected
132
+ renderComponent ( { beacon } ) ;
133
+ expect ( mockMarker . setLngLat ) . toHaveBeenLastCalledWith ( lonLat1 ) ;
134
+ expect ( mockMarker . addTo ) . toHaveBeenCalledWith ( mockMap ) ;
135
+
136
+ // add a location, check mockMarker called with new location details
122
137
act ( ( ) => {
123
- beacon . addLocations ( [ location2 ] ) ;
138
+ beacon ? .addLocations ( [ location2 ] ) ;
124
139
} ) ;
125
- component . setProps ( { } ) ;
126
-
127
- // updated to latest location
128
- expect ( component . find ( "SmartMarker" ) . props ( ) [ "geoUri" ] ) . toEqual ( "geo:52,42" ) ;
140
+ expect ( mockMarker . setLngLat ) . toHaveBeenLastCalledWith ( lonLat2 ) ;
141
+ expect ( mockMarker . addTo ) . toHaveBeenCalledWith ( mockMap ) ;
129
142
} ) ;
130
143
} ) ;
0 commit comments