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

Commit 70cdd57

Browse files
author
Kerry
authored
Live location sharing: move test utils into utils (#8365)
* move makeRoomWithState events to test utils Signed-off-by: Kerry Archibald <[email protected]> * move beacon test helpers into utils Signed-off-by: Kerry Archibald <[email protected]> * remove file Signed-off-by: Kerry Archibald <[email protected]> * more types Signed-off-by: Kerry Archibald <[email protected]>
1 parent 56cf921 commit 70cdd57

File tree

5 files changed

+84
-47
lines changed

5 files changed

+84
-47
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ import {
2222
Beacon,
2323
Room,
2424
RoomMember,
25+
MatrixEvent,
2526
getBeaconInfoIdentifier,
2627
} from 'matrix-js-sdk/src/matrix';
2728

2829
import BeaconMarker from '../../../../src/components/views/beacon/BeaconMarker';
2930
import MatrixClientContext from '../../../../src/contexts/MatrixClientContext';
30-
import { getMockClientWithEventEmitter, makeBeaconEvent, makeBeaconInfoEvent } from '../../../test-utils';
31+
import {
32+
getMockClientWithEventEmitter,
33+
makeBeaconEvent,
34+
makeBeaconInfoEvent,
35+
makeRoomWithStateEvents,
36+
} from '../../../test-utils';
3137
import { TILE_SERVER_WK_KEY } from '../../../../src/utils/WellKnownUtils';
3238

3339
describe('<BeaconMarker />', () => {
@@ -53,13 +59,9 @@ describe('<BeaconMarker />', () => {
5359

5460
// make fresh rooms every time
5561
// as we update room state
56-
const makeRoomWithStateEvents = (stateEvents = []): Room => {
57-
const room1 = new Room(roomId, mockClient, aliceId);
58-
59-
room1.currentState.setStateEvents(stateEvents);
62+
const setupRoom = (stateEvents: MatrixEvent[] = []): Room => {
63+
const room1 = makeRoomWithStateEvents(stateEvents, { roomId, mockClient });
6064
jest.spyOn(room1, 'getMember').mockReturnValue(aliceMember);
61-
mockClient.getRoom.mockReturnValue(room1);
62-
6365
return room1;
6466
};
6567

@@ -97,29 +99,29 @@ describe('<BeaconMarker />', () => {
9799
});
98100

99101
it('renders nothing when beacon is not live', () => {
100-
const room = makeRoomWithStateEvents([notLiveEvent]);
102+
const room = setupRoom([notLiveEvent]);
101103
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(notLiveEvent));
102104
const component = getComponent({ beacon });
103105
expect(component.html()).toBe(null);
104106
});
105107

106108
it('renders nothing when beacon has no location', () => {
107-
const room = makeRoomWithStateEvents([defaultEvent]);
109+
const room = setupRoom([defaultEvent]);
108110
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
109111
const component = getComponent({ beacon });
110112
expect(component.html()).toBe(null);
111113
});
112114

113115
it('renders marker when beacon has location', () => {
114-
const room = makeRoomWithStateEvents([defaultEvent]);
116+
const room = setupRoom([defaultEvent]);
115117
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
116118
beacon.addLocations([location1]);
117119
const component = getComponent({ beacon });
118120
expect(component).toMatchSnapshot();
119121
});
120122

121123
it('updates with new locations', () => {
122-
const room = makeRoomWithStateEvents([defaultEvent]);
124+
const room = setupRoom([defaultEvent]);
123125
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
124126
beacon.addLocations([location1]);
125127
const component = getComponent({ beacon });

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { mount } from 'enzyme';
1919
import { act } from 'react-dom/test-utils';
2020
import {
2121
MatrixClient,
22+
MatrixEvent,
2223
Room,
2324
RoomMember,
2425
getBeaconInfoIdentifier,
@@ -30,6 +31,7 @@ import {
3031
getMockClientWithEventEmitter,
3132
makeBeaconEvent,
3233
makeBeaconInfoEvent,
34+
makeRoomWithStateEvents,
3335
} from '../../../test-utils';
3436
import { TILE_SERVER_WK_KEY } from '../../../../src/utils/WellKnownUtils';
3537

@@ -55,12 +57,9 @@ describe('<BeaconViewDialog />', () => {
5557

5658
// make fresh rooms every time
5759
// as we update room state
58-
const makeRoomWithStateEvents = (stateEvents = []): Room => {
59-
const room1 = new Room(roomId, mockClient, aliceId);
60-
61-
room1.currentState.setStateEvents(stateEvents);
60+
const setupRoom = (stateEvents: MatrixEvent[] = []): Room => {
61+
const room1 = makeRoomWithStateEvents(stateEvents, { roomId, mockClient });
6262
jest.spyOn(room1, 'getMember').mockReturnValue(aliceMember);
63-
mockClient.getRoom.mockReturnValue(room1);
6463

6564
return room1;
6665
};
@@ -85,7 +84,7 @@ describe('<BeaconViewDialog />', () => {
8584
mount(<BeaconViewDialog {...defaultProps} {...props} />);
8685

8786
it('renders a map with markers', () => {
88-
const room = makeRoomWithStateEvents([defaultEvent]);
87+
const room = setupRoom([defaultEvent]);
8988
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
9089
beacon.addLocations([location1]);
9190
const component = getComponent();
@@ -97,7 +96,7 @@ describe('<BeaconViewDialog />', () => {
9796
});
9897

9998
it('updates markers on changes to beacons', () => {
100-
const room = makeRoomWithStateEvents([defaultEvent]);
99+
const room = setupRoom([defaultEvent]);
101100
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
102101
beacon.addLocations([location1]);
103102
const component = getComponent();
@@ -122,7 +121,7 @@ describe('<BeaconViewDialog />', () => {
122121

123122
it('renders a fallback when no live beacons remain', () => {
124123
const onFinished = jest.fn();
125-
const room = makeRoomWithStateEvents([defaultEvent]);
124+
const room = setupRoom([defaultEvent]);
126125
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
127126
beacon.addLocations([location1]);
128127
const component = getComponent({ onFinished });

test/components/views/messages/MBeaconBody-test.tsx

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ import { act } from 'react-dom/test-utils';
2020
import maplibregl from 'maplibre-gl';
2121
import {
2222
BeaconEvent,
23-
Room,
2423
getBeaconInfoIdentifier,
2524
} from 'matrix-js-sdk/src/matrix';
2625

2726
import MBeaconBody from '../../../../src/components/views/messages/MBeaconBody';
28-
import { getMockClientWithEventEmitter, makeBeaconEvent, makeBeaconInfoEvent } from '../../../test-utils';
27+
import {
28+
getMockClientWithEventEmitter,
29+
makeBeaconEvent,
30+
makeBeaconInfoEvent,
31+
makeRoomWithStateEvents,
32+
} from '../../../test-utils';
2933
import { RoomPermalinkCreator } from '../../../../src/utils/permalinks/Permalinks';
3034
import { MediaEventHelper } from '../../../../src/utils/MediaEventHelper';
3135
import MatrixClientContext from '../../../../src/contexts/MatrixClientContext';
@@ -51,17 +55,6 @@ describe('<MBeaconBody />', () => {
5155
getRoom: jest.fn(),
5256
});
5357

54-
// make fresh rooms every time
55-
// as we update room state
56-
const makeRoomWithStateEvents = (stateEvents = []): Room => {
57-
const room1 = new Room(roomId, mockClient, aliceId);
58-
59-
room1.currentState.setStateEvents(stateEvents);
60-
mockClient.getRoom.mockReturnValue(room1);
61-
62-
return room1;
63-
};
64-
6558
const defaultEvent = makeBeaconInfoEvent(aliceId,
6659
roomId,
6760
{ isLive: true },
@@ -96,7 +89,7 @@ describe('<MBeaconBody />', () => {
9689
{ isLive: false },
9790
'$alice-room1-1',
9891
);
99-
makeRoomWithStateEvents([beaconInfoEvent]);
92+
makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
10093
const component = getComponent({ mxEvent: beaconInfoEvent });
10194
expect(component.text()).toEqual("Live location ended");
10295
});
@@ -108,7 +101,7 @@ describe('<MBeaconBody />', () => {
108101
{ isLive: true, timestamp: now - 600000, timeout: 500 },
109102
'$alice-room1-1',
110103
);
111-
makeRoomWithStateEvents([beaconInfoEvent]);
104+
makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
112105
const component = getComponent({ mxEvent: beaconInfoEvent });
113106
expect(component.text()).toEqual("Live location ended");
114107
});
@@ -120,7 +113,7 @@ describe('<MBeaconBody />', () => {
120113
{ isLive: true, timestamp: now - 600000, timeout: 500 },
121114
'$alice-room1-1',
122115
);
123-
makeRoomWithStateEvents([beaconInfoEvent]);
116+
makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
124117
const component = getComponent({ mxEvent: beaconInfoEvent });
125118
act(() => {
126119
component.find('.mx_MBeaconBody_map').simulate('click');
@@ -145,7 +138,7 @@ describe('<MBeaconBody />', () => {
145138
'$alice-room1-2',
146139
);
147140

148-
makeRoomWithStateEvents([aliceBeaconInfo1, aliceBeaconInfo2]);
141+
makeRoomWithStateEvents([aliceBeaconInfo1, aliceBeaconInfo2], { roomId, mockClient });
149142

150143
const component = getComponent({ mxEvent: aliceBeaconInfo1 });
151144
// beacon1 has been superceded by beacon2
@@ -168,7 +161,7 @@ describe('<MBeaconBody />', () => {
168161
'$alice-room1-2',
169162
);
170163

171-
const room = makeRoomWithStateEvents([aliceBeaconInfo1]);
164+
const room = makeRoomWithStateEvents([aliceBeaconInfo1], { roomId, mockClient });
172165
const component = getComponent({ mxEvent: aliceBeaconInfo1 });
173166

174167
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo1));
@@ -193,7 +186,7 @@ describe('<MBeaconBody />', () => {
193186
'$alice-room1-1',
194187
);
195188

196-
const room = makeRoomWithStateEvents([aliceBeaconInfo]);
189+
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
197190
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo));
198191
const component = getComponent({ mxEvent: aliceBeaconInfo });
199192

@@ -226,14 +219,14 @@ describe('<MBeaconBody />', () => {
226219
);
227220

228221
it('renders a live beacon without a location correctly', () => {
229-
makeRoomWithStateEvents([aliceBeaconInfo]);
222+
makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
230223
const component = getComponent({ mxEvent: aliceBeaconInfo });
231224

232225
expect(component.text()).toEqual("Loading live location...");
233226
});
234227

235228
it('does nothing on click when a beacon has no location', () => {
236-
makeRoomWithStateEvents([aliceBeaconInfo]);
229+
makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
237230
const component = getComponent({ mxEvent: aliceBeaconInfo });
238231

239232
act(() => {
@@ -244,7 +237,7 @@ describe('<MBeaconBody />', () => {
244237
});
245238

246239
it('renders a live beacon with a location correctly', () => {
247-
const room = makeRoomWithStateEvents([aliceBeaconInfo]);
240+
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
248241
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo));
249242
beaconInstance.addLocations([location1]);
250243
const component = getComponent({ mxEvent: aliceBeaconInfo });
@@ -253,7 +246,7 @@ describe('<MBeaconBody />', () => {
253246
});
254247

255248
it('opens maximised map view on click when beacon has a live location', () => {
256-
const room = makeRoomWithStateEvents([aliceBeaconInfo]);
249+
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
257250
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo));
258251
beaconInstance.addLocations([location1]);
259252
const component = getComponent({ mxEvent: aliceBeaconInfo });
@@ -267,7 +260,7 @@ describe('<MBeaconBody />', () => {
267260
});
268261

269262
it('does nothing on click when a beacon has no location', () => {
270-
makeRoomWithStateEvents([aliceBeaconInfo]);
263+
makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
271264
const component = getComponent({ mxEvent: aliceBeaconInfo });
272265

273266
act(() => {
@@ -278,7 +271,7 @@ describe('<MBeaconBody />', () => {
278271
});
279272

280273
it('renders a live beacon with a location correctly', () => {
281-
const room = makeRoomWithStateEvents([aliceBeaconInfo]);
274+
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
282275
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo));
283276
beaconInstance.addLocations([location1]);
284277
const component = getComponent({ mxEvent: aliceBeaconInfo });
@@ -287,7 +280,7 @@ describe('<MBeaconBody />', () => {
287280
});
288281

289282
it('opens maximised map view on click when beacon has a live location', () => {
290-
const room = makeRoomWithStateEvents([aliceBeaconInfo]);
283+
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
291284
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo));
292285
beaconInstance.addLocations([location1]);
293286
const component = getComponent({ mxEvent: aliceBeaconInfo });
@@ -301,7 +294,7 @@ describe('<MBeaconBody />', () => {
301294
});
302295

303296
it('updates latest location', () => {
304-
const room = makeRoomWithStateEvents([aliceBeaconInfo]);
297+
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
305298
const component = getComponent({ mxEvent: aliceBeaconInfo });
306299

307300
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo));

test/test-utils/beacon.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ limitations under the License.
1616

1717
import { MockedObject } from "jest-mock";
1818
import { makeBeaconInfoContent, makeBeaconContent } from "matrix-js-sdk/src/content-helpers";
19-
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
19+
import {
20+
MatrixClient,
21+
MatrixEvent,
22+
Beacon,
23+
getBeaconInfoIdentifier,
24+
} from "matrix-js-sdk/src/matrix";
2025
import { M_BEACON, M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
2126
import { LocationAssetType } from "matrix-js-sdk/src/@types/location";
2227

2328
import { getMockGeolocationPositionError } from "./location";
29+
import { makeRoomWithStateEvents } from "./room";
2430

2531
type InfoContentProps = {
2632
timeout: number;
@@ -182,3 +188,22 @@ export const watchPositionMockImplementation = (delays: number[], errorCodes: nu
182188
});
183189
};
184190
};
191+
192+
/**
193+
* Creates a room with beacon events
194+
* sets given locations on beacons
195+
* returns beacons
196+
*/
197+
export const makeRoomWithBeacons = (
198+
roomId: string,
199+
mockClient: MockedObject<MatrixClient>,
200+
beaconInfoEvents: MatrixEvent[],
201+
locationEvents?: MatrixEvent[],
202+
): Beacon[] => {
203+
const room = makeRoomWithStateEvents(beaconInfoEvents, { roomId, mockClient });
204+
const beacons = beaconInfoEvents.map(event => room.currentState.beacons.get(getBeaconInfoIdentifier(event)));
205+
if (locationEvents) {
206+
beacons.forEach(beacon => beacon.addLocations(locationEvents));
207+
}
208+
return beacons;
209+
};

test/test-utils/room.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
import { MockedObject } from "jest-mock";
1718
import {
19+
MatrixClient,
20+
MatrixEvent,
1821
EventType,
22+
Room,
1923
} from "matrix-js-sdk/src/matrix";
2024

2125
import { mkEvent } from "./test-utils";
@@ -32,3 +36,17 @@ export const makeMembershipEvent = (
3236
ts: Date.now(),
3337
});
3438

39+
/**
40+
* Creates a room
41+
* sets state events on the room
42+
* Sets client getRoom to return room
43+
* returns room
44+
*/
45+
export const makeRoomWithStateEvents = (
46+
stateEvents: MatrixEvent[] = [],
47+
{ roomId, mockClient }: { roomId: string, mockClient: MockedObject<MatrixClient>}): Room => {
48+
const room1 = new Room(roomId, mockClient, '@user:server.org');
49+
room1.currentState.setStateEvents(stateEvents);
50+
mockClient.getRoom.mockReturnValue(room1);
51+
return room1;
52+
};

0 commit comments

Comments
 (0)