Fix broken jest tests

This commit is contained in:
R Midhun Suresh
2024-12-16 23:44:58 +05:30
parent 83265ff22f
commit e78f6ba01f
3 changed files with 39 additions and 41 deletions

View File

@@ -124,7 +124,7 @@ describe("RightPanel", () => {
await waitFor(() => expect(screen.queryByTestId("spinner")).not.toBeInTheDocument()); await waitFor(() => expect(screen.queryByTestId("spinner")).not.toBeInTheDocument());
// room one will be in the MemberList phase - confirm this is rendered // room one will be in the MemberList phase - confirm this is rendered
expect(container.getElementsByClassName("mx_MemberList")).toHaveLength(1); expect(container.getElementsByClassName("mx_MemberListView")).toHaveLength(1);
// wait for RPS room 2 updates to fire, then rerender // wait for RPS room 2 updates to fire, then rerender
const _rpsUpdated = waitForRpsUpdate(); const _rpsUpdated = waitForRpsUpdate();
@@ -146,7 +146,7 @@ describe("RightPanel", () => {
// the correct right panel state for whichever room we are showing, so we // the correct right panel state for whichever room we are showing, so we
// confirm we do not have the MemberList class on the page and that we have // confirm we do not have the MemberList class on the page and that we have
// the expected room title // the expected room title
expect(container.getElementsByClassName("mx_MemberList")).toHaveLength(0); expect(container.getElementsByClassName("mx_MemberListView")).toHaveLength(0);
expect(screen.getByRole("heading", { name: "r2" })).toBeInTheDocument(); expect(screen.getByRole("heading", { name: "r2" })).toBeInTheDocument();
}); });
}); });

View File

