2022-03-04 18:53:22 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 * as React from "react";
|
|
|
|
import { mount, ReactWrapper } from "enzyme";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
|
|
|
import { createTestClient, mkEvent, mkStubRoom, stubClient } from "../../../test-utils";
|
|
|
|
import MessageComposer from "../../../../src/components/views/rooms/MessageComposer";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import RoomContext from "../../../../src/contexts/RoomContext";
|
2022-03-21 12:03:03 +03:00
|
|
|
import { IRoomState } from "../../../../src/components/structures/RoomView";
|
|
|
|
import ResizeNotifier from "../../../../src/utils/ResizeNotifier";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2022-03-04 18:53:22 +03:00
|
|
|
|
|
|
|
describe("MessageComposer", () => {
|
|
|
|
stubClient();
|
|
|
|
const cli = createTestClient();
|
|
|
|
const room = mkStubRoom("!roomId:server", "Room 1", cli);
|
|
|
|
|
|
|
|
it("Renders a SendMessageComposer and MessageComposerButtons by default", () => {
|
2022-03-21 12:03:03 +03:00
|
|
|
const wrapper = wrapAndRender({ room });
|
2022-03-04 18:53:22 +03:00
|
|
|
|
|
|
|
expect(wrapper.find("SendMessageComposer")).toHaveLength(1);
|
|
|
|
expect(wrapper.find("MessageComposerButtons")).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Does not render a SendMessageComposer or MessageComposerButtons when user has no permission", () => {
|
2022-03-21 12:03:03 +03:00
|
|
|
const wrapper = wrapAndRender({ room }, false);
|
2022-03-04 18:53:22 +03:00
|
|
|
|
|
|
|
expect(wrapper.find("SendMessageComposer")).toHaveLength(0);
|
|
|
|
expect(wrapper.find("MessageComposerButtons")).toHaveLength(0);
|
|
|
|
expect(wrapper.find(".mx_MessageComposer_noperm_error")).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Does not render a SendMessageComposer or MessageComposerButtons when room is tombstoned", () => {
|
2022-03-21 12:03:03 +03:00
|
|
|
const wrapper = wrapAndRender({ room }, true, mkEvent({
|
2022-03-04 18:53:22 +03:00
|
|
|
event: true,
|
|
|
|
type: "m.room.tombstone",
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@user1:server",
|
|
|
|
skey: "",
|
|
|
|
content: {},
|
|
|
|
ts: Date.now(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(wrapper.find("SendMessageComposer")).toHaveLength(0);
|
|
|
|
expect(wrapper.find("MessageComposerButtons")).toHaveLength(0);
|
2022-06-27 11:43:58 +03:00
|
|
|
expect(wrapper.find(".mx_MessageComposer_roomReplaced_header")).toHaveLength(1);
|
2022-03-04 18:53:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-21 12:03:03 +03:00
|
|
|
function wrapAndRender(props = {}, canSendMessages = true, tombstone?: MatrixEvent): ReactWrapper {
|
2022-03-04 18:53:22 +03:00
|
|
|
const mockClient = MatrixClientPeg.get();
|
|
|
|
const roomId = "myroomid";
|
|
|
|
const room: any = {
|
|
|
|
currentState: undefined,
|
|
|
|
roomId,
|
|
|
|
client: mockClient,
|
|
|
|
getMember: function(userId: string): RoomMember {
|
|
|
|
return new RoomMember(roomId, userId);
|
|
|
|
},
|
|
|
|
};
|
2022-03-21 12:03:03 +03:00
|
|
|
|
|
|
|
const roomState = {
|
|
|
|
room, canSendMessages, tombstone,
|
|
|
|
} as unknown as IRoomState;
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
room,
|
|
|
|
resizeNotifier: new ResizeNotifier(),
|
|
|
|
permalinkCreator: new RoomPermalinkCreator(room),
|
|
|
|
};
|
|
|
|
|
2022-03-04 18:53:22 +03:00
|
|
|
return mount(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
2022-03-21 12:03:03 +03:00
|
|
|
<RoomContext.Provider value={roomState}>
|
|
|
|
<MessageComposer {...defaultProps} {...props} />
|
2022-03-04 18:53:22 +03:00
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
}
|