diff --git a/test/stores/SpaceStore-test.ts b/test/stores/SpaceStore-test.ts index 5b6e776d31..60faa55804 100644 --- a/test/stores/SpaceStore-test.ts +++ b/test/stores/SpaceStore-test.ts @@ -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"; @@ -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(); }; @@ -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, @@ -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 = { @@ -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 @@ -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, @@ -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(); }); @@ -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([]); @@ -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([]); }); @@ -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]); @@ -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([]); @@ -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([]); @@ -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]); @@ -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) => { @@ -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 () => { @@ -857,8 +863,8 @@ describe("SpaceStore", () => { await run(); - const deferred = defer(); - (space.loadMembersIfNeeded as jest.Mock).mockImplementation(() => { + const deferred = defer(); + space.loadMembersIfNeeded.mockImplementation(() => { const event = mkEvent({ event: true, type: EventType.RoomMember, @@ -876,7 +882,7 @@ describe("SpaceStore", () => { }); client.emit(RoomStateEvent.Members, event, null, null); - deferred.resolve(); + return deferred.resolve(true) as unknown as Promise; }); spyDispatcher.mockClear(); @@ -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, @@ -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]); @@ -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]); @@ -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]); @@ -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: { @@ -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]); @@ -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]); diff --git a/test/stores/notifications/RoomNotificationState-test.ts b/test/stores/notifications/RoomNotificationState-test.ts index 76600dfd2c..befa1a0008 100644 --- a/test/stores/notifications/RoomNotificationState-test.ts +++ b/test/stores/notifications/RoomNotificationState-test.ts @@ -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"; @@ -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(); }); }); diff --git a/test/stores/room-list/SpaceWatcher-test.ts b/test/stores/room-list/SpaceWatcher-test.ts index 74c58fc359..b870da6aa1 100644 --- a/test/stores/room-list/SpaceWatcher-test.ts +++ b/test/stores/room-list/SpaceWatcher-test.ts @@ -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"; @@ -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); diff --git a/test/stores/room-list/filters/SpaceFilterCondition-test.ts b/test/stores/room-list/filters/SpaceFilterCondition-test.ts index 0f1586dcd7..ae9aab135c 100644 --- a/test/stores/room-list/filters/SpaceFilterCondition-test.ts +++ b/test/stores/room-list/filters/SpaceFilterCondition-test.ts @@ -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"; diff --git a/test/stores/room-list/previews/PollStartEventPreview-test.ts b/test/stores/room-list/previews/PollStartEventPreview-test.ts index a24de59ccc..a7726167e1 100644 --- a/test/stores/room-list/previews/PollStartEventPreview-test.ts +++ b/test/stores/room-list/previews/PollStartEventPreview-test.ts @@ -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 () => { diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 289fb8e5d7..1bdb8a5ec1 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -1,5 +1,5 @@ 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 { JoinRule } from 'matrix-js-sdk/src/@types/partials'; import { @@ -417,8 +417,12 @@ export const mockStateEventImplementation = (events: MatrixEvent[]) => { return getStateEvents; }; -export const mkRoom = (client: MatrixClient, roomId: string, rooms?: ReturnType[]) => { - const room = mkStubRoom(roomId, roomId, client); +export const mkRoom = ( + client: MatrixClient, + roomId: string, + rooms?: ReturnType[], +): MockedObject => { + const room = mocked(mkStubRoom(roomId, roomId, client)); mocked(room.currentState).getStateEvents.mockImplementation(mockStateEventImplementation([])); rooms?.push(room); return room; @@ -447,9 +451,9 @@ export const mkSpace = ( spaceId: string, rooms?: ReturnType[], children: string[] = [], -) => { - const space = mkRoom(client, spaceId, rooms); - mocked(space).isSpaceRoom.mockReturnValue(true); +): MockedObject => { + const space = mocked(mkRoom(client, spaceId, rooms)); + space.isSpaceRoom.mockReturnValue(true); mocked(space.currentState).getStateEvents.mockImplementation(mockStateEventImplementation(children.map(roomId => mkEvent({ event: true, diff --git a/tsconfig.json b/tsconfig.json index 4f896a72ea..4f09a98e99 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -26,5 +26,7 @@ "./test/test-utils/**/*.tsx", "./test/utils/**/*.ts", "./test/utils/**/*.tsx", + "./test/stores/**/*.ts", + "./test/stores/**/*.tsx", ], }