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

Commit cd224b5

Browse files
author
Kerry Archibald
committed
fix tests for weird asyncstore closure issues
Signed-off-by: Kerry Archibald <[email protected]>
1 parent bb5a5cb commit cd224b5

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/stores/OwnBeaconStore.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
5050
}
5151

5252
protected async onNotReady() {
53-
await this.reset({});
53+
this.matrixClient.removeListener(BeaconEvent.LivenessChange, this.onBeaconLiveness);
54+
this.matrixClient.removeListener(BeaconEvent.New, this.onNewBeacon);
55+
56+
this.beacons.forEach(beacon => beacon.destroy());
57+
58+
this.beacons.clear();
59+
this.beaconsByRoomId.clear();
60+
this.liveBeaconIds = [];
5461
}
5562

56-
protected async onReady() {
63+
protected async onReady(): Promise<void> {
5764
this.matrixClient.on(BeaconEvent.LivenessChange, this.onBeaconLiveness.bind(this));
5865
this.matrixClient.on(BeaconEvent.New, this.onNewBeacon.bind(this));
66+
5967
this.initialiseBeaconState();
6068
}
6169

test/stores/OwnBeaconStore-test.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
import { Room, Beacon, BeaconEvent } from "matrix-js-sdk/src/matrix";
1818

1919
import { OwnBeaconStore, OwnBeaconStoreEvent } from "../../src/stores/OwnBeaconStore";
20+
import { resetAsyncStoreWithClient, setupAsyncStoreWithClient } from "../test-utils";
2021
import { makeBeaconInfoEvent } from "../test-utils/beacon";
2122
import { getMockClientWithEventEmitter } from "../test-utils/client";
2223

@@ -69,15 +70,26 @@ describe('OwnBeaconStore', () => {
6970
};
7071

7172
const makeOwnBeaconStore = async () => {
72-
const store = new OwnBeaconStore();
73-
// @ts-ignore
74-
await store.onReady();
73+
const store = OwnBeaconStore.instance;
74+
// await resetAsyncStoreWithClient(store)
75+
76+
await setupAsyncStoreWithClient(store, mockClient);
7577
return store;
7678
};
7779

7880
beforeEach(() => {
7981
mockClient.getVisibleRooms.mockReturnValue([]);
8082
jest.spyOn(global.Date, 'now').mockReturnValue(now);
83+
jest.spyOn(OwnBeaconStore.instance, 'emit').mockRestore();
84+
});
85+
86+
afterEach(async () => {
87+
await resetAsyncStoreWithClient(OwnBeaconStore.instance);
88+
});
89+
90+
it('works', async () => {
91+
const store = await makeOwnBeaconStore();
92+
expect(store.hasLiveBeacons()).toBe(false);
8193
});
8294

8395
describe('onReady()', () => {
@@ -112,6 +124,31 @@ describe('OwnBeaconStore', () => {
112124
});
113125
});
114126

127+
describe('onNotReady()', () => {
128+
it('removes listeners', async () => {
129+
const store = await makeOwnBeaconStore();
130+
const removeSpy = jest.spyOn(mockClient, 'removeListener');
131+
// @ts-ignore
132+
store.onNotReady();
133+
134+
expect(removeSpy.mock.calls[0]).toEqual(expect.arrayContaining([BeaconEvent.LivenessChange]));
135+
expect(removeSpy.mock.calls[1]).toEqual(expect.arrayContaining([BeaconEvent.New]));
136+
});
137+
138+
it('destroys beacons', async () => {
139+
const [room1] = makeRoomsWithStateEvents([
140+
alicesRoom1BeaconInfo,
141+
]);
142+
const store = await makeOwnBeaconStore();
143+
const beacon = room1.currentState.beacons.get(alicesRoom1BeaconInfo.getId());
144+
const destroySpy = jest.spyOn(beacon, 'destroy');
145+
// @ts-ignore
146+
store.onNotReady();
147+
148+
expect(destroySpy).toHaveBeenCalled();
149+
});
150+
});
151+
115152
describe('hasLiveBeacons()', () => {
116153
beforeEach(() => {
117154
makeRoomsWithStateEvents([

0 commit comments

Comments
 (0)