2021-05-09 01:51:51 +02:00
|
|
|
/*
|
2021-06-06 03:41:28 +02:00
|
|
|
Copyright 2021 Robin Townsend <robin@robin.town>
|
2021-05-09 01:51:51 +02: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 React from "react";
|
2021-10-13 12:09:43 +02:00
|
|
|
import { mount } from "enzyme";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { act } from "react-dom/test-utils";
|
2022-05-03 10:22:38 +02:00
|
|
|
import { MatrixEvent, EventType } from "matrix-js-sdk/src/matrix";
|
2022-05-10 18:21:20 +02:00
|
|
|
import { ReactWrapper } from "enzyme";
|
|
|
|
import { LocationAssetType, M_ASSET, M_LOCATION, M_TIMESTAMP } from "matrix-js-sdk/src/@types/location";
|
|
|
|
import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events";
|
2021-05-09 01:51:51 +02:00
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-05-03 10:22:38 +02:00
|
|
|
import ForwardDialog from "../../../../src/components/views/dialogs/ForwardDialog";
|
2021-05-09 01:51:51 +02:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2022-05-03 10:22:38 +02:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
2022-05-10 18:21:20 +02:00
|
|
|
makeLegacyLocationEvent,
|
|
|
|
makeLocationEvent,
|
2022-05-03 10:22:38 +02:00
|
|
|
mkEvent,
|
|
|
|
mkMessage,
|
|
|
|
mkStubRoom,
|
|
|
|
} from "../../../test-utils";
|
2022-05-10 18:21:20 +02:00
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
2021-05-09 01:51:51 +02:00
|
|
|
|
|
|
|
describe("ForwardDialog", () => {
|
2021-05-10 06:54:00 +02:00
|
|
|
const sourceRoom = "!111111111111111111:example.org";
|
2022-05-03 10:22:38 +02:00
|
|
|
const aliceId = "@alice:example.org";
|
|
|
|
const defaultMessage = mkMessage({
|
2021-05-10 06:54:00 +02:00
|
|
|
room: sourceRoom,
|
2022-05-03 10:22:38 +02:00
|
|
|
user: aliceId,
|
2021-05-09 01:51:51 +02:00
|
|
|
msg: "Hello world!",
|
|
|
|
event: true,
|
|
|
|
});
|
2022-05-03 10:22:38 +02:00
|
|
|
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(),
|
2022-05-10 18:21:20 +02:00
|
|
|
getClientWellKnown: jest.fn().mockReturnValue({
|
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: 'maps.com' },
|
|
|
|
}),
|
2022-05-03 10:22:38 +02:00
|
|
|
});
|
|
|
|
const defaultRooms = ["a", "A", "b"].map(name => mkStubRoom(name, name, mockClient));
|
2021-05-09 01:51:51 +02:00
|
|
|
|
2021-05-10 06:54:00 +02:00
|
|
|
const mountForwardDialog = async (message = defaultMessage, rooms = defaultRooms) => {
|
2022-05-03 10:22:38 +02:00
|
|
|
mockClient.getVisibleRooms.mockReturnValue(rooms);
|
|
|
|
mockClient.getRoom.mockImplementation(roomId => rooms.find(room => room.roomId === roomId));
|
2021-05-09 01:51:51 +02:00
|
|
|
|
2021-05-10 06:54:00 +02:00
|
|
|
let wrapper;
|
2021-05-09 01:51:51 +02:00
|
|
|
await act(async () => {
|
|
|
|
wrapper = mount(
|
|
|
|
<ForwardDialog
|
2022-05-03 10:22:38 +02:00
|
|
|
matrixClient={mockClient}
|
2021-05-09 01:51:51 +02:00
|
|
|
event={message}
|
2021-05-10 06:54:00 +02:00
|
|
|
permalinkCreator={new RoomPermalinkCreator(undefined, sourceRoom)}
|
2021-05-09 01:51:51 +02: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));
|
|
|
|
});
|
2022-03-03 13:54:20 +01:00
|
|
|
wrapper.update();
|
2021-05-10 06:54:00 +02:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
DMRoomMap.makeShared();
|
2022-05-03 10:22:38 +02:00
|
|
|
jest.clearAllMocks();
|
|
|
|
mockClient.getUserId.mockReturnValue("@bob:example.org");
|
|
|
|
mockClient.sendEvent.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.spyOn(MatrixClientPeg, 'get').mockRestore();
|
2021-05-09 01:51:51 +02:00
|
|
|
});
|
|
|
|
|
2021-05-10 06:54:00 +02:00
|
|
|
it("shows a preview with us as the sender", async () => {
|
|
|
|
const wrapper = await mountForwardDialog();
|
|
|
|
|
2021-05-09 01:51:51 +02: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 06:54:00 +02:00
|
|
|
it("filters the rooms", async () => {
|
|
|
|
const wrapper = await mountForwardDialog();
|
|
|
|
|
2021-05-10 07:02:12 +02:00
|
|
|
expect(wrapper.find("Entry")).toHaveLength(3);
|
2021-05-09 01:51:51 +02:00
|
|
|
|
2021-05-10 07:02:12 +02:00
|
|
|
const searchInput = wrapper.find("SearchBox input");
|
2021-05-09 01:51:51 +02:00
|
|
|
searchInput.instance().value = "a";
|
|
|
|
searchInput.simulate("change");
|
|
|
|
|
2021-05-10 07:02:12 +02:00
|
|
|
expect(wrapper.find("Entry")).toHaveLength(2);
|
2021-05-09 01:51:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("tracks message sending progress across multiple rooms", async () => {
|
2021-05-10 06:54:00 +02:00
|
|
|
const wrapper = await mountForwardDialog();
|
|
|
|
|
2021-05-09 01:51:51 +02:00
|
|
|
// Make sendEvent require manual resolution so we can see the sending state
|
|
|
|
let finishSend;
|
|
|
|
let cancelSend;
|
2022-05-03 10:22:38 +02:00
|
|
|
mockClient.sendEvent.mockImplementation(() => new Promise((resolve, reject) => {
|
2021-05-09 01:51:51 +02:00
|
|
|
finishSend = resolve;
|
|
|
|
cancelSend = reject;
|
|
|
|
}));
|
|
|
|
|
2022-03-03 13:54:20 +01:00
|
|
|
let firstButton;
|
|
|
|
let secondButton;
|
|
|
|
const update = () => {
|
|
|
|
wrapper.update();
|
|
|
|
firstButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").first();
|
|
|
|
secondButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").at(1);
|
|
|
|
};
|
|
|
|
update();
|
|
|
|
|
|
|
|
expect(firstButton.is(".mx_ForwardList_canSend")).toBe(true);
|
2021-05-09 01:51:51 +02:00
|
|
|
|
2021-05-10 07:02:12 +02:00
|
|
|
act(() => { firstButton.simulate("click"); });
|
2022-03-03 13:54:20 +01:00
|
|
|
update();
|
|
|
|
expect(firstButton.is(".mx_ForwardList_sending")).toBe(true);
|
2021-05-09 01:51:51 +02:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cancelSend();
|
|
|
|
// Wait one tick for the button to realize the send failed
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
});
|
2022-03-03 13:54:20 +01:00
|
|
|
update();
|
|
|
|
expect(firstButton.is(".mx_ForwardList_sendFailed")).toBe(true);
|
2021-05-09 01:51:51 +02:00
|
|
|
|
2022-03-03 13:54:20 +01:00
|
|
|
expect(secondButton.is(".mx_ForwardList_canSend")).toBe(true);
|
2021-05-09 01:51:51 +02:00
|
|
|
|
2021-05-10 07:02:12 +02:00
|
|
|
act(() => { secondButton.simulate("click"); });
|
2022-03-03 13:54:20 +01:00
|
|
|
update();
|
|
|
|
expect(secondButton.is(".mx_ForwardList_sending")).toBe(true);
|
2021-05-09 01:51:51 +02:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
finishSend();
|
|
|
|
// Wait one tick for the button to realize the send succeeded
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
});
|
2022-03-03 13:54:20 +01:00
|
|
|
update();
|
|
|
|
expect(secondButton.is(".mx_ForwardList_sent")).toBe(true);
|
2021-05-09 01:51:51 +02:00
|
|
|
});
|
2021-05-10 06:54:00 +02:00
|
|
|
|
|
|
|
it("can render replies", async () => {
|
2022-05-03 10:22:38 +02:00
|
|
|
const replyMessage = mkEvent({
|
2021-05-10 06:54:00 +02:00
|
|
|
type: "m.room.message",
|
|
|
|
room: "!111111111111111111:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
content: {
|
2021-05-10 07:16:37 +02:00
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "> <@bob:example.org> Hi Alice!\n\nHi Bob!",
|
2021-05-10 06:54:00 +02:00
|
|
|
"m.relates_to": {
|
|
|
|
"m.in_reply_to": {
|
|
|
|
event_id: "$2222222222222222222222222222222222222222222",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapper = await mountForwardDialog(replyMessage);
|
2021-10-15 18:42:44 +02:00
|
|
|
expect(wrapper.find("ReplyChain")).toBeTruthy();
|
2021-05-10 06:54:00 +02:00
|
|
|
});
|
2021-05-10 07:07:42 +02:00
|
|
|
|
|
|
|
it("disables buttons for rooms without send permissions", async () => {
|
2022-05-03 10:22:38 +02:00
|
|
|
const readOnlyRoom = mkStubRoom("a", "a", mockClient);
|
2021-05-10 07:07:42 +02:00
|
|
|
readOnlyRoom.maySendMessage = jest.fn().mockReturnValue(false);
|
2022-05-03 10:22:38 +02:00
|
|
|
const rooms = [readOnlyRoom, mkStubRoom("b", "b", mockClient)];
|
2021-05-10 07:07:42 +02:00
|
|
|
|
|
|
|
const wrapper = await mountForwardDialog(undefined, rooms);
|
|
|
|
|
2021-05-10 19:00:06 +02:00
|
|
|
const firstButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").first();
|
2021-05-10 07:07:42 +02:00
|
|
|
expect(firstButton.prop("disabled")).toBe(true);
|
2021-05-10 19:00:06 +02:00
|
|
|
const secondButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").last();
|
2021-05-10 07:07:42 +02:00
|
|
|
expect(secondButton.prop("disabled")).toBe(false);
|
|
|
|
});
|
2022-05-10 18:21:20 +02:00
|
|
|
|
|
|
|
describe('Location events', () => {
|
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
const roomId = "a";
|
|
|
|
const geoUri = "geo:51.5076,-0.1276";
|
|
|
|
const legacyLocationEvent = makeLegacyLocationEvent(geoUri);
|
|
|
|
const modernLocationEvent = makeLocationEvent(geoUri);
|
|
|
|
const pinDropLocationEvent = makeLocationEvent(geoUri, LocationAssetType.Pin);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// legacy events will default timestamp to Date.now()
|
|
|
|
// mock a stable now for easy assertion
|
|
|
|
jest.spyOn(Date, 'now').mockReturnValue(now);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.spyOn(Date, 'now').mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
const sendToFirstRoom = (wrapper: ReactWrapper): void =>
|
|
|
|
act(() => {
|
|
|
|
const sendToFirstRoomButton = wrapper.find("AccessibleButton.mx_ForwardList_sendButton").first();
|
|
|
|
sendToFirstRoomButton.simulate("click");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('converts legacy location events to pin drop shares', async () => {
|
|
|
|
const wrapper = await mountForwardDialog(legacyLocationEvent);
|
|
|
|
|
|
|
|
expect(wrapper.find('MLocationBody').length).toBeTruthy();
|
|
|
|
sendToFirstRoom(wrapper);
|
|
|
|
|
|
|
|
// text and description from original event are removed
|
|
|
|
// text gets new default message from event values
|
|
|
|
// timestamp is defaulted to now
|
|
|
|
const text = `Location ${geoUri} at ${new Date(now).toISOString()}`;
|
|
|
|
const expectedStrippedContent = {
|
|
|
|
...modernLocationEvent.getContent(),
|
|
|
|
body: text,
|
|
|
|
[TEXT_NODE_TYPE.name]: text,
|
|
|
|
[M_TIMESTAMP.name]: now,
|
|
|
|
[M_ASSET.name]: { type: LocationAssetType.Pin },
|
|
|
|
[M_LOCATION.name]: {
|
|
|
|
uri: geoUri,
|
|
|
|
description: undefined,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
|
|
|
roomId, legacyLocationEvent.getType(), expectedStrippedContent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes personal information from static self location shares', async () => {
|
|
|
|
const wrapper = await mountForwardDialog(modernLocationEvent);
|
|
|
|
|
|
|
|
expect(wrapper.find('MLocationBody').length).toBeTruthy();
|
|
|
|
sendToFirstRoom(wrapper);
|
|
|
|
|
|
|
|
const timestamp = M_TIMESTAMP.findIn<number>(modernLocationEvent.getContent());
|
|
|
|
// text and description from original event are removed
|
|
|
|
// text gets new default message from event values
|
|
|
|
const text = `Location ${geoUri} at ${new Date(timestamp).toISOString()}`;
|
|
|
|
const expectedStrippedContent = {
|
|
|
|
...modernLocationEvent.getContent(),
|
|
|
|
body: text,
|
|
|
|
[TEXT_NODE_TYPE.name]: text,
|
|
|
|
[M_ASSET.name]: { type: LocationAssetType.Pin },
|
|
|
|
[M_LOCATION.name]: {
|
|
|
|
uri: geoUri,
|
|
|
|
description: undefined,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
|
|
|
roomId, modernLocationEvent.getType(), expectedStrippedContent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forwards pin drop event', async () => {
|
|
|
|
const wrapper = await mountForwardDialog(pinDropLocationEvent);
|
|
|
|
|
|
|
|
expect(wrapper.find('MLocationBody').length).toBeTruthy();
|
|
|
|
|
|
|
|
sendToFirstRoom(wrapper);
|
|
|
|
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
|
|
|
roomId, pinDropLocationEvent.getType(), pinDropLocationEvent.getContent(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-05-09 01:51:51 +02:00
|
|
|
});
|