2021-05-09 07:51:51 +08:00
|
|
|
/*
|
2021-06-06 09:41:28 +08:00
|
|
|
Copyright 2021 Robin Townsend <robin@robin.town>
|
2021-05-09 07:51:51 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
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 "../../../skinned-sdk";
|
|
|
|
|
|
|
|
import React from "react";
|
2021-06-29 20:11:58 +08:00
|
|
|
import { configure, mount } from "enzyme";
|
2021-06-09 18:58:08 +08:00
|
|
|
import Adapter from "@wojtekmaj/enzyme-adapter-react-17";
|
2021-06-29 20:11:58 +08:00
|
|
|
import { act } from "react-dom/test-utils";
|
2021-05-09 07:51:51 +08:00
|
|
|
|
|
|
|
import * as TestUtils from "../../../test-utils";
|
2021-06-29 20:11:58 +08:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2021-05-09 07:51:51 +08:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2021-06-29 20:11:58 +08:00
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2021-05-09 07:51:51 +08:00
|
|
|
import ForwardDialog from "../../../../src/components/views/dialogs/ForwardDialog";
|
|
|
|
|
|
|
|
configure({ adapter: new Adapter() });
|
|
|
|
|
|
|
|
describe("ForwardDialog", () => {
|
2021-05-10 12:54:00 +08:00
|
|
|
const sourceRoom = "!111111111111111111:example.org";
|
|
|
|
const defaultMessage = TestUtils.mkMessage({
|
|
|
|
room: sourceRoom,
|
2021-05-09 07:51:51 +08:00
|
|
|
user: "@alice:example.org",
|
|
|
|
msg: "Hello world!",
|
|
|
|
event: true,
|
|
|
|
});
|
2021-05-10 12:54:00 +08:00
|
|
|
const defaultRooms = ["a", "A", "b"].map(name => TestUtils.mkStubRoom(name, name));
|
2021-05-09 07:51:51 +08:00
|
|
|
|
2021-05-10 12:54:00 +08:00
|
|
|
const mountForwardDialog = async (message = defaultMessage, rooms = defaultRooms) => {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
client.getVisibleRooms = jest.fn().mockReturnValue(rooms);
|
2021-05-09 07:51:51 +08:00
|
|
|
|
2021-05-10 12:54:00 +08:00
|
|
|
let wrapper;
|
2021-05-09 07:51:51 +08:00
|
|
|
await act(async () => {
|
|
|
|
wrapper = mount(
|
|
|
|
<ForwardDialog
|
2021-05-24 20:51:04 +08:00
|
|
|
matrixClient={client}
|
2021-05-09 07:51:51 +08:00
|
|
|
event={message}
|
2021-05-10 12:54:00 +08:00
|
|
|
permalinkCreator={new RoomPermalinkCreator(undefined, sourceRoom)}
|
2021-05-09 07:51:51 +08:00
|
|
|
onFinished={jest.fn()}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
// Wait one tick for our profile data to load so the state update happens within act
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
});
|
2021-05-10 12:54:00 +08:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestUtils.stubClient();
|
|
|
|
DMRoomMap.makeShared();
|
|
|
|
MatrixClientPeg.get().getUserId = jest.fn().mockReturnValue("@bob:example.org");
|
2021-05-09 07:51:51 +08:00
|
|
|
});
|
|
|
|
|
2021-05-10 12:54:00 +08:00
|
|
|
it("shows a preview with us as the sender", async () => {
|
|
|
|
const wrapper = await mountForwardDialog();
|
|
|
|
|
2021-05-09 07:51:51 +08:00
|
|
|
const previewBody = wrapper.find(".mx_EventTile_body");
|
|
|
|
expect(previewBody.text()).toBe("Hello world!");
|
|
|
|
|
|
|
|
// We would just test SenderProfile for the user ID, but it's stubbed
|
|
|
|
const previewAvatar = wrapper.find(".mx_EventTile_avatar .mx_BaseAvatar_image");
|
|
|
|
expect(previewAvatar.prop("title")).toBe("@bob:example.org");
|
|
|
|
});
|
|
|
|
|
2021-05-10 12:54:00 +08:00
|
|
|
it("filters the rooms", async () => {
|
|
|
|
const wrapper = await mountForwardDialog();
|
|
|
|
|
2021-05-10 13:02:12 +08:00
|
|
|
expect(wrapper.find("Entry")).toHaveLength(3);
|
2021-05-09 07:51:51 +08:00
|
|
|
|
2021-05-10 13:02:12 +08:00
|
|
|
const searchInput = wrapper.find("SearchBox input");
|
2021-05-09 07:51:51 +08:00
|
|
|
searchInput.instance().value = "a";
|
|
|
|
searchInput.simulate("change");
|
|
|
|
|
2021-05-10 13:02:12 +08:00
|
|
|
expect(wrapper.find("Entry")).toHaveLength(2);
|
2021-05-09 07:51:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("tracks message sending progress across multiple rooms", async () => {
|
2021-05-10 12:54:00 +08:00
|
|
|
const wrapper = await mountForwardDialog();
|
|
|
|
|
2021-05-09 07:51:51 +08:00
|
|
|
// Make sendEvent require manual resolution so we can see the sending state
|
|
|
|
let finishSend;
|
|
|
|
let cancelSend;
|
2021-05-10 12:54:00 +08:00
|
|
|
MatrixClientPeg.get().sendEvent = jest.fn(() => new Promise((resolve, reject) => {
|
2021-05-09 07:51:51 +08:00
|
|
|
finishSend = resolve;
|
|
|
|
cancelSend = reject;
|
|
|
|
}));
|
|
|
|
|
2021-05-11 01:00:06 +08:00
|
|
|
const firstButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").first();
|
2021-05-22 00:41:29 +08:00
|
|
|
expect(firstButton.render().is(".mx_ForwardList_canSend")).toBe(true);
|
2021-05-09 07:51:51 +08:00
|
|
|
|
2021-05-10 13:02:12 +08:00
|
|
|
act(() => { firstButton.simulate("click"); });
|
2021-05-22 00:41:29 +08:00
|
|
|
expect(firstButton.render().is(".mx_ForwardList_sending")).toBe(true);
|
2021-05-09 07:51:51 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cancelSend();
|
|
|
|
// Wait one tick for the button to realize the send failed
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
});
|
2021-05-22 00:41:29 +08:00
|
|
|
expect(firstButton.render().is(".mx_ForwardList_sendFailed")).toBe(true);
|
2021-05-09 07:51:51 +08:00
|
|
|
|
2021-05-11 01:00:06 +08:00
|
|
|
const secondButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").at(1);
|
2021-05-22 00:41:29 +08:00
|
|
|
expect(secondButton.render().is(".mx_ForwardList_canSend")).toBe(true);
|
2021-05-09 07:51:51 +08:00
|
|
|
|
2021-05-10 13:02:12 +08:00
|
|
|
act(() => { secondButton.simulate("click"); });
|
2021-05-22 00:41:29 +08:00
|
|
|
expect(secondButton.render().is(".mx_ForwardList_sending")).toBe(true);
|
2021-05-09 07:51:51 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
finishSend();
|
|
|
|
// Wait one tick for the button to realize the send succeeded
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
});
|
2021-05-22 00:41:29 +08:00
|
|
|
expect(secondButton.render().is(".mx_ForwardList_sent")).toBe(true);
|
2021-05-09 07:51:51 +08:00
|
|
|
});
|
2021-05-10 12:54:00 +08:00
|
|
|
|
|
|
|
it("can render replies", async () => {
|
|
|
|
const replyMessage = TestUtils.mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "!111111111111111111:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
content: {
|
2021-05-10 13:16:37 +08:00
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "> <@bob:example.org> Hi Alice!\n\nHi Bob!",
|
2021-05-10 12:54:00 +08:00
|
|
|
"m.relates_to": {
|
|
|
|
"m.in_reply_to": {
|
|
|
|
event_id: "$2222222222222222222222222222222222222222222",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapper = await mountForwardDialog(replyMessage);
|
|
|
|
expect(wrapper.find("ReplyThread")).toBeTruthy();
|
|
|
|
});
|
2021-05-10 13:07:42 +08:00
|
|
|
|
|
|
|
it("disables buttons for rooms without send permissions", async () => {
|
|
|
|
const readOnlyRoom = TestUtils.mkStubRoom("a", "a");
|
|
|
|
readOnlyRoom.maySendMessage = jest.fn().mockReturnValue(false);
|
|
|
|
const rooms = [readOnlyRoom, TestUtils.mkStubRoom("b", "b")];
|
|
|
|
|
|
|
|
const wrapper = await mountForwardDialog(undefined, rooms);
|
|
|
|
|
2021-05-11 01:00:06 +08:00
|
|
|
const firstButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").first();
|
2021-05-10 13:07:42 +08:00
|
|
|
expect(firstButton.prop("disabled")).toBe(true);
|
2021-05-11 01:00:06 +08:00
|
|
|
const secondButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").last();
|
2021-05-10 13:07:42 +08:00
|
|
|
expect(secondButton.prop("disabled")).toBe(false);
|
|
|
|
});
|
2021-05-09 07:51:51 +08:00
|
|
|
});
|