@@ -2029,7 +2029,7 @@ exports[`RoomView video rooms should render joined video room view 1`] = `
</div> </div>
<div <div
class="mx_RightPanel_ResizeWrapper" class="mx_RightPanel_ResizeWrapper"
style="position: relative; user-select: auto; width: 420px; height: 100%; max-width: 50%; min-width: 264px; box-sizing: border-box; flex-shrink: 0;" style="position: relative; user-select: auto; width: 420px; height: 100%; max-width: 50%; min-width: 320px; box-sizing: border-box; flex-shrink: 0;"
> >
<aside <aside
class="mx_RightPanel" class="mx_RightPanel"

View File

@@ -162,7 +162,7 @@ describe("MemberListView and MemberlistHeaderView", () => {
} }
} }
function renderMemberList(enablePresence: boolean, usersPerLevel: number = 2): void { async function renderMemberList(enablePresence: boolean, usersPerLevel: number = 2): Promise<void> {
TestUtils.stubClient(); TestUtils.stubClient();
client = MatrixClientPeg.safeGet(); client = MatrixClientPeg.safeGet();
client.hasLazyLoadMembersEnabled = () => false; client.hasLazyLoadMembersEnabled = () => false;
@@ -231,6 +231,9 @@ describe("MemberListView and MemberlistHeaderView", () => {
</SDKContext.Provider> </SDKContext.Provider>
</MatrixClientContext.Provider>, </MatrixClientContext.Provider>,
); );
await waitFor(async () => {
expect(root.container.querySelectorAll(".mx_MemberTileView")).toHaveLength(usersPerLevel * 3);
});
} }
async function reRenderMemberList(): Promise<void> { async function reRenderMemberList(): Promise<void> {
@@ -246,13 +249,8 @@ describe("MemberListView and MemberlistHeaderView", () => {
} }
describe("MemberListView", () => { describe("MemberListView", () => {
beforeEach(function () { beforeEach(async function () {
renderMemberList(true); await renderMemberList(true);
});
it("Renders correctly", async () => {
// Should have rendered 6 members
expect(root.container.querySelectorAll(".mx_MemberTileView")).toHaveLength(6);
}); });
it("Memberlist is re-rendered on unreachable presence event", async () => { it("Memberlist is re-rendered on unreachable presence event", async () => {
@@ -275,8 +273,8 @@ describe("MemberListView and MemberlistHeaderView", () => {
}); });
describe.each([true, false])("does order members correctly (presence %s)", (enablePresence) => { describe.each([true, false])("does order members correctly (presence %s)", (enablePresence) => {
beforeEach(function () { beforeEach(async function () {
renderMemberList(enablePresence); await renderMemberList(enablePresence);
}); });
describe("does order members correctly", () => { describe("does order members correctly", () => {
@@ -368,8 +366,8 @@ describe("MemberListView and MemberlistHeaderView", () => {
}); });
describe("MemberListHeaderView", () => { describe("MemberListHeaderView", () => {
beforeEach(function () { beforeEach(async function () {
renderMemberList(true); await renderMemberList(true);
}); });
it("Shows the correct member count", async () => { it("Shows the correct member count", async () => {
@@ -404,35 +402,35 @@ describe("MemberListView and MemberlistHeaderView", () => {
it("Does not render invite button when user is not a member", async () => {}); it("Does not render invite button when user is not a member", async () => {});
it("does not render invite button UI customisation hides invites", async () => { it("does not render invite button UI customisation hides invites", async () => {});
it("Renders disabled invite button when current user is a member but does not have rights to invite", async () => {
jest.spyOn(memberListRoom, "getMyMembership").mockReturnValue(KnownMembership.Join);
jest.spyOn(memberListRoom, "canInvite").mockReturnValue(false);
mocked(shouldShowComponent).mockReturnValue(true);
await reRenderMemberList();
expect(screen.getByRole("button", { name: "Invite" })).toHaveAttribute("aria-disabled", "true");
});
it("Renders enabled invite button when current user is a member and has rights to invite", async () => { it("Renders disabled invite button when current user is a member but does not have rights to invite", async () => {
jest.spyOn(memberListRoom, "getMyMembership").mockReturnValue(KnownMembership.Join); jest.spyOn(memberListRoom, "getMyMembership").mockReturnValue(KnownMembership.Join);
jest.spyOn(memberListRoom, "canInvite").mockReturnValue(true); jest.spyOn(memberListRoom, "canInvite").mockReturnValue(false);
mocked(shouldShowComponent).mockReturnValue(true); mocked(shouldShowComponent).mockReturnValue(true);
await reRenderMemberList(); await reRenderMemberList();
expect(screen.getByRole("button", { name: "Invite" })).not.toHaveAttribute("aria-disabled", "true"); expect(screen.getByRole("button", { name: "Invite" })).toHaveAttribute("aria-disabled", "true");
}); });
it("Opens room inviter on button click", async () => { it("Renders enabled invite button when current user is a member and has rights to invite", async () => {
jest.spyOn(defaultDispatcher, "dispatch"); jest.spyOn(memberListRoom, "getMyMembership").mockReturnValue(KnownMembership.Join);
jest.spyOn(memberListRoom, "getMyMembership").mockReturnValue(KnownMembership.Join); jest.spyOn(memberListRoom, "canInvite").mockReturnValue(true);
jest.spyOn(memberListRoom, "canInvite").mockReturnValue(true); mocked(shouldShowComponent).mockReturnValue(true);
mocked(shouldShowComponent).mockReturnValue(true); await reRenderMemberList();
await reRenderMemberList(); expect(screen.getByRole("button", { name: "Invite" })).not.toHaveAttribute("aria-disabled", "true");
});
fireEvent.click(screen.getByRole("button", { name: "Invite" })); it("Opens room inviter on button click", async () => {
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ jest.spyOn(defaultDispatcher, "dispatch");
action: "view_invite", jest.spyOn(memberListRoom, "getMyMembership").mockReturnValue(KnownMembership.Join);
roomId: memberListRoom.roomId, jest.spyOn(memberListRoom, "canInvite").mockReturnValue(true);
}); mocked(shouldShowComponent).mockReturnValue(true);
await reRenderMemberList();
fireEvent.click(screen.getByRole("button", { name: "Invite" }));
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({
action: "view_invite",
roomId: memberListRoom.roomId,
}); });
}); });
}); });