Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit d141fed

Browse files
author
Kerry
authored
port BeaconViewDialog unit test to rtl (#10180)
1 parent b48d568 commit d141fed

File tree

3 files changed

+128
-143
lines changed

3 files changed

+128
-143
lines changed

src/components/views/beacon/BeaconViewDialog.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ const BeaconViewDialog: React.FC<IProps> = ({ initialFocusedBeacon, roomId, matr
158158
)}
159159
{mapDisplayError && <MapError error={mapDisplayError.message as LocationShareError} isMinimised />}
160160
{!centerGeoUri && !mapDisplayError && (
161-
<MapFallback data-test-id="beacon-view-dialog-map-fallback" className="mx_BeaconViewDialog_map">
161+
<MapFallback data-testid="beacon-view-dialog-map-fallback" className="mx_BeaconViewDialog_map">
162162
<span className="mx_BeaconViewDialog_mapFallbackMessage">{_t("No live locations")}</span>
163163
<AccessibleButton
164164
kind="primary"
165165
onClick={onFinished}
166-
data-test-id="beacon-view-dialog-fallback-close"
166+
data-testid="beacon-view-dialog-fallback-close"
167167
>
168168
{_t("Close")}
169169
</AccessibleButton>
@@ -179,7 +179,7 @@ const BeaconViewDialog: React.FC<IProps> = ({ initialFocusedBeacon, roomId, matr
179179
<AccessibleButton
180180
kind="primary"
181181
onClick={() => setSidebarOpen(true)}
182-
data-test-id="beacon-view-dialog-open-sidebar"
182+
data-testid="beacon-view-dialog-open-sidebar"
183183
className="mx_BeaconViewDialog_viewListButton"
184184
>
185185
<LiveLocationIcon height={12} />

test/components/views/beacon/BeaconViewDialog-test.tsx

Lines changed: 57 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ limitations under the License.
1515
*/
1616

1717
import React from "react";
18-
// eslint-disable-next-line deprecate/import
19-
import { mount, ReactWrapper } from "enzyme";
2018
import { act } from "react-dom/test-utils";
19+
import { fireEvent, render, RenderResult } from "@testing-library/react";
2120
import { MatrixClient, MatrixEvent, Room, RoomMember, getBeaconInfoIdentifier } from "matrix-js-sdk/src/matrix";
2221
import * as maplibregl from "maplibre-gl";
2322
import { mocked } from "jest-mock";
2423

2524
import BeaconViewDialog from "../../../../src/components/views/beacon/BeaconViewDialog";
2625
import {
27-
findByAttr,
28-
findByTestId,
2926
getMockClientWithEventEmitter,
3027
makeBeaconEvent,
3128
makeBeaconInfoEvent,
@@ -34,8 +31,6 @@ import {
3431
} from "../../../test-utils";
3532
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
3633
import { OwnBeaconStore } from "../../../../src/stores/OwnBeaconStore";
37-
import { BeaconDisplayStatus } from "../../../../src/components/views/beacon/displayStatus";
38-
import BeaconListItem from "../../../../src/components/views/beacon/BeaconListItem";
3934

4035
describe("<BeaconViewDialog />", () => {
4136
// 14.03.2022 16:15
@@ -60,6 +55,7 @@ describe("<BeaconViewDialog />", () => {
6055

6156
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
6257
const mockMap = new maplibregl.Map(mapOptions);
58+
const mockMarker = new maplibregl.Marker();
6359

6460
// make fresh rooms every time
6561
// as we update room state
@@ -84,13 +80,11 @@ describe("<BeaconViewDialog />", () => {
8480
matrixClient: mockClient as MatrixClient,
8581
};
8682

87-
const getComponent = (props = {}) => mount(<BeaconViewDialog {...defaultProps} {...props} />);
83+
const getComponent = (props = {}): RenderResult => render(<BeaconViewDialog {...defaultProps} {...props} />);
8884

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+
};
9488

9589
beforeEach(() => {
9690
jest.spyOn(OwnBeaconStore.instance, "getLiveBeaconIds").mockRestore();
@@ -103,23 +97,23 @@ describe("<BeaconViewDialog />", () => {
10397
const room = setupRoom([defaultEvent]);
10498
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
10599
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);
114108
});
115109

116110
it("does not render any own beacon status when user is not live sharing", () => {
117111
// default event belongs to alice, we are bob
118112
const room = setupRoom([defaultEvent]);
119113
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
120114
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();
123117
});
124118

125119
it("renders own beacon status when user is live sharing", () => {
@@ -130,52 +124,47 @@ describe("<BeaconViewDialog />", () => {
130124
// mock own beacon store to show default event as alice's live beacon
131125
jest.spyOn(OwnBeaconStore.instance, "getLiveBeaconIds").mockReturnValue([beacon.identifier]);
132126
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();
140129
});
141130

142-
it("updates markers on changes to beacons", () => {
131+
it("updates markers on changes to beacons", async () => {
143132
const room = setupRoom([defaultEvent]);
144133
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
145134
beacon.addLocations([location1]);
146-
const component = getComponent();
147-
expect(component.find("BeaconMarker").length).toEqual(1);
135+
const { container } = getComponent();
148136

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);
150140

141+
const anotherBeaconEvent = makeBeaconInfoEvent(bobId, roomId, { isLive: true }, "$bob-room1-1");
151142
act(() => {
152143
// emits RoomStateEvent.BeaconLiveness
153144
room.currentState.setStateEvents([anotherBeaconEvent]);
145+
const beacon2 = room.currentState.beacons.get(getBeaconInfoIdentifier(anotherBeaconEvent))!;
146+
beacon2.addLocations([location1]);
154147
});
155148

156-
component.setProps({});
157-
158149
// two markers now!
159-
expect(component.find("BeaconMarker").length).toEqual(2);
150+
expect(container.getElementsByClassName("mx_Marker").length).toEqual(2);
160151
});
161152

162153
it("does not update bounds or center on changing beacons", () => {
163154
const room = setupRoom([defaultEvent]);
164155
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
165156
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);
168159

169160
const anotherBeaconEvent = makeBeaconInfoEvent(bobId, roomId, { isLive: true }, "$bob-room1-1");
170-
171161
act(() => {
172162
// emits RoomStateEvent.BeaconLiveness
173163
room.currentState.setStateEvents([anotherBeaconEvent]);
164+
const beacon2 = room.currentState.beacons.get(getBeaconInfoIdentifier(anotherBeaconEvent))!;
165+
beacon2.addLocations([location1]);
174166
});
175-
176-
component.setProps({});
177-
178-
// two markers now!
167+
// called once on init
179168
expect(mockMap.setCenter).toHaveBeenCalledTimes(1);
180169
expect(mockMap.fitBounds).toHaveBeenCalledTimes(1);
181170
});
@@ -185,14 +174,12 @@ describe("<BeaconViewDialog />", () => {
185174
const onFinished = jest.fn();
186175
const room = setupRoom([defaultEvent]);
187176
room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
188-
const component = getComponent({ onFinished });
177+
const { getByTestId } = getComponent({ onFinished });
189178

190179
// map placeholder
191-
expect(findByTestId(component, "beacon-view-dialog-map-fallback")).toMatchSnapshot();
180+
expect(getByTestId("beacon-view-dialog-map-fallback")).toMatchSnapshot();
192181

193-
act(() => {
194-
findByTestId(component, "beacon-view-dialog-fallback-close").at(0).simulate("click");
195-
});
182+
fireEvent.click(getByTestId("beacon-view-dialog-fallback-close"));
196183

197184
expect(onFinished).toHaveBeenCalled();
198185
});
@@ -202,8 +189,8 @@ describe("<BeaconViewDialog />", () => {
202189
const room = setupRoom([defaultEvent]);
203190
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
204191
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);
207194

208195
// this will replace the defaultEvent
209196
// leading to no more live beacons
@@ -219,12 +206,10 @@ describe("<BeaconViewDialog />", () => {
219206
room.currentState.setStateEvents([anotherBeaconEvent]);
220207
});
221208

222-
component.setProps({});
223-
224209
// no more avatars
225-
expect(component.find("MemberAvatar").length).toBeFalsy();
210+
expect(container.getElementsByClassName("mx_Marker").length).toEqual(0);
226211
// map still rendered
227-
expect(component.find("Map").length).toBeTruthy();
212+
expect(container.querySelector("#mx_Map_mx_BeaconViewDialog")).toBeInTheDocument();
228213
// map location unchanged
229214
expect(mockMap.setCenter).not.toHaveBeenCalled();
230215
expect(mockMap.fitBounds).not.toHaveBeenCalled();
@@ -235,31 +220,28 @@ describe("<BeaconViewDialog />", () => {
235220
const room = setupRoom([defaultEvent]);
236221
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
237222
beacon.addLocations([location1]);
238-
const component = getComponent();
223+
const { container, getByTestId } = getComponent();
239224

240-
openSidebar(component);
225+
openSidebar(getByTestId);
241226

242-
expect(component.find("DialogSidebar").length).toBeTruthy();
227+
expect(container.querySelector(".mx_DialogSidebar")).toBeInTheDocument();
243228
});
244229

245230
it("closes sidebar on close button click", () => {
246231
const room = setupRoom([defaultEvent]);
247232
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
248233
beacon.addLocations([location1]);
249-
const component = getComponent();
234+
const { container, getByTestId } = getComponent();
250235

251236
// open the sidebar
252-
openSidebar(component);
237+
openSidebar(getByTestId);
253238

254-
expect(component.find("DialogSidebar").length).toBeTruthy();
239+
expect(container.querySelector(".mx_DialogSidebar")).toBeInTheDocument();
255240

256241
// 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"));
261243

262-
expect(component.find("DialogSidebar").length).toBeFalsy();
244+
expect(container.querySelector(".mx_DialogSidebar")).not.toBeInTheDocument();
263245
});
264246
});
265247

@@ -326,16 +308,17 @@ describe("<BeaconViewDialog />", () => {
326308
[location1, location2],
327309
);
328310

329-
const component = getComponent({ beacons: [beacon1, beacon2] });
311+
const { container, getByTestId } = getComponent({ beacons: [beacon1, beacon2] });
330312

331313
// reset call counts on map mocks after initial render
332314
jest.clearAllMocks();
333315

334-
openSidebar(component);
316+
openSidebar(getByTestId);
335317

336318
act(() => {
319+
const listItems = container.querySelectorAll(".mx_BeaconListItem");
337320
// click on the first beacon in the list
338-
component.find(BeaconListItem).at(0).simulate("click");
321+
fireEvent.click(listItems[0]!);
339322
});
340323

341324
// centered on clicked beacon
@@ -359,16 +342,17 @@ describe("<BeaconViewDialog />", () => {
359342
[location1, location2],
360343
);
361344

362-
const component = getComponent({ beacons: [beacon1, beacon2] });
345+
const { container, getByTestId } = getComponent({ beacons: [beacon1, beacon2] });
363346

364347
// reset call counts on map mocks after initial render
365348
jest.clearAllMocks();
366349

367-
openSidebar(component);
350+
openSidebar(getByTestId);
368351

369352
act(() => {
370353
// 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]!);
372356
});
373357

374358
const expectedBounds = new maplibregl.LngLatBounds([22, 33], [22, 33]);
@@ -378,7 +362,8 @@ describe("<BeaconViewDialog />", () => {
378362

379363
act(() => {
380364
// 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]!);
382367
});
383368

384369
// centered on clicked beacon

0 commit comments

Comments
 (0)