include /test/stores in tsconfig (#8026)

* 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>
This commit is contained in:
Kerry 2022-03-11 17:03:33 +01:00 committed by GitHub
parent ceb2281d36
commit df6c53ff41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 83 additions and 67 deletions

View file

@ -14,10 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { mocked } from 'jest-mock';
import { EventType } from "matrix-js-sdk/src/@types/event"; import { EventType } from "matrix-js-sdk/src/@types/event";
import { RoomMember } from "matrix-js-sdk/src/models/room-member"; import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state"; import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { defer } from "matrix-js-sdk/src/utils"; 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 "../skinned-sdk"; // Must be first for skinning to work
import SpaceStore from "../../src/stores/spaces/SpaceStore"; import SpaceStore from "../../src/stores/spaces/SpaceStore";
@ -101,8 +103,8 @@ describe("SpaceStore", () => {
const viewRoom = roomId => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: roomId }, true); const viewRoom = roomId => defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: roomId }, true);
const run = async () => { const run = async () => {
client.getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId)); mocked(client).getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId));
client.getRoomUpgradeHistory.mockImplementation(roomId => [rooms.find(room => room.roomId === roomId)]); mocked(client).getRoomUpgradeHistory.mockImplementation(roomId => [rooms.find(room => room.roomId === roomId)]);
await testUtils.setupAsyncStoreWithClient(store, client); await testUtils.setupAsyncStoreWithClient(store, client);
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
}; };
@ -117,7 +119,7 @@ describe("SpaceStore", () => {
beforeEach(async () => { beforeEach(async () => {
jest.runOnlyPendingTimers(); // run async dispatch jest.runOnlyPendingTimers(); // run async dispatch
client.getVisibleRooms.mockReturnValue(rooms = []); mocked(client).getVisibleRooms.mockReturnValue(rooms = []);
await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, { await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, {
[MetaSpace.Home]: true, [MetaSpace.Home]: true,
@ -318,7 +320,7 @@ describe("SpaceStore", () => {
mkSpace(space3, [invite2]); mkSpace(space3, [invite2]);
mkSpace(space4, [room4, fav2, space2, space3]); 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 => { [fav1, fav2, fav3].forEach(roomId => {
client.getRoom(roomId).tags = { client.getRoom(roomId).tags = {
@ -329,7 +331,7 @@ describe("SpaceStore", () => {
}); });
[invite1, invite2].forEach(roomId => { [invite1, invite2].forEach(roomId => {
client.getRoom(roomId).getMyMembership.mockReturnValue("invite"); mocked(client.getRoom(roomId)).getMyMembership.mockReturnValue("invite");
}); });
// have dmPartner1 be in space1 with you // have dmPartner1 be in space1 with you
@ -350,7 +352,7 @@ describe("SpaceStore", () => {
// room 2 claims to be a child of space2 and is so via a valid m.space.parent // room 2 claims to be a child of space2 and is so via a valid m.space.parent
const cliRoom2 = client.getRoom(room2); const cliRoom2 = client.getRoom(room2);
cliRoom2.currentState.getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([ const room2MockStateEvents = testUtils.mockStateEventImplementation([
mkEvent({ mkEvent({
event: true, event: true,
type: EventType.SpaceParent, type: EventType.SpaceParent,
@ -359,36 +361,40 @@ describe("SpaceStore", () => {
skey: space2, skey: space2,
content: { via: [], canonical: true }, content: { via: [], canonical: true },
ts: Date.now(), ts: Date.now(),
}), }) as MatrixEvent,
])); ]);
mocked(cliRoom2.currentState).getStateEvents.mockImplementation(room2MockStateEvents);
const cliSpace2 = client.getRoom(space2); const cliSpace2 = client.getRoom(space2);
cliSpace2.currentState.maySendStateEvent.mockImplementation((evType: string, userId: string) => { mocked(cliSpace2.currentState).maySendStateEvent.mockImplementation(
if (evType === EventType.SpaceChild) { (evType: string, userId: string) => {
return userId === client.getUserId(); if (evType === EventType.SpaceChild) {
} return userId === client.getUserId();
return true; }
}); return true;
});
// room 3 claims to be a child of space3 but is not due to invalid m.space.parent (permissions) // room 3 claims to be a child of space3 but is not due to invalid m.space.parent (permissions)
const cliRoom3 = client.getRoom(room3); const cliRoom3 = client.getRoom(room3);
cliRoom3.currentState.getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([ mocked(cliRoom3.currentState).getStateEvents.mockImplementation(
mkEvent({ testUtils.mockStateEventImplementation([
event: true, mkEvent({
type: EventType.SpaceParent, event: true,
room: room3, type: EventType.SpaceParent,
user: client.getUserId(), room: room3,
skey: space3, user: client.getUserId(),
content: { via: [], canonical: true }, skey: space3,
ts: Date.now(), content: { via: [], canonical: true },
}), ts: Date.now(),
])); }),
]));
const cliSpace3 = client.getRoom(space3); const cliSpace3 = client.getRoom(space3);
cliSpace3.currentState.maySendStateEvent.mockImplementation((evType: string, userId: string) => { mocked(cliSpace3.currentState).maySendStateEvent.mockImplementation(
if (evType === EventType.SpaceChild) { (evType: string, userId: string) => {
return false; if (evType === EventType.SpaceChild) {
} return false;
return true; }
}); return true;
});
await run(); await run();
}); });
@ -598,7 +604,7 @@ describe("SpaceStore", () => {
expect(store.spacePanelSpaces).toStrictEqual([]); expect(store.spacePanelSpaces).toStrictEqual([]);
const space = mkSpace(space1); const space = mkSpace(space1);
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES); const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES);
client.emit("Room", space); client.emit(ClientEvent.Room, space);
await prom; await prom;
expect(store.spacePanelSpaces).toStrictEqual([space]); expect(store.spacePanelSpaces).toStrictEqual([space]);
expect(store.invitedSpaces).toStrictEqual([]); expect(store.invitedSpaces).toStrictEqual([]);
@ -611,7 +617,7 @@ describe("SpaceStore", () => {
expect(store.spacePanelSpaces).toStrictEqual([space]); expect(store.spacePanelSpaces).toStrictEqual([space]);
space.getMyMembership.mockReturnValue("leave"); space.getMyMembership.mockReturnValue("leave");
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES); 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; await prom;
expect(store.spacePanelSpaces).toStrictEqual([]); expect(store.spacePanelSpaces).toStrictEqual([]);
}); });
@ -623,7 +629,7 @@ describe("SpaceStore", () => {
const space = mkSpace(space1); const space = mkSpace(space1);
space.getMyMembership.mockReturnValue("invite"); space.getMyMembership.mockReturnValue("invite");
const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES); const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES);
client.emit("Room", space); client.emit(ClientEvent.Room, space);
await prom; await prom;
expect(store.spacePanelSpaces).toStrictEqual([]); expect(store.spacePanelSpaces).toStrictEqual([]);
expect(store.invitedSpaces).toStrictEqual([space]); expect(store.invitedSpaces).toStrictEqual([space]);
@ -638,7 +644,7 @@ describe("SpaceStore", () => {
expect(store.invitedSpaces).toStrictEqual([space]); expect(store.invitedSpaces).toStrictEqual([space]);
space.getMyMembership.mockReturnValue("join"); space.getMyMembership.mockReturnValue("join");
const prom = testUtils.emitPromise(store, UPDATE_TOP_LEVEL_SPACES); 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; await prom;
expect(store.spacePanelSpaces).toStrictEqual([space]); expect(store.spacePanelSpaces).toStrictEqual([space]);
expect(store.invitedSpaces).toStrictEqual([]); expect(store.invitedSpaces).toStrictEqual([]);
@ -653,7 +659,7 @@ describe("SpaceStore", () => {
expect(store.invitedSpaces).toStrictEqual([space]); expect(store.invitedSpaces).toStrictEqual([space]);
space.getMyMembership.mockReturnValue("leave"); space.getMyMembership.mockReturnValue("leave");
const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES); const prom = testUtils.emitPromise(store, UPDATE_INVITED_SPACES);
client.emit("Room.myMembership", space, "leave", "invite"); client.emit(RoomEvent.MyMembership, space, "leave", "invite");
await prom; await prom;
expect(store.spacePanelSpaces).toStrictEqual([]); expect(store.spacePanelSpaces).toStrictEqual([]);
expect(store.invitedSpaces).toStrictEqual([]); expect(store.invitedSpaces).toStrictEqual([]);
@ -673,7 +679,7 @@ describe("SpaceStore", () => {
const invite = mkRoom(invite1); const invite = mkRoom(invite1);
invite.getMyMembership.mockReturnValue("invite"); invite.getMyMembership.mockReturnValue("invite");
const prom = testUtils.emitPromise(store, space1); const prom = testUtils.emitPromise(store, space1);
client.emit("Room", space); client.emit(ClientEvent.Room, space);
await prom; await prom;
expect(store.spacePanelSpaces).toStrictEqual([space]); expect(store.spacePanelSpaces).toStrictEqual([space]);
@ -705,11 +711,11 @@ describe("SpaceStore", () => {
ts: Date.now(), ts: Date.now(),
}); });
const spaceRoom = client.getRoom(spaceId); const spaceRoom = client.getRoom(spaceId);
spaceRoom.currentState.getStateEvents.mockImplementation( mocked(spaceRoom.currentState).getStateEvents.mockImplementation(
testUtils.mockStateEventImplementation([childEvent]), testUtils.mockStateEventImplementation([childEvent]),
); );
client.emit("RoomState.events", childEvent); client.emit(RoomStateEvent.Events, childEvent, spaceRoom.currentState, undefined);
}; };
const addMember = (spaceId, user: RoomMember) => { const addMember = (spaceId, user: RoomMember) => {
@ -723,12 +729,12 @@ describe("SpaceStore", () => {
ts: Date.now(), ts: Date.now(),
}); });
const spaceRoom = client.getRoom(spaceId); const spaceRoom = client.getRoom(spaceId);
spaceRoom.currentState.getStateEvents.mockImplementation( mocked(spaceRoom.currentState).getStateEvents.mockImplementation(
testUtils.mockStateEventImplementation([memberEvent]), 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 () => { it('emits events for parent spaces when child room is added', async () => {
@ -857,8 +863,8 @@ describe("SpaceStore", () => {
await run(); await run();
const deferred = defer<void>(); const deferred = defer<boolean>();
(space.loadMembersIfNeeded as jest.Mock).mockImplementation(() => { space.loadMembersIfNeeded.mockImplementation(() => {
const event = mkEvent({ const event = mkEvent({
event: true, event: true,
type: EventType.RoomMember, type: EventType.RoomMember,
@ -876,7 +882,7 @@ describe("SpaceStore", () => {
}); });
client.emit(RoomStateEvent.Members, event, null, null); client.emit(RoomStateEvent.Members, event, null, null);
deferred.resolve(); return deferred.resolve(true) as unknown as Promise<boolean>;
}); });
spyDispatcher.mockClear(); spyDispatcher.mockClear();
@ -1000,7 +1006,7 @@ describe("SpaceStore", () => {
mkSpace(space2, [room1, room2]); mkSpace(space2, [room1, room2]);
const cliRoom2 = client.getRoom(room2); const cliRoom2 = client.getRoom(room2);
cliRoom2.currentState.getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([ mocked(cliRoom2.currentState).getStateEvents.mockImplementation(testUtils.mockStateEventImplementation([
mkEvent({ mkEvent({
event: true, event: true,
type: EventType.SpaceParent, type: EventType.SpaceParent,
@ -1123,14 +1129,14 @@ describe("SpaceStore", () => {
// receive invite to space // receive invite to space
const rootSpace = mkSpace(space1, [room1, room2, space2]); const rootSpace = mkSpace(space1, [room1, room2, space2]);
rootSpace.getMyMembership.mockReturnValue("invite"); rootSpace.getMyMembership.mockReturnValue("invite");
client.emit("Room", rootSpace); client.emit(ClientEvent.Room, rootSpace);
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([rootSpace]); expect(SpaceStore.instance.invitedSpaces).toStrictEqual([rootSpace]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([]); expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([]);
// accept invite to space // accept invite to space
rootSpace.getMyMembership.mockReturnValue("join"); rootSpace.getMyMembership.mockReturnValue("join");
client.emit("Room.myMembership", rootSpace, "join", "invite"); client.emit(RoomEvent.MyMembership, rootSpace, "join", "invite");
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]); expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]); expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
@ -1139,7 +1145,7 @@ describe("SpaceStore", () => {
expect(SpaceStore.instance.isRoomInSpace(space1, room1)).toBeFalsy(); expect(SpaceStore.instance.isRoomInSpace(space1, room1)).toBeFalsy();
const rootSpaceRoom1 = mkRoom(room1); const rootSpaceRoom1 = mkRoom(room1);
rootSpaceRoom1.getMyMembership.mockReturnValue("join"); rootSpaceRoom1.getMyMembership.mockReturnValue("join");
client.emit("Room", rootSpaceRoom1); client.emit(ClientEvent.Room, rootSpaceRoom1);
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]); expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]); expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
@ -1153,7 +1159,7 @@ describe("SpaceStore", () => {
expect(SpaceStore.instance.isRoomInSpace(space1, room2)).toBeFalsy(); expect(SpaceStore.instance.isRoomInSpace(space1, room2)).toBeFalsy();
const rootSpaceRoom2 = mkRoom(room2); const rootSpaceRoom2 = mkRoom(room2);
rootSpaceRoom2.getMyMembership.mockReturnValue("invite"); rootSpaceRoom2.getMyMembership.mockReturnValue("invite");
client.emit("Room", rootSpaceRoom2); client.emit(ClientEvent.Room, rootSpaceRoom2);
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]); expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]); expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
@ -1181,7 +1187,7 @@ describe("SpaceStore", () => {
} }
}); });
expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeFalsy(); expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeFalsy();
client.emit("RoomState.members", mkEvent({ const memberEvent = mkEvent({
event: true, event: true,
type: EventType.RoomMember, type: EventType.RoomMember,
content: { content: {
@ -1190,12 +1196,13 @@ describe("SpaceStore", () => {
skey: dm1Partner.userId, skey: dm1Partner.userId,
user: dm1Partner.userId, user: dm1Partner.userId,
room: space1, room: space1,
})); });
client.emit(RoomStateEvent.Members, memberEvent, undefined, undefined);
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeTruthy(); expect(SpaceStore.instance.getSpaceFilteredUserIds(space1).has(dm1Partner.userId)).toBeTruthy();
const dm1Room = mkRoom(dm1); const dm1Room = mkRoom(dm1);
dm1Room.getMyMembership.mockReturnValue("join"); dm1Room.getMyMembership.mockReturnValue("join");
client.emit("Room", dm1Room); client.emit(ClientEvent.Room, dm1Room);
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]); expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]); expect(SpaceStore.instance.spacePanelSpaces).toStrictEqual([rootSpace]);
@ -1209,7 +1216,7 @@ describe("SpaceStore", () => {
const subspace = mkSpace(space2); const subspace = mkSpace(space2);
subspace.getMyMembership.mockReturnValue("join"); subspace.getMyMembership.mockReturnValue("join");
const prom = testUtils.emitPromise(SpaceStore.instance, space1); const prom = testUtils.emitPromise(SpaceStore.instance, space1);
client.emit("Room", subspace); client.emit(ClientEvent.Room, subspace);
jest.runOnlyPendingTimers(); jest.runOnlyPendingTimers();
expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]); expect(SpaceStore.instance.invitedSpaces).toStrictEqual([]);
expect(SpaceStore.instance.spacePanelSpaces.map(r => r.roomId)).toStrictEqual([rootSpace.roomId]); expect(SpaceStore.instance.spacePanelSpaces.map(r => r.roomId)).toStrictEqual([rootSpace.roomId]);

View file

@ -15,6 +15,7 @@ limitations under the License.
*/ */
import { Room } from "matrix-js-sdk/src/models/room"; import { Room } from "matrix-js-sdk/src/models/room";
import { MatrixEventEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
import "../../skinned-sdk"; import "../../skinned-sdk";
import { stubClient } from "../../test-utils"; import { stubClient } from "../../test-utils";
@ -28,16 +29,16 @@ describe("RoomNotificationState", () => {
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
it("Updates on event decryption", () => { 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 roomNotifState = new RoomNotificationState(testRoom as any as Room);
const listener = jest.fn(); const listener = jest.fn();
roomNotifState.addListener(NotificationStateEvents.Update, listener); roomNotifState.addListener(NotificationStateEvents.Update, listener);
const testEvent = { const testEvent = {
getRoomId: () => testRoom.roomId, getRoomId: () => testRoom.roomId,
}; } as unknown as MatrixEvent;
testRoom.getUnreadNotificationCount = jest.fn().mockReturnValue(1); testRoom.getUnreadNotificationCount = jest.fn().mockReturnValue(1);
client.emit("Event.decrypted", testEvent); client.emit(MatrixEventEvent.Decrypted, testEvent);
expect(listener).toHaveBeenCalled(); expect(listener).toHaveBeenCalled();
}); });
}); });

View file

@ -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 See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { mocked } from 'jest-mock';
import "../../skinned-sdk"; // Must be first for skinning to work import "../../skinned-sdk"; // Must be first for skinning to work
import { SpaceWatcher } from "../../../src/stores/room-list/SpaceWatcher"; import { SpaceWatcher } from "../../../src/stores/room-list/SpaceWatcher";
@ -49,7 +50,7 @@ const space2 = "!space2:server";
describe("SpaceWatcher", () => { describe("SpaceWatcher", () => {
stubClient(); stubClient();
const store = SpaceStore.instance; const store = SpaceStore.instance;
const client = MatrixClientPeg.get(); const client = mocked(MatrixClientPeg.get());
let rooms = []; let rooms = [];
const mkSpaceForRooms = (spaceId: string, children: string[] = []) => mkSpace(client, spaceId, rooms, children); const mkSpaceForRooms = (spaceId: string, children: string[] = []) => mkSpace(client, spaceId, rooms, children);

View file

@ -15,6 +15,7 @@ limitations under the License.
*/ */
import { mocked } from 'jest-mock'; import { mocked } from 'jest-mock';
import { Room } from "matrix-js-sdk/src/matrix";
import SettingsStore from "../../../../src/settings/SettingsStore"; import SettingsStore from "../../../../src/settings/SettingsStore";
import { FILTER_CHANGED } from "../../../../src/stores/room-list/filters/IFilterCondition"; import { FILTER_CHANGED } from "../../../../src/stores/room-list/filters/IFilterCondition";

View file

@ -14,15 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License. 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 { 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 { PollStartEventPreview } from "../../../../src/stores/room-list/previews/PollStartEventPreview";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
MatrixClientPeg.matrixClient = { jest.spyOn(MatrixClientPeg, 'get').mockReturnValue({
getUserId: () => "@me:example.com", getUserId: () => "@me:example.com",
}; } as unknown as MatrixClient);
describe("PollStartEventPreview", () => { describe("PollStartEventPreview", () => {
it("shows the question for a poll I created", async () => { it("shows the question for a poll I created", async () => {

View file

@ -1,5 +1,5 @@
import EventEmitter from "events"; import EventEmitter from "events";
import { mocked } from 'jest-mock'; import { mocked, MockedObject } from 'jest-mock';
import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { JoinRule } from 'matrix-js-sdk/src/@types/partials'; import { JoinRule } from 'matrix-js-sdk/src/@types/partials';
import { import {
@ -417,8 +417,12 @@ export const mockStateEventImplementation = (events: MatrixEvent[]) => {
return getStateEvents; return getStateEvents;
}; };
export const mkRoom = (client: MatrixClient, roomId: string, rooms?: ReturnType<typeof mkStubRoom>[]) => { export const mkRoom = (
const room = mkStubRoom(roomId, roomId, client); client: MatrixClient,
roomId: string,
rooms?: ReturnType<typeof mkStubRoom>[],
): MockedObject<Room> => {
const room = mocked(mkStubRoom(roomId, roomId, client));
mocked(room.currentState).getStateEvents.mockImplementation(mockStateEventImplementation([])); mocked(room.currentState).getStateEvents.mockImplementation(mockStateEventImplementation([]));
rooms?.push(room); rooms?.push(room);
return room; return room;
@ -447,9 +451,9 @@ export const mkSpace = (
spaceId: string, spaceId: string,
rooms?: ReturnType<typeof mkStubRoom>[], rooms?: ReturnType<typeof mkStubRoom>[],
children: string[] = [], children: string[] = [],
) => { ): MockedObject<Room> => {
const space = mkRoom(client, spaceId, rooms); const space = mocked(mkRoom(client, spaceId, rooms));
mocked(space).isSpaceRoom.mockReturnValue(true); space.isSpaceRoom.mockReturnValue(true);
mocked(space.currentState).getStateEvents.mockImplementation(mockStateEventImplementation(children.map(roomId => mocked(space.currentState).getStateEvents.mockImplementation(mockStateEventImplementation(children.map(roomId =>
mkEvent({ mkEvent({
event: true, event: true,

View file

@ -26,5 +26,7 @@
"./test/test-utils/**/*.tsx", "./test/test-utils/**/*.tsx",
"./test/utils/**/*.ts", "./test/utils/**/*.ts",
"./test/utils/**/*.tsx", "./test/utils/**/*.tsx",
"./test/stores/**/*.ts",
"./test/stores/**/*.tsx",
], ],
} }