test typescriptification - ForwardDialog (#8469)

* test/components/views/dialogs/ForwardDialog-test.js -> tsx

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

* fix ts issues in ForwardDialog

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

* remove unused stub-component

Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
Kerry 2022-05-03 10:22:38 +02:00 committed by GitHub
parent 1c70696b10
commit f280fab47d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 34 deletions

View file

@ -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 <div>{ displayName }</div>;
};
}
return class extends React.Component {
static displayName = displayName;
render() {
return render();
}
};
}

View file

@ -17,32 +17,59 @@ limitations under the License.
import React from "react"; import React from "react";
import { mount } from "enzyme"; import { mount } from "enzyme";
import { act } from "react-dom/test-utils"; 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 { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import ForwardDialog from "../../../../src/components/views/dialogs/ForwardDialog";
import DMRoomMap from "../../../../src/utils/DMRoomMap"; import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks"; 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", () => { describe("ForwardDialog", () => {
const sourceRoom = "!111111111111111111:example.org"; const sourceRoom = "!111111111111111111:example.org";
const defaultMessage = TestUtils.mkMessage({ const aliceId = "@alice:example.org";
const defaultMessage = mkMessage({
room: sourceRoom, room: sourceRoom,
user: "@alice:example.org", user: aliceId,
msg: "Hello world!", msg: "Hello world!",
event: true, 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 mountForwardDialog = async (message = defaultMessage, rooms = defaultRooms) => {
const client = MatrixClientPeg.get(); mockClient.getVisibleRooms.mockReturnValue(rooms);
client.getVisibleRooms = jest.fn().mockReturnValue(rooms); mockClient.getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId));
let wrapper; let wrapper;
await act(async () => { await act(async () => {
wrapper = mount( wrapper = mount(
<ForwardDialog <ForwardDialog
matrixClient={client} matrixClient={mockClient}
event={message} event={message}
permalinkCreator={new RoomPermalinkCreator(undefined, sourceRoom)} permalinkCreator={new RoomPermalinkCreator(undefined, sourceRoom)}
onFinished={jest.fn()} onFinished={jest.fn()}
@ -57,9 +84,14 @@ describe("ForwardDialog", () => {
}; };
beforeEach(() => { beforeEach(() => {
TestUtils.stubClient();
DMRoomMap.makeShared(); 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 () => { 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 // Make sendEvent require manual resolution so we can see the sending state
let finishSend; let finishSend;
let cancelSend; let cancelSend;
MatrixClientPeg.get().sendEvent = jest.fn(() => new Promise((resolve, reject) => { mockClient.sendEvent.mockImplementation(() => new Promise((resolve, reject) => {
finishSend = resolve; finishSend = resolve;
cancelSend = reject; cancelSend = reject;
})); }));
@ -135,7 +167,7 @@ describe("ForwardDialog", () => {
}); });
it("can render replies", async () => { it("can render replies", async () => {
const replyMessage = TestUtils.mkEvent({ const replyMessage = mkEvent({
type: "m.room.message", type: "m.room.message",
room: "!111111111111111111:example.org", room: "!111111111111111111:example.org",
user: "@alice:example.org", user: "@alice:example.org",
@ -156,9 +188,9 @@ describe("ForwardDialog", () => {
}); });
it("disables buttons for rooms without send permissions", async () => { 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); 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); const wrapper = await mountForwardDialog(undefined, rooms);