2022-05-06 17:09:28 +08: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.
|
|
|
|
*/
|
|
|
|
|
2021-10-01 21:54:26 +08:00
|
|
|
import React from 'react';
|
2022-03-04 23:53:22 +08:00
|
|
|
import { mount, ReactWrapper } from 'enzyme';
|
2022-03-02 04:42:05 +08:00
|
|
|
import { Room, PendingEventOrdering, MatrixEvent, MatrixClient } from 'matrix-js-sdk/src/matrix';
|
2021-10-01 21:54:26 +08:00
|
|
|
|
2021-10-23 06:23:32 +08:00
|
|
|
import * as TestUtils from '../../../test-utils';
|
2021-10-01 21:54:26 +08:00
|
|
|
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
|
|
|
import DMRoomMap from '../../../../src/utils/DMRoomMap';
|
|
|
|
import RoomHeader from '../../../../src/components/views/rooms/RoomHeader';
|
|
|
|
import { SearchScope } from '../../../../src/components/views/rooms/SearchBar';
|
|
|
|
import { E2EStatus } from '../../../../src/utils/ShieldUtils';
|
|
|
|
import { mkEvent } from '../../../test-utils';
|
2022-03-04 23:53:22 +08:00
|
|
|
import { IRoomState } from "../../../../src/components/structures/RoomView";
|
|
|
|
import RoomContext from '../../../../src/contexts/RoomContext';
|
2021-10-01 21:54:26 +08:00
|
|
|
|
|
|
|
describe('RoomHeader', () => {
|
2021-10-04 18:41:09 +08:00
|
|
|
it('shows the room avatar in a room with only ourselves', () => {
|
2021-10-01 21:54:26 +08:00
|
|
|
// When we render a non-DM room with 1 person in it
|
2021-10-04 18:41:09 +08:00
|
|
|
const room = createRoom({ name: "X Room", isDm: false, userIds: [] });
|
2021-10-01 21:54:26 +08:00
|
|
|
const rendered = render(room);
|
|
|
|
|
|
|
|
// Then the room's avatar is the initial of its name
|
|
|
|
const initial = findSpan(rendered, ".mx_BaseAvatar_initial");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(initial.text()).toEqual("X");
|
2021-10-01 21:54:26 +08:00
|
|
|
|
|
|
|
// And there is no image avatar (because it's not set on this room)
|
|
|
|
const image = findImg(rendered, ".mx_BaseAvatar_image");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(image.prop("src")).toEqual("data:image/png;base64,00");
|
2021-10-01 21:54:26 +08:00
|
|
|
});
|
|
|
|
|
2021-10-04 18:41:09 +08:00
|
|
|
it('shows the room avatar in a room with 2 people', () => {
|
2021-10-01 21:54:26 +08:00
|
|
|
// When we render a non-DM room with 2 people in it
|
|
|
|
const room = createRoom(
|
2021-10-04 18:41:09 +08:00
|
|
|
{ name: "Y Room", isDm: false, userIds: ["other"] });
|
2021-10-01 21:54:26 +08:00
|
|
|
const rendered = render(room);
|
|
|
|
|
|
|
|
// Then the room's avatar is the initial of its name
|
|
|
|
const initial = findSpan(rendered, ".mx_BaseAvatar_initial");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(initial.text()).toEqual("Y");
|
2021-10-01 21:54:26 +08:00
|
|
|
|
|
|
|
// And there is no image avatar (because it's not set on this room)
|
|
|
|
const image = findImg(rendered, ".mx_BaseAvatar_image");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(image.prop("src")).toEqual("data:image/png;base64,00");
|
2021-10-01 21:54:26 +08:00
|
|
|
});
|
|
|
|
|
2021-10-04 18:41:09 +08:00
|
|
|
it('shows the room avatar in a room with >2 people', () => {
|
2021-10-01 21:54:26 +08:00
|
|
|
// When we render a non-DM room with 3 people in it
|
2022-03-04 23:53:22 +08:00
|
|
|
const room = createRoom({ name: "Z Room", isDm: false, userIds: ["other1", "other2"] });
|
2021-10-01 21:54:26 +08:00
|
|
|
const rendered = render(room);
|
|
|
|
|
|
|
|
// Then the room's avatar is the initial of its name
|
|
|
|
const initial = findSpan(rendered, ".mx_BaseAvatar_initial");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(initial.text()).toEqual("Z");
|
2021-10-01 21:54:26 +08:00
|
|
|
|
|
|
|
// And there is no image avatar (because it's not set on this room)
|
|
|
|
const image = findImg(rendered, ".mx_BaseAvatar_image");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(image.prop("src")).toEqual("data:image/png;base64,00");
|
2021-10-01 21:54:26 +08:00
|
|
|
});
|
|
|
|
|
2021-10-04 18:41:09 +08:00
|
|
|
it('shows the room avatar in a DM with only ourselves', () => {
|
2021-10-01 21:54:26 +08:00
|
|
|
// When we render a non-DM room with 1 person in it
|
2021-10-04 18:41:09 +08:00
|
|
|
const room = createRoom({ name: "Z Room", isDm: true, userIds: [] });
|
2021-10-01 21:54:26 +08:00
|
|
|
const rendered = render(room);
|
|
|
|
|
|
|
|
// Then the room's avatar is the initial of its name
|
|
|
|
const initial = findSpan(rendered, ".mx_BaseAvatar_initial");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(initial.text()).toEqual("Z");
|
2021-10-01 21:54:26 +08:00
|
|
|
|
|
|
|
// And there is no image avatar (because it's not set on this room)
|
|
|
|
const image = findImg(rendered, ".mx_BaseAvatar_image");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(image.prop("src")).toEqual("data:image/png;base64,00");
|
2021-10-01 21:54:26 +08:00
|
|
|
});
|
|
|
|
|
2021-10-04 18:41:09 +08:00
|
|
|
it('shows the user avatar in a DM with 2 people', () => {
|
2021-10-01 21:54:26 +08:00
|
|
|
// Note: this is the interesting case - this is the ONLY
|
|
|
|
// time we should use the user's avatar.
|
|
|
|
|
|
|
|
// When we render a DM room with only 2 people in it
|
2021-10-04 18:41:09 +08:00
|
|
|
const room = createRoom({ name: "Y Room", isDm: true, userIds: ["other"] });
|
2021-10-01 21:54:26 +08:00
|
|
|
const rendered = render(room);
|
|
|
|
|
|
|
|
// Then we use the other user's avatar as our room's image avatar
|
|
|
|
const image = findImg(rendered, ".mx_BaseAvatar_image");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(image.prop("src")).toEqual("http://this.is.a.url/example.org/other");
|
2021-10-01 21:54:26 +08:00
|
|
|
|
|
|
|
// And there is no initial avatar
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(rendered.find(".mx_BaseAvatar_initial")).toHaveLength(0);
|
2021-10-01 21:54:26 +08:00
|
|
|
});
|
|
|
|
|
2021-10-04 18:41:09 +08:00
|
|
|
it('shows the room avatar in a DM with >2 people', () => {
|
2021-10-01 21:54:26 +08:00
|
|
|
// When we render a DM room with 3 people in it
|
|
|
|
const room = createRoom({
|
2021-10-04 18:41:09 +08:00
|
|
|
name: "Z Room", isDm: true, userIds: ["other1", "other2"] });
|
2021-10-01 21:54:26 +08:00
|
|
|
const rendered = render(room);
|
|
|
|
|
|
|
|
// Then the room's avatar is the initial of its name
|
|
|
|
const initial = findSpan(rendered, ".mx_BaseAvatar_initial");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(initial.text()).toEqual("Z");
|
2021-10-01 21:54:26 +08:00
|
|
|
|
|
|
|
// And there is no image avatar (because it's not set on this room)
|
|
|
|
const image = findImg(rendered, ".mx_BaseAvatar_image");
|
2022-03-04 23:53:22 +08:00
|
|
|
expect(image.prop("src")).toEqual("data:image/png;base64,00");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders call buttons normally", () => {
|
|
|
|
const room = createRoom({ name: "Room", isDm: false, userIds: [] });
|
|
|
|
const wrapper = render(room);
|
|
|
|
|
|
|
|
expect(wrapper.find('[aria-label="Voice call"]').hostNodes()).toHaveLength(1);
|
|
|
|
expect(wrapper.find('[aria-label="Video call"]').hostNodes()).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("hides call buttons when the room is tombstoned", () => {
|
|
|
|
const room = createRoom({ name: "Room", isDm: false, userIds: [] });
|
|
|
|
const wrapper = render(room, {
|
|
|
|
tombstone: mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.tombstone",
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@user1:server",
|
|
|
|
skey: "",
|
|
|
|
content: {},
|
|
|
|
ts: Date.now(),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find('[aria-label="Voice call"]').hostNodes()).toHaveLength(0);
|
|
|
|
expect(wrapper.find('[aria-label="Video call"]').hostNodes()).toHaveLength(0);
|
2021-10-01 21:54:26 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IRoomCreationInfo {
|
|
|
|
name: string;
|
|
|
|
isDm: boolean;
|
2021-10-04 18:41:09 +08:00
|
|
|
userIds: string[];
|
2021-10-01 21:54:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createRoom(info: IRoomCreationInfo) {
|
|
|
|
TestUtils.stubClient();
|
|
|
|
const client: MatrixClient = MatrixClientPeg.get();
|
|
|
|
|
|
|
|
const roomId = '!1234567890:domain';
|
|
|
|
const userId = client.getUserId();
|
|
|
|
if (info.isDm) {
|
|
|
|
client.getAccountData = (eventType) => {
|
|
|
|
expect(eventType).toEqual("m.direct");
|
2021-10-04 18:41:09 +08:00
|
|
|
return mkDirectEvent(roomId, userId, info.userIds);
|
2021-10-01 21:54:26 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
DMRoomMap.makeShared().start();
|
|
|
|
|
|
|
|
const room = new Room(roomId, client, userId, {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
|
|
|
|
const otherJoinEvents = [];
|
2021-10-04 18:41:09 +08:00
|
|
|
for (const otherUserId of info.userIds) {
|
2021-10-01 21:54:26 +08:00
|
|
|
otherJoinEvents.push(mkJoinEvent(roomId, otherUserId));
|
|
|
|
}
|
|
|
|
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
mkCreationEvent(roomId, userId),
|
|
|
|
mkNameEvent(roomId, userId, info.name),
|
|
|
|
mkJoinEvent(roomId, userId),
|
|
|
|
...otherJoinEvents,
|
|
|
|
]);
|
|
|
|
room.recalculate();
|
|
|
|
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2022-03-04 23:53:22 +08:00
|
|
|
function render(room: Room, roomContext?: Partial<IRoomState>): ReactWrapper {
|
|
|
|
return mount((
|
2022-03-21 17:03:03 +08:00
|
|
|
<RoomContext.Provider value={{ ...roomContext, room } as IRoomState}>
|
2021-10-01 21:54:26 +08:00
|
|
|
<RoomHeader
|
|
|
|
room={room}
|
|
|
|
inRoom={true}
|
|
|
|
onSearchClick={() => {}}
|
2022-04-20 23:03:33 +08:00
|
|
|
onInviteClick={null}
|
2021-10-01 21:54:26 +08:00
|
|
|
onForgetClick={() => {}}
|
2022-03-21 17:03:03 +08:00
|
|
|
onCallPlaced={(_type) => { }}
|
2021-10-01 21:54:26 +08:00
|
|
|
onAppsClick={() => {}}
|
|
|
|
e2eStatus={E2EStatus.Normal}
|
|
|
|
appsShown={true}
|
|
|
|
searchInfo={{
|
|
|
|
searchTerm: "",
|
|
|
|
searchScope: SearchScope.Room,
|
|
|
|
searchCount: 0,
|
|
|
|
}}
|
|
|
|
/>
|
2022-03-04 23:53:22 +08:00
|
|
|
</RoomContext.Provider>
|
|
|
|
));
|
2021-10-01 21:54:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function mkCreationEvent(roomId: string, userId: string): MatrixEvent {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.create",
|
|
|
|
room: roomId,
|
|
|
|
user: userId,
|
|
|
|
content: {
|
|
|
|
creator: userId,
|
|
|
|
room_version: "5",
|
|
|
|
predecessor: {
|
|
|
|
room_id: "!prevroom",
|
|
|
|
event_id: "$someevent",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mkNameEvent(
|
|
|
|
roomId: string, userId: string, name: string,
|
|
|
|
): MatrixEvent {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.name",
|
|
|
|
room: roomId,
|
|
|
|
user: userId,
|
|
|
|
content: { name },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mkJoinEvent(roomId: string, userId: string) {
|
|
|
|
const ret = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.member",
|
|
|
|
room: roomId,
|
|
|
|
user: userId,
|
|
|
|
content: {
|
|
|
|
"membership": "join",
|
|
|
|
"avatar_url": "mxc://example.org/" + userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
ret.event.state_key = userId;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mkDirectEvent(
|
|
|
|
roomId: string, userId: string, otherUsers: string[],
|
|
|
|
): MatrixEvent {
|
|
|
|
const content = {};
|
|
|
|
for (const otherUserId of otherUsers) {
|
|
|
|
content[otherUserId] = [roomId];
|
|
|
|
}
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.direct",
|
|
|
|
room: roomId,
|
|
|
|
user: userId,
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-04 23:53:22 +08:00
|
|
|
function findSpan(wrapper: ReactWrapper, selector: string): ReactWrapper {
|
|
|
|
const els = wrapper.find(selector).hostNodes();
|
|
|
|
expect(els).toHaveLength(1);
|
|
|
|
return els.at(0);
|
2021-10-01 21:54:26 +08:00
|
|
|
}
|
|
|
|
|
2022-03-04 23:53:22 +08:00
|
|
|
function findImg(wrapper: ReactWrapper, selector: string): ReactWrapper {
|
|
|
|
const els = wrapper.find(selector).hostNodes();
|
|
|
|
expect(els).toHaveLength(1);
|
|
|
|
return els.at(0);
|
2021-10-01 21:54:26 +08:00
|
|
|
}
|