@@ -15,14 +15,11 @@ limitations under the License.
15
15
*/
16
16
17
17
import React , { ComponentProps } from "react" ;
18
- // eslint-disable-next-line deprecate/import
19
- import { mount } from "enzyme" ;
18
+ import { fireEvent , render } from "@testing-library/react" ;
20
19
import { LocationAssetType } from "matrix-js-sdk/src/@types/location" ;
21
20
import { ClientEvent , RoomMember } from "matrix-js-sdk/src/matrix" ;
22
21
import * as maplibregl from "maplibre-gl" ;
23
22
import { logger } from "matrix-js-sdk/src/logger" ;
24
- // eslint-disable-next-line deprecate/import
25
- import { act } from "react-dom/test-utils" ;
26
23
import { SyncState } from "matrix-js-sdk/src/sync" ;
27
24
28
25
import MLocationBody from "../../../../src/components/views/messages/MLocationBody" ;
@@ -35,6 +32,13 @@ import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
35
32
import { makeLocationEvent } from "../../../test-utils/location" ;
36
33
import { getMockClientWithEventEmitter } from "../../../test-utils" ;
37
34
35
+ // Fake random strings to give a predictable snapshot
36
+ jest . mock ( "matrix-js-sdk/src/randomstring" , ( ) => {
37
+ return {
38
+ randomString : ( ) => "abdefghi" ,
39
+ } ;
40
+ } ) ;
41
+
38
42
describe ( "MLocationBody" , ( ) => {
39
43
const mapOptions = { container : { } as unknown as HTMLElement , style : "" } ;
40
44
describe ( "<MLocationBody>" , ( ) => {
@@ -57,10 +61,11 @@ describe("MLocationBody", () => {
57
61
mediaEventHelper : { } as MediaEventHelper ,
58
62
} ;
59
63
const getComponent = ( props = { } ) =>
60
- mount ( < MLocationBody { ...defaultProps } { ...props } /> , {
61
- wrappingComponent : MatrixClientContext . Provider ,
62
- wrappingComponentProps : { value : mockClient } ,
63
- } ) ;
64
+ render (
65
+ < MatrixClientContext . Provider value = { mockClient } >
66
+ < MLocationBody { ...defaultProps } { ...props } />
67
+ </ MatrixClientContext . Provider > ,
68
+ ) ;
64
69
const getMapErrorComponent = ( ) => {
65
70
const mockMap = new maplibregl . Map ( mapOptions ) ;
66
71
mockClient . getClientWellKnown . mockReturnValue ( {
@@ -96,20 +101,19 @@ describe("MLocationBody", () => {
96
101
97
102
it ( "displays correct fallback content without error style when map_style_url is not configured" , ( ) => {
98
103
const component = getComponent ( ) ;
99
- expect ( component . find ( ".mx_EventTile_body" ) ) . toMatchSnapshot ( ) ;
104
+ expect ( component . container . querySelector ( ".mx_EventTile_body" ) ) . toMatchSnapshot ( ) ;
100
105
} ) ;
101
106
102
107
it ( "displays correct fallback content when map_style_url is misconfigured" , ( ) => {
103
108
const component = getMapErrorComponent ( ) ;
104
- component . setProps ( { } ) ;
105
- expect ( component . find ( ".mx_EventTile_body" ) ) . toMatchSnapshot ( ) ;
109
+ expect ( component . container . querySelector ( ".mx_EventTile_body" ) ) . toMatchSnapshot ( ) ;
106
110
} ) ;
107
111
108
112
it ( "should clear the error on reconnect" , ( ) => {
109
113
const component = getMapErrorComponent ( ) ;
110
- expect ( ( component . state ( ) as React . ComponentState ) . error ) . toBeDefined ( ) ;
114
+ expect ( component . container . querySelector ( ".mx_EventTile_tileError" ) ) . toBeDefined ( ) ;
111
115
mockClient . emit ( ClientEvent . Sync , SyncState . Reconnecting , SyncState . Error ) ;
112
- expect ( ( component . state ( ) as React . ComponentState ) . error ) . toBeUndefined ( ) ;
116
+ expect ( component . container . querySelector ( ".mx_EventTile_tileError" ) ) . toBeFalsy ( ) ;
113
117
} ) ;
114
118
} ) ;
115
119
@@ -132,40 +136,25 @@ describe("MLocationBody", () => {
132
136
const mockMap = new maplibregl . Map ( mapOptions ) ;
133
137
const component = getComponent ( ) ;
134
138
135
- expect ( component ) . toMatchSnapshot ( ) ;
139
+ expect ( component . asFragment ( ) ) . toMatchSnapshot ( ) ;
136
140
// map was centered
137
141
expect ( mockMap . setCenter ) . toHaveBeenCalledWith ( {
138
142
lat : 51.5076 ,
139
143
lon : - 0.1276 ,
140
144
} ) ;
141
145
} ) ;
142
146
143
- it ( "opens map dialog on click" , ( ) => {
147
+ it ( "opens map dialog on click" , async ( ) => {
144
148
const modalSpy = jest
145
149
. spyOn ( Modal , "createDialog" )
146
150
. mockReturnValue ( { finished : new Promise ( ( ) => { } ) , close : jest . fn ( ) } ) ;
147
151
const component = getComponent ( ) ;
148
152
149
- act ( ( ) => {
150
- component . find ( "Map" ) . at ( 0 ) . simulate ( "click" ) ;
151
- } ) ;
153
+ await fireEvent . click ( component . container . querySelector ( ".mx_Map" ) ! ) ;
152
154
153
155
expect ( modalSpy ) . toHaveBeenCalled ( ) ;
154
156
} ) ;
155
157
156
- it ( "renders marker correctly for a non-self share" , ( ) => {
157
- const mockMap = new maplibregl . Map ( mapOptions ) ;
158
- const component = getComponent ( ) ;
159
-
160
- expect ( component . find ( "SmartMarker" ) . at ( 0 ) . props ( ) ) . toEqual (
161
- expect . objectContaining ( {
162
- map : mockMap ,
163
- geoUri : "geo:51.5076,-0.1276" ,
164
- roomMember : undefined ,
165
- } ) ,
166
- ) ;
167
- } ) ;
168
-
169
158
it ( "renders marker correctly for a self share" , ( ) => {
170
159
const selfShareEvent = makeLocationEvent ( "geo:51.5076,-0.1276" , LocationAssetType . Self ) ;
171
160
const member = new RoomMember ( roomId , userId ) ;
@@ -174,7 +163,7 @@ describe("MLocationBody", () => {
174
163
const component = getComponent ( { mxEvent : selfShareEvent } ) ;
175
164
176
165
// render self locations with user avatars
177
- expect ( component . find ( "SmartMarker" ) . at ( 0 ) . prop ( "roomMember" ) ) . toEqual ( member ) ;
166
+ expect ( component . asFragment ( ) ) . toMatchSnapshot ( ) ;
178
167
} ) ;
179
168
} ) ;
180
169
} ) ;
0 commit comments