From f280fab47df45571c45e4a7db95259ec63f3246f Mon Sep 17 00:00:00 2001 From: Kerry Date: Tue, 3 May 2022 10:22:38 +0200 Subject: [PATCH] test typescriptification - ForwardDialog (#8469) * test/components/views/dialogs/ForwardDialog-test.js -> tsx Signed-off-by: Kerry Archibald * fix ts issues in ForwardDialog Signed-off-by: Kerry Archibald * remove unused stub-component Signed-off-by: Kerry Archibald --- test/components/stub-component.js | 20 ------- ...dDialog-test.js => ForwardDialog-test.tsx} | 60 ++++++++++++++----- 2 files changed, 46 insertions(+), 34 deletions(-) delete mode 100644 test/components/stub-component.js rename test/components/views/dialogs/{ForwardDialog-test.js => ForwardDialog-test.tsx} (75%) diff --git a/test/components/stub-component.js b/test/components/stub-component.js deleted file mode 100644 index f98959a573..0000000000 --- a/test/components/stub-component.js +++ /dev/null @@ -1,20 +0,0 @@ -/* A dummy React component which we use for stubbing out app-level components - */ - -import React from 'react'; - -export default function({ displayName = "StubComponent", render } = {}) { - if (!render) { - render = function() { - return
{ displayName }
; - }; - } - - return class extends React.Component { - static displayName = displayName; - - render() { - return render(); - } - }; -} diff --git a/test/components/views/dialogs/ForwardDialog-test.js b/test/components/views/dialogs/ForwardDialog-test.tsx similarity index 75% rename from test/components/views/dialogs/ForwardDialog-test.js rename to test/components/views/dialogs/ForwardDialog-test.tsx index 835f73bbf6..089a92a2b3 100644 --- a/test/components/views/dialogs/ForwardDialog-test.js +++ b/test/components/views/dialogs/ForwardDialog-test.tsx @@ -17,32 +17,59 @@ limitations under the License. import React from "react"; import { mount } from "enzyme"; import { act } from "react-dom/test-utils"; +import { MatrixEvent, EventType } from "matrix-js-sdk/src/matrix"; -import * as TestUtils from "../../../test-utils"; import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; +import ForwardDialog from "../../../../src/components/views/dialogs/ForwardDialog"; import DMRoomMap from "../../../../src/utils/DMRoomMap"; import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks"; -import ForwardDialog from "../../../../src/components/views/dialogs/ForwardDialog"; +import { + getMockClientWithEventEmitter, + mkEvent, + mkMessage, + mkStubRoom, +} from "../../../test-utils"; describe("ForwardDialog", () => { const sourceRoom = "!111111111111111111:example.org"; - const defaultMessage = TestUtils.mkMessage({ + const aliceId = "@alice:example.org"; + const defaultMessage = mkMessage({ room: sourceRoom, - user: "@alice:example.org", + user: aliceId, msg: "Hello world!", event: true, }); - const defaultRooms = ["a", "A", "b"].map(name => TestUtils.mkStubRoom(name, name)); + const accountDataEvent = new MatrixEvent({ + type: EventType.Direct, + sender: aliceId, + content: {}, + }); + const mockClient = getMockClientWithEventEmitter({ + getUserId: jest.fn().mockReturnValue(aliceId), + isGuest: jest.fn().mockReturnValue(false), + getVisibleRooms: jest.fn().mockReturnValue([]), + getRoom: jest.fn(), + getAccountData: jest.fn().mockReturnValue(accountDataEvent), + getPushActionsForEvent: jest.fn(), + mxcUrlToHttp: jest.fn().mockReturnValue(''), + isRoomEncrypted: jest.fn().mockReturnValue(false), + getProfileInfo: jest.fn().mockResolvedValue({ + displayname: 'Alice', + }), + decryptEventIfNeeded: jest.fn(), + sendEvent: jest.fn(), + }); + const defaultRooms = ["a", "A", "b"].map(name => mkStubRoom(name, name, mockClient)); const mountForwardDialog = async (message = defaultMessage, rooms = defaultRooms) => { - const client = MatrixClientPeg.get(); - client.getVisibleRooms = jest.fn().mockReturnValue(rooms); + mockClient.getVisibleRooms.mockReturnValue(rooms); + mockClient.getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId)); let wrapper; await act(async () => { wrapper = mount( { }; beforeEach(() => { - TestUtils.stubClient(); DMRoomMap.makeShared(); - MatrixClientPeg.get().getUserId = jest.fn().mockReturnValue("@bob:example.org"); + jest.clearAllMocks(); + mockClient.getUserId.mockReturnValue("@bob:example.org"); + mockClient.sendEvent.mockReset(); + }); + + afterAll(() => { + jest.spyOn(MatrixClientPeg, 'get').mockRestore(); }); it("shows a preview with us as the sender", async () => { @@ -91,7 +123,7 @@ describe("ForwardDialog", () => { // Make sendEvent require manual resolution so we can see the sending state let finishSend; let cancelSend; - MatrixClientPeg.get().sendEvent = jest.fn(() => new Promise((resolve, reject) => { + mockClient.sendEvent.mockImplementation(() => new Promise((resolve, reject) => { finishSend = resolve; cancelSend = reject; })); @@ -135,7 +167,7 @@ describe("ForwardDialog", () => { }); it("can render replies", async () => { - const replyMessage = TestUtils.mkEvent({ + const replyMessage = mkEvent({ type: "m.room.message", room: "!111111111111111111:example.org", user: "@alice:example.org", @@ -156,9 +188,9 @@ describe("ForwardDialog", () => { }); it("disables buttons for rooms without send permissions", async () => { - const readOnlyRoom = TestUtils.mkStubRoom("a", "a"); + const readOnlyRoom = mkStubRoom("a", "a", mockClient); readOnlyRoom.maySendMessage = jest.fn().mockReturnValue(false); - const rooms = [readOnlyRoom, TestUtils.mkStubRoom("b", "b")]; + const rooms = [readOnlyRoom, mkStubRoom("b", "b", mockClient)]; const wrapper = await mountForwardDialog(undefined, rooms);