element-web/test/components/views/room_settings/RoomProfileSettings-test.tsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.8 KiB
TypeScript
Raw Normal View History

/*
Copyright 2024 New Vector Ltd
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";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { mocked } from "jest-mock";
import { EventType, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { mkStubRoom, stubClient } from "../../../test-utils";
import RoomProfileSettings from "../../../../src/components/views/room_settings/RoomProfileSettings";
Fix display of no avatar in avatar setting controls (#12558) * New user profile UI in User Settings Using new Edit In Place component. * Show avatar upload error * Fix avatar upload error * Wire up errors & feedback for display name setting * Implement avatar upload / remove progress toast * Add 768px breakpoint * Fix display of no avatar in avatar setting controls There was supposed to be a person icon but it was invisible, and also would have been inappropriate for room avatars anyway. This makes it match the designs by being the same as whatever the default avatar is. * Fix room profile display * Update to released compund-web with required components / fixes * Require compound-web 4.4.0 because we do need it * Update snapshots Because of course all the auto-generated IDs of unrelated things have changed. * Fix duplicate import * Fix CSS comment * Update snapshot * Run all the tests so the ids stay the same * Start of a test for ProfileSettings * More tests * Test that a toast appears * Test ToastRack * Update snapshots * Add the usernamee control * Fix playwright tests * New compound version for editinplace fixes * Fix useId to not just generate a constant ID * Use the label in the username component * Fix widths of test boxes * Update screenshots * Put ^ back on compound-web version * Split CSS for room & user profile settings and name the components correspondingly * Fix playwright test * Update room settings screenshot * Use original screenshot instead * Add required props in test * Fix test * Also here * Update screenshots * Remove user icon ...which is unused now, as far as I can see. * Fix styling of unrelated buttons Needed to be added in other places otherwise the specificity changes. Also put the old screenshots back. * Add copyright year * Fix copyright year * Switch to useMatrixClientContext * Fix other test
2024-06-06 17:35:44 +00:00
import DMRoomMap from "../../../../src/utils/DMRoomMap";
const BASE64_GIF = "R0lGODlhAQABAAAAACw=";
const AVATAR_FILE = new File([Uint8Array.from(atob(BASE64_GIF), (c) => c.charCodeAt(0))], "avatar.gif", {
type: "image/gif",
});
const ROOM_ID = "!floob:itty";
describe("RoomProfileSetting", () => {
let client: MatrixClient;
let room: Room;
beforeEach(() => {
Fix display of no avatar in avatar setting controls (#12558) * New user profile UI in User Settings Using new Edit In Place component. * Show avatar upload error * Fix avatar upload error * Wire up errors & feedback for display name setting * Implement avatar upload / remove progress toast * Add 768px breakpoint * Fix display of no avatar in avatar setting controls There was supposed to be a person icon but it was invisible, and also would have been inappropriate for room avatars anyway. This makes it match the designs by being the same as whatever the default avatar is. * Fix room profile display * Update to released compund-web with required components / fixes * Require compound-web 4.4.0 because we do need it * Update snapshots Because of course all the auto-generated IDs of unrelated things have changed. * Fix duplicate import * Fix CSS comment * Update snapshot * Run all the tests so the ids stay the same * Start of a test for ProfileSettings * More tests * Test that a toast appears * Test ToastRack * Update snapshots * Add the usernamee control * Fix playwright tests * New compound version for editinplace fixes * Fix useId to not just generate a constant ID * Use the label in the username component * Fix widths of test boxes * Update screenshots * Put ^ back on compound-web version * Split CSS for room & user profile settings and name the components correspondingly * Fix playwright test * Update room settings screenshot * Use original screenshot instead * Add required props in test * Fix test * Also here * Update screenshots * Remove user icon ...which is unused now, as far as I can see. * Fix styling of unrelated buttons Needed to be added in other places otherwise the specificity changes. Also put the old screenshots back. * Add copyright year * Fix copyright year * Switch to useMatrixClientContext * Fix other test
2024-06-06 17:35:44 +00:00
const dmRoomMap = {
getUserIdForRoomId: jest.fn(),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
client = stubClient();
room = mkStubRoom(ROOM_ID, "Test room", client);
});
it("handles uploading a room avatar", async () => {
const user = userEvent.setup();
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://matrix.org/1234" });
render(<RoomProfileSettings roomId={ROOM_ID} />);
await user.upload(screen.getByAltText("Upload"), AVATAR_FILE);
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(client.uploadContent).toHaveBeenCalledWith(AVATAR_FILE));
await waitFor(() =>
expect(client.sendStateEvent).toHaveBeenCalledWith(
ROOM_ID,
EventType.RoomAvatar,
{
url: "mxc://matrix.org/1234",
},
"",
),
);
});
it("removes a room avatar", async () => {
const user = userEvent.setup();
mocked(client).getRoom.mockReturnValue(room);
mocked(room).currentState.getStateEvents.mockImplementation(
// @ts-ignore
(type: string): MatrixEvent[] | MatrixEvent | null => {
if (type === EventType.RoomAvatar) {
// @ts-ignore
return { getContent: () => ({ url: "mxc://matrix.org/1234" }) };
}
return null;
},
);
render(<RoomProfileSettings roomId="!floob:itty" />);
await user.click(screen.getByRole("button", { name: "Remove" }));
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() =>
expect(client.sendStateEvent).toHaveBeenCalledWith("!floob:itty", EventType.RoomAvatar, {}, ""),
);
});
it("cancels changes", async () => {
const user = userEvent.setup();
render(<RoomProfileSettings roomId="!floob:itty" />);
const roomNameInput = screen.getByLabelText("Room Name");
expect(roomNameInput).toHaveValue("");
await user.type(roomNameInput, "My Room");
expect(roomNameInput).toHaveValue("My Room");
await user.click(screen.getByRole("button", { name: "Cancel" }));
expect(roomNameInput).toHaveValue("");
});
});