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

Commit 3a9d931

Browse files
author
Kerry Archibald
committed
move advanceDateAndTime to utils, tidy
Signed-off-by: Kerry Archibald <[email protected]>
1 parent bc18bd2 commit 3a9d931

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

src/stores/OwnBeaconStore.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
BeaconInfoState, makeBeaconContent, makeBeaconInfoContent,
2626
} from "matrix-js-sdk/src/content-helpers";
2727
import { M_BEACON } from "matrix-js-sdk/src/@types/beacon";
28+
import { logger } from "matrix-js-sdk/src/logger";
2829

2930
import defaultDispatcher from "../dispatcher/dispatcher";
3031
import { ActionPayload } from "../dispatcher/payloads";
@@ -63,10 +64,11 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
6364
private geolocationError: GeolocationError | undefined;
6465
private clearPositionWatch: ClearWatchCallback | undefined;
6566
/**
66-
* Track the last published position and when it was published
67-
* so it can be republished while the user is static
67+
* Track when the last position was published
68+
* So we can manually get position on slow interval
69+
* when the target is status
6870
*/
69-
private lastPublishedPosition: { position: TimedGeoUri, publishedTimestamp: number } | undefined;
71+
private lastPublishedPositionTimestamp: number | undefined;
7072

7173
public constructor() {
7274
super(defaultDispatcher);
@@ -241,13 +243,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
241243
this.clearPositionWatch = await watchPosition(this.onWatchedPosition, this.onWatchedPositionError);
242244

243245
this.locationInterval = setInterval(() => {
244-
if (!this.lastPublishedPosition) {
246+
if (!this.lastPublishedPositionTimestamp) {
245247
return;
246248
}
247-
const { publishedTimestamp } = this.lastPublishedPosition;
248249
// if position was last updated STATIC_UPDATE_INTERVAL ms ago or more
249250
// get our position and publish it
250-
if (publishedTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
251+
if (this.lastPublishedPositionTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
251252
this.publishCurrentLocationToBeacons();
252253
}
253254
}, STATIC_UPDATE_INTERVAL);
@@ -257,7 +258,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
257258
const timedGeoPosition = mapGeolocationPositionToTimedGeo(position);
258259

259260
// if this is our first position, publish immediateley
260-
if (!this.lastPublishedPosition) {
261+
if (!this.lastPublishedPositionTimestamp) {
261262
this.publishLocationToBeacons(timedGeoPosition);
262263
} else {
263264
this.debouncedPublishLocationToBeacons(timedGeoPosition);
@@ -266,13 +267,13 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
266267

267268
private onWatchedPositionError = (error: GeolocationError) => {
268269
this.geolocationError = error;
269-
console.log(this.geolocationError);
270+
logger.error(this.geolocationError);
270271
};
271272

272273
private stopPollingLocation = () => {
273274
clearInterval(this.locationInterval);
274275
this.locationInterval = undefined;
275-
this.lastPublishedPosition = undefined;
276+
this.lastPublishedPositionTimestamp = undefined;
276277
this.geolocationError = undefined;
277278

278279
if (this.clearPositionWatch) {
@@ -286,7 +287,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
286287
* Sets last published beacon
287288
*/
288289
private publishLocationToBeacons = async (position: TimedGeoUri) => {
289-
this.lastPublishedPosition = { position, publishedTimestamp: Date.now() };
290+
this.lastPublishedPositionTimestamp = Date.now();
290291
// TODO handle failure in individual beacon without rejecting rest
291292
await Promise.all(this.liveBeaconIds.map(beaconId =>
292293
this.sendLocationToBeacon(this.beacons.get(beaconId), position)),

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import '../../../skinned-sdk';
2323
import RoomLiveShareWarning from '../../../../src/components/views/beacon/RoomLiveShareWarning';
2424
import { OwnBeaconStore } from '../../../../src/stores/OwnBeaconStore';
2525
import {
26+
advanceDateAndTime,
2627
findByTestId,
2728
getMockClientWithEventEmitter,
2829
makeBeaconInfoEvent,
@@ -72,14 +73,6 @@ describe('<RoomLiveShareWarning />', () => {
7273
return [room1, room2];
7374
};
7475

75-
const advanceDateAndTime = (ms: number) => {
76-
// bc liveness check uses Date.now we have to advance this mock
77-
jest.spyOn(global.Date, 'now').mockReturnValue(Date.now() + ms);
78-
79-
// then advance time for the interval by the same amount
80-
jest.advanceTimersByTime(ms);
81-
};
82-
8376
const makeOwnBeaconStore = async () => {
8477
const store = OwnBeaconStore.instance;
8578

test/stores/OwnBeaconStore-test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ import { makeBeaconContent } from "matrix-js-sdk/src/content-helpers";
1919
import { M_BEACON, M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
2020

2121
import { OwnBeaconStore, OwnBeaconStoreEvent } from "../../src/stores/OwnBeaconStore";
22-
import { flushPromisesWithFakeTimers, resetAsyncStoreWithClient, setupAsyncStoreWithClient } from "../test-utils";
22+
import {
23+
advanceDateAndTime,
24+
flushPromisesWithFakeTimers,
25+
resetAsyncStoreWithClient,
26+
setupAsyncStoreWithClient,
27+
} from "../test-utils";
2328
import {
2429
makeBeaconInfoEvent,
2530
makeGeolocationPosition,
@@ -107,13 +112,6 @@ describe('OwnBeaconStore', () => {
107112
return [room1, room2];
108113
};
109114

110-
const advanceDateAndTime = (ms: number) => {
111-
// bc liveness check uses Date.now we have to advance this mock
112-
jest.spyOn(global.Date, 'now').mockReturnValue(Date.now() + ms);
113-
// then advance time for the interval by the same amount
114-
jest.advanceTimersByTime(ms);
115-
};
116-
117115
const makeOwnBeaconStore = async () => {
118116
const store = OwnBeaconStore.instance;
119117

test/test-utils/utilities.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const findByTagAndTestId = findByTagAndAttr('data-test-id');
3232
export const flushPromises = async () => await new Promise(resolve => setTimeout(resolve));
3333

3434
// with jest's modern fake timers process.nextTick is also mocked,
35-
// flushing promises in the normal way waiting for some advancement
35+
// flushing promises in the normal way then waits for some advancement
3636
// of the fake timers
3737
// https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4?permalink_comment_id=4018174#gistcomment-4018174
3838
export const flushPromisesWithFakeTimers = async (): Promise<void> => {
@@ -67,3 +67,13 @@ export function waitForUpdate(inst: React.Component, updates = 1): Promise<void>
6767
};
6868
});
6969
}
70+
71+
/**
72+
* Advance jests fake timers and Date.now mock by ms
73+
* Useful for testing code using timeouts or intervals
74+
* that also checks timestamps
75+
*/
76+
export const advanceDateAndTime = (ms: number) => {
77+
jest.spyOn(global.Date, 'now').mockReturnValue(Date.now() + ms);
78+
jest.advanceTimersByTime(ms);
79+
};

0 commit comments

Comments
 (0)