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

Commit

Permalink
include /test/stores in tsconfig (#8026)
Browse files Browse the repository at this point in the history
* fix space store emits

Signed-off-by: Kerry Archibald <kerrya@element.io>

* fix spacestore-test imports

* fix export-test.tsx

Signed-off-by: Kerry Archibald <kerrya@element.io>

* include test/utils to tsconfig

Signed-off-by: Kerry Archibald <kerrya@element.io>

* fix types in SpaceStore-test

Signed-off-by: Kerry Archibald <kerrya@element.io>

* fix ts issues in rest of test/stores

Signed-off-by: Kerry Archibald <kerrya@element.io>

* include test/stores in tsconfig

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry authored Mar 11, 2022
1 parent ceb2281 commit df6c53f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 67 deletions.
115 changes: 61 additions & 54 deletions test/stores/SpaceStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { mocked } from 'jest-mock';
import { EventType } from "matrix-js-sdk/src/@types/event";
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { defer } from "matrix-js-sdk/src/utils";
import { ClientEvent, RoomEvent, MatrixEvent } from 'matrix-js-sdk/src/matrix';

import "../skinned-sdk"; // Must be first for skinning to work
import SpaceStore from "../../src/stores/spaces/SpaceStore";
Expand Down Expand Up @@ -101,8 +103,8 @@ describe("SpaceStore", () => {
const viewRoom = roomId => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: roomId }, true);

const run = async () => {
client.getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId));
client.getRoomUpgradeHistory.mockImplementation(roomId => [rooms.find(room => room.roomId === roomId)]);
mocked(client).getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId));
mocked(client).getRoomUpgradeHistory.mockImplementation(roomId => [rooms.find(room => room.roomId === roomId)]);
await testUtils.setupAsyncStoreWithClient(store, client);
jest.runOnlyPendingTimers();
};
Expand All @@ -117,7 +119,7 @@ describe("SpaceStore", () => {

beforeEach(async () => {
jest.runOnlyPendingTimers(); // run async dispatch
client.getVisibleRooms.mockReturnValue(rooms = []);
mocked(client).getVisibleRooms.mockReturnValue(rooms = []);

await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, {
[MetaSpace.Home]: true,
Expand Down Expand Up @@ -318,7 +320,7 @@ describe("SpaceStore", () => {
mkSpace(space3, [invite2]);
mkSpace(space4, [room4, fav2, space2, space3]);

client.getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId));
mocked(client).getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId));

[fav1, fav2, fav3].forEach(roomId => {
client.getRoom(roomId).tags = {
Expand All @@ -329,7 +331,7 @@ describe("SpaceStore", () => {
});

[invite1, invite2].forEach(roomId => {
client.getRoom(roomId).getMyMembership.mockReturnValue("invite");
mocked(client.getRoom(roomId)).getMyMembership.mockReturnValue("invite");
});

// have dmPartner1 be in space1 with you
Expand All @@ -350,7 +352,7 @@ describe("SpaceStore", () => {

// room 2 claims to be a child of space2 and is so via a valid m.space.parent
const cliRoom2 = client.getRoom(room2);
cliRoom2.currentState.getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([
const room2MockStateEvents = testUtils.mockStateEventImplementation([
mkEvent({
event: true,
type: EventType.SpaceParent,
Expand All @@ -359,36 +361,40 @@ describe("SpaceStore", () => {
skey: space2,
content: { via: [], canonical: true },
ts: Date.now(),
}),
]));
}) as MatrixEvent,
]);
mocked(cliRoom2.currentState).getStateEvents.mockImplementation(room2MockStateEvents);
const cliSpace2 = client.getRoom(space2);
cliSpace2.currentState.maySendStateEvent.mockImplementation((evType: string, userId: string) => {
if (evType === EventType.SpaceChild) {
return userId === client.getUserId();
}
return true;
});
mocked(cliSpace2.currentState).maySendStateEvent.mockImplementation(
(evType: string, userId: string) => {
if (evType === EventType.SpaceChild) {
return userId === client.getUserId();
}
return true;
});

// room 3 claims to be a child of space3 but is not due to invalid m.space.parent (permissions)
const cliRoom3 = client.getRoom(room3);
cliRoom3.currentState.getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([
mkEvent({
event: true,
type: EventType.SpaceParent,
room: room3,
user: client.getUserId(),
skey: space3,
content: { via: [], canonical: true },
ts: Date.now(),
}),
]));
mocked(cliRoom3.currentState).getStateEvents.mockImplementation(
testUtils.mockStateEventImplementation([
mkEvent({
event: true,
type: EventType.SpaceParent,
room: room3,
user: client.getUserId(),
skey: space3,
content: { via: [], canonical: true },
ts: Date.now(),
}),
]));
const cliSpace3 = client.getRoom(space3);
cliSpace3.currentState.maySendStateEvent.mockImplementation((evType: string, userId: string) => {
if (evType === EventType.SpaceChild) {
return false;
}
return true;
});
mocked(cliSpace3.currentState).maySendStateEvent.mockImplementation(
(evType: string, userId: string) => {
if (evType === EventType.SpaceChild) {
return false;
}
return true;
});

await run();
});
Expand Down Expand Up @@ -598,7 +604,7 @@ describe("SpaceStore", () => {
expect(store.spacePanelSpaces).toStrictEqual([]);
const space = mkSpace(space1);
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES);
client.emit("Room", space);
client.emit(ClientEvent.Room, space);
await prom;
expect(store.spacePanelSpaces).toStrictEqual([space]);
expect(store.invitedSpaces).toStrictEqual([]);
Expand All @@ -611,7 +617,7 @@ describe("SpaceStore", () => {
expect(store.spacePanelSpaces).toStrictEqual([space]);
space.getMyMembership.mockReturnValue("leave");
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES);
client.emit("Room.myMembership", space, "leave", "join");
client.emit(RoomEvent.MyMembership, space, "leave", "join");
await prom;
expect(store.spacePanelSpaces).toStrictEqual([]);
});
Expand All @@ -623,7 +629,7 @@ describe("SpaceStore", () => {
const space = mkSpace(space1);
space.getMyMembership.mockReturnValue("invite");
const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES);
client.emit("Room", space);
client.emit(ClientEvent.Room, space);
await prom;
expect(store.spacePanelSpaces).toStrictEqual([]);
expect(store.invitedSpaces).toStrictEqual([space]);
Expand All @@ -638,7 +644,7 @@ describe("SpaceStore", () => {
expect(store.invitedSpaces).toStrictEqual([space]);
space.getMyMembership.mockReturnValue("join");
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES);
client.emit("Room.myMembership", space, "join", "invite");
client.emit(RoomEvent.MyMembership, space, "join", "invite");
await prom;
expect(store.spacePanelSpaces).toStrictEqual([space]);
expect(store.invitedSpaces).toStrictEqual([]);
Expand All @@ -653,7 +659,7 @@ describe("SpaceStore", () => {
expect(store.invitedSpaces).toStrictEqual([space]);
space.getMyMembership.mockReturnValue("leave");
const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES);
client.emit("Room.myMembership", space, "leave", "invite");
client.emit(RoomEvent.MyMembership, space, "leave", "invite");
await prom;
expect(store.spacePanelSpaces).toStrictEqual([]);
expect(store.invitedSpaces).toStrictEqual([]);
Expand All @@ -673,7 +679,7 @@ describe("SpaceStore", () => {
const invite = mkRoom(invite1);
invite.getMyMembership.mockReturnValue("invite");
const prom = testUtils.emitPromise(store, space1);
client.emit("Room", space);
client.emit(ClientEvent.Room, space);
await prom;

expect(store.spacePanelSpaces).toStrictEqual([space]);
Expand Down Expand Up @@ -705,11 +711,11 @@ describe("SpaceStore", () => {
ts: Date.now(),
});
const spaceRoom = client.getRoom(spaceId);
spaceRoom.currentState.getStateEvents.mockImplementation(
mocked(spaceRoom.currentState).getStateEvents.mockImplementation(
testUtils.mockStateEventImplementation([childEvent]),
);

client.emit("RoomState.events", childEvent);
client.emit(RoomStateEvent.Events, childEvent, spaceRoom.currentState, undefined);
};

const addMember = (spaceId, user: RoomMember) => {
Expand All @@ -723,12 +729,12 @@ describe("SpaceStore", () => {
ts: Date.now(),
});
const spaceRoom = client.getRoom(spaceId);
spaceRoom.currentState.getStateEvents.mockImplementation(
mocked(spaceRoom.currentState).getStateEvents.mockImplementation(
testUtils.mockStateEventImplementation([memberEvent]),
);
spaceRoom.getMember.mockReturnValue(user);
mocked(spaceRoom).getMember.mockReturnValue(user);

client.emit("RoomState.members", memberEvent);
client.emit(RoomStateEvent.Members, memberEvent, spaceRoom.currentState, user);
};

it('emits events for parent spaces when child room is added', async () => {
Expand Down Expand Up @@ -857,8 +863,8 @@ describe("SpaceStore", () => {

await run();

const deferred = defer<void>();
(space.loadMembersIfNeeded as jest.Mock).mockImplementation(() => {
const deferred = defer<boolean>();
space.loadMembersIfNeeded.mockImplementation(() => {
const event = mkEvent({
event: true,
type: EventType.RoomMember,
Expand All @@ -876,7 +882,7 @@ describe("SpaceStore", () => {
});

client.emit(RoomStateEvent.Members, event, null, null);
deferred.resolve();
return deferred.resolve(true) as unknown as Promise<boolean>;
});

spyDispatcher.mockClear();
Expand Down Expand Up @@ -1000,7 +1006,7 @@ describe("SpaceStore", () => {
mkSpace(space2, [room1, room2]);

const cliRoom2 = client.getRoom(room2);
cliRoom2.currentState.getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([
mocked(cliRoom2.currentState).getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([
mkEvent({
event: true,
type: EventType.SpaceParent,
Expand Down Expand Up @@ -1123,14 +1129,14 @@ describe("SpaceStore", () => {
// receive invite to space
const rootSpace = mkSpace(space1, [room1, room2, space2]);
rootSpace.getMyMembership.mockReturnValue("invite");
client.emit("Room", rootSpace);
client.emit(ClientEvent.Room, rootSpace);
jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([rootSpace]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([]);

// accept invite to space
rootSpace.getMyMembership.mockReturnValue("join");
client.emit("Room.myMembership", rootSpace, "join", "invite");
client.emit(RoomEvent.MyMembership, rootSpace, "join", "invite");
jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
Expand All @@ -1139,7 +1145,7 @@ describe("SpaceStore", () => {
expect(SpaceStore.instance.isRoomInSpace(space1, room1)).toBeFalsy();
const rootSpaceRoom1 = mkRoom(room1);
rootSpaceRoom1.getMyMembership.mockReturnValue("join");
client.emit("Room", rootSpaceRoom1);
client.emit(ClientEvent.Room, rootSpaceRoom1);
jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
Expand All @@ -1153,7 +1159,7 @@ describe("SpaceStore", () => {
expect(SpaceStore.instance.isRoomInSpace(space1, room2)).toBeFalsy();
const rootSpaceRoom2 = mkRoom(room2);
rootSpaceRoom2.getMyMembership.mockReturnValue("invite");
client.emit("Room", rootSpaceRoom2);
client.emit(ClientEvent.Room, rootSpaceRoom2);
jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
Expand Down Expand Up @@ -1181,7 +1187,7 @@ describe("SpaceStore", () => {
}
});
expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeFalsy();
client.emit("RoomState.members", mkEvent({
const memberEvent = mkEvent({
event: true,
type: EventType.RoomMember,
content: {
Expand All @@ -1190,12 +1196,13 @@ describe("SpaceStore", () => {
skey: dm1Partner.userId,
user: dm1Partner.userId,
room: space1,
}));
});
client.emit(RoomStateEvent.Members, memberEvent, undefined, undefined);
jest.runOnlyPendingTimers();
expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeTruthy();
const dm1Room = mkRoom(dm1);
dm1Room.getMyMembership.mockReturnValue("join");
client.emit("Room", dm1Room);
client.emit(ClientEvent.Room, dm1Room);
jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
Expand All @@ -1209,7 +1216,7 @@ describe("SpaceStore", () => {
const subspace = mkSpace(space2);
subspace.getMyMembership.mockReturnValue("join");
const prom = testUtils.emitPromise(SpaceStore.instance, space1);
client.emit("Room", subspace);
client.emit(ClientEvent.Room, subspace);
jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces.map(r => r.roomId)).toStrictEqual([rootSpace.roomId]);
Expand Down
7 changes: 4 additions & 3 deletions test/stores/notifications/RoomNotificationState-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import { Room } from "matrix-js-sdk/src/models/room";
import { MatrixEventEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";

import "../../skinned-sdk";
import { stubClient } from "../../test-utils";
Expand All @@ -28,16 +29,16 @@ describe("RoomNotificationState", () => {
const client = MatrixClientPeg.get();

it("Updates on event decryption", () => {
const testRoom = testUtils.mkStubRoom(client, "$aroomid", "Test room");
const testRoom = testUtils.mkStubRoom("$aroomid", "Test room", client);

const roomNotifState = new RoomNotificationState(testRoom as any as Room);
const listener = jest.fn();
roomNotifState.addListener(NotificationStateEvents.Update, listener);
const testEvent = {
getRoomId: () => testRoom.roomId,
};
} as unknown as MatrixEvent;
testRoom.getUnreadNotificationCount = jest.fn().mockReturnValue(1);
client.emit("Event.decrypted", testEvent);
client.emit(MatrixEventEvent.Decrypted, testEvent);
expect(listener).toHaveBeenCalled();
});
});
3 changes: 2 additions & 1 deletion test/stores/room-list/SpaceWatcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { mocked } from 'jest-mock';

import "../../skinned-sdk"; // Must be first for skinning to work
import { SpaceWatcher } from "../../../src/stores/room-list/SpaceWatcher";
Expand Down Expand Up @@ -49,7 +50,7 @@ const space2 = "!space2:server";
describe("SpaceWatcher", () => {
stubClient();
const store = SpaceStore.instance;
const client = MatrixClientPeg.get();
const client = mocked(MatrixClientPeg.get());

let rooms = [];
const mkSpaceForRooms = (spaceId: string, children: string[] = []) => mkSpace(client, spaceId, rooms, children);
Expand Down
1 change: 1 addition & 0 deletions test/stores/room-list/filters/SpaceFilterCondition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import { mocked } from 'jest-mock';
import { Room } from "matrix-js-sdk/src/matrix";

import SettingsStore from "../../../../src/settings/SettingsStore";
import { FILTER_CHANGED } from "../../../../src/stores/room-list/filters/IFilterCondition";
Expand Down
6 changes: 3 additions & 3 deletions test/stores/room-list/previews/PollStartEventPreview-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { MatrixEvent } from "matrix-js-sdk/src/matrix";
import { MatrixEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
import { POLL_ANSWER, M_TEXT, M_POLL_KIND_DISCLOSED, M_POLL_START } from "matrix-events-sdk";

import { PollStartEventPreview } from "../../../../src/stores/room-list/previews/PollStartEventPreview";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";

MatrixClientPeg.matrixClient = {
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue({
getUserId: () => "@me:example.com",
};
} as unknown as MatrixClient);

describe("PollStartEventPreview", () => {
it("shows the question for a poll I created", async () => {
Expand Down
Loading

0 comments on commit df6c53f

Please sign in to comment.