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

Commit e677901

Browse files
author
Kerry
authored
Add test utils for Beacons (#8064)
* copy beacon event test utils from js-sdk Signed-off-by: Kerry Archibald <[email protected]> * mock with event emitter test util * set return type getMockClientWithEventEmitter Signed-off-by: Kerry Archibald <[email protected]> * fix lint Signed-off-by: Kerry Archibald <[email protected]> * copyright Signed-off-by: Kerry Archibald <[email protected]> * pr improvements Signed-off-by: Kerry Archibald <[email protected]>
1 parent 6ee54b6 commit e677901

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

test/test-utils/beacon.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { makeBeaconInfoContent, makeBeaconContent } from "matrix-js-sdk/src/content-helpers";
18+
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
19+
import { M_BEACON, M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
20+
import { LocationAssetType } from "matrix-js-sdk/src/@types/location";
21+
22+
type InfoContentProps = {
23+
timeout: number;
24+
isLive?: boolean;
25+
assetType?: LocationAssetType;
26+
description?: string;
27+
};
28+
const DEFAULT_INFO_CONTENT_PROPS: InfoContentProps = {
29+
timeout: 3600000,
30+
};
31+
32+
let count = 1;
33+
34+
/**
35+
* Create an m.beacon_info event
36+
* all required properties are mocked
37+
* override with contentProps
38+
*/
39+
export const makeBeaconInfoEvent = (
40+
sender: string,
41+
roomId: string,
42+
contentProps: Partial<InfoContentProps> = {},
43+
eventId?: string,
44+
): MatrixEvent => {
45+
const {
46+
timeout, isLive, description, assetType,
47+
} = {
48+
...DEFAULT_INFO_CONTENT_PROPS,
49+
...contentProps,
50+
};
51+
const event = new MatrixEvent({
52+
type: `${M_BEACON_INFO.name}.${sender}.${++count}`,
53+
room_id: roomId,
54+
state_key: sender,
55+
content: makeBeaconInfoContent(timeout, isLive, description, assetType),
56+
});
57+
58+
// live beacons use the beacon_info event id
59+
// set or default this
60+
event.replaceLocalEventId(eventId || `$${Math.random()}-${Math.random()}`);
61+
62+
return event;
63+
};
64+
65+
type ContentProps = {
66+
geoUri: string;
67+
timestamp: number;
68+
beaconInfoId: string;
69+
description?: string;
70+
};
71+
const DEFAULT_CONTENT_PROPS: ContentProps = {
72+
geoUri: 'geo:-36.24484561954707,175.46884959563613;u=10',
73+
timestamp: 123,
74+
beaconInfoId: '$123',
75+
};
76+
77+
/**
78+
* Create an m.beacon event
79+
* all required properties are mocked
80+
* override with contentProps
81+
*/
82+
export const makeBeaconEvent = (
83+
sender: string,
84+
contentProps: Partial<ContentProps> = {},
85+
): MatrixEvent => {
86+
const { geoUri, timestamp, beaconInfoId, description } = {
87+
...DEFAULT_CONTENT_PROPS,
88+
...contentProps,
89+
};
90+
91+
return new MatrixEvent({
92+
type: M_BEACON.name,
93+
sender,
94+
content: makeBeaconContent(geoUri, timestamp, beaconInfoId, description),
95+
});
96+
};
97+
98+
/**
99+
* Create a mock geolocation position
100+
* defaults all required properties
101+
*/
102+
export const makeGeolocationPosition = (
103+
{ timestamp, coords }:
104+
{ timestamp?: number, coords: Partial<GeolocationCoordinates> },
105+
): GeolocationPosition => ({
106+
timestamp: timestamp ?? 1647256791840,
107+
coords: {
108+
accuracy: 1,
109+
latitude: 54.001927,
110+
longitude: -8.253491,
111+
altitude: null,
112+
altitudeAccuracy: null,
113+
heading: null,
114+
speed: null,
115+
...coords,
116+
},
117+
});

test/test-utils/client.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import EventEmitter from "events";
18+
import { MethodKeysOf, mocked, MockedObject } from "jest-mock";
19+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
20+
21+
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
22+
23+
/**
24+
* Mock client with real event emitter
25+
* useful for testing code that listens
26+
* to MatrixClient events
27+
*/
28+
export class MockClientWithEventEmitter extends EventEmitter {
29+
constructor(mockProperties: Partial<Record<MethodKeysOf<MatrixClient>, unknown>> = {}) {
30+
super();
31+
32+
Object.assign(this, mockProperties);
33+
}
34+
}
35+
36+
/**
37+
* - make a mock client
38+
* - cast the type to mocked(MatrixClient)
39+
* - spy on MatrixClientPeg.get to return the mock
40+
* eg
41+
* ```
42+
* const mockClient = getMockClientWithEventEmitter({
43+
getUserId: jest.fn().mockReturnValue(aliceId),
44+
});
45+
* ```
46+
*/
47+
export const getMockClientWithEventEmitter = (
48+
mockProperties: Partial<Record<MethodKeysOf<MatrixClient>, unknown>>,
49+
): MockedObject<MatrixClient> => {
50+
const mock = mocked(new MockClientWithEventEmitter(mockProperties) as unknown as MatrixClient);
51+
52+
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue(mock);
53+
return mock;
54+
};
55+

0 commit comments

Comments
 (0)