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

Commit 3bcea5f

Browse files
authored
Convert enzyme to rtl: BeaconMarker (#9840)
1 parent cb1af0d commit 3bcea5f

File tree

3 files changed

+73
-260
lines changed

3 files changed

+73
-260
lines changed

src/components/views/beacon/BeaconMarker.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ const BeaconMarker: React.FC<Props> = ({ map, beacon, tooltip }) => {
4545
return null;
4646
}
4747

48-
const geoUri = latestLocationState?.uri;
48+
const geoUri = latestLocationState.uri || "";
4949

50-
const markerRoomMember =
51-
beacon.beaconInfo.assetType === LocationAssetType.Self ? room.getMember(beacon.beaconInfoOwner) : undefined;
50+
const assetTypeIsSelf = beacon.beaconInfo?.assetType === LocationAssetType.Self;
51+
const _member = room?.getMember(beacon.beaconInfoOwner);
52+
53+
const markerRoomMember = assetTypeIsSelf && _member ? _member : undefined;
5254

5355
return (
5456
<SmartMarker

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import React from "react";
18-
// eslint-disable-next-line deprecate/import
19-
import { mount } from "enzyme";
18+
import { render, screen } from "@testing-library/react";
2019
import * as maplibregl from "maplibre-gl";
2120
import { act } from "react-dom/test-utils";
2221
import { Beacon, Room, RoomMember, MatrixEvent, getBeaconInfoIdentifier } from "matrix-js-sdk/src/matrix";
@@ -43,6 +42,7 @@ describe("<BeaconMarker />", () => {
4342

4443
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
4544
const mockMap = new maplibregl.Map(mapOptions);
45+
const mockMarker = new maplibregl.Marker();
4646

4747
const mockClient = getMockClientWithEventEmitter({
4848
getClientWellKnown: jest.fn().mockReturnValue({
@@ -64,14 +64,16 @@ describe("<BeaconMarker />", () => {
6464
const defaultEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
6565
const notLiveEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false }, "$alice-room1-2");
6666

67+
const geoUri1 = "geo:51,41";
6768
const location1 = makeBeaconEvent(aliceId, {
6869
beaconInfoId: defaultEvent.getId(),
69-
geoUri: "geo:51,41",
70+
geoUri: geoUri1,
7071
timestamp: now + 1,
7172
});
73+
const geoUri2 = "geo:52,42";
7274
const location2 = makeBeaconEvent(aliceId, {
7375
beaconInfoId: defaultEvent.getId(),
74-
geoUri: "geo:52,42",
76+
geoUri: geoUri2,
7577
timestamp: now + 10000,
7678
});
7779

@@ -80,11 +82,15 @@ describe("<BeaconMarker />", () => {
8082
beacon: new Beacon(defaultEvent),
8183
};
8284

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,
8792
});
93+
};
8894

8995
beforeEach(() => {
9096
jest.clearAllMocks();
@@ -93,38 +99,45 @@ describe("<BeaconMarker />", () => {
9399
it("renders nothing when beacon is not live", () => {
94100
const room = setupRoom([notLiveEvent]);
95101
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();
98105
});
99106

100107
it("renders nothing when beacon has no location", () => {
101108
const room = setupRoom([defaultEvent]);
102109
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();
105113
});
106114

107115
it("renders marker when beacon has location", () => {
108116
const room = setupRoom([defaultEvent]);
109117
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();
113122
});
114123

115124
it("updates with new locations", () => {
125+
const lonLat1 = { lon: 41, lat: 51 };
126+
const lonLat2 = { lon: 42, lat: 52 };
116127
const room = setupRoom([defaultEvent]);
117128
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]);
121130

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
122137
act(() => {
123-
beacon.addLocations([location2]);
138+
beacon?.addLocations([location2]);
124139
});
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);
129142
});
130143
});

0 commit comments

Comments
 (0)