From 95630f525fdcd43cb672bba61054f2d191d7b31c Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 20 Nov 2024 18:09:51 +0000 Subject: [PATCH] Remove usage of legacyRoot in RTL tests (#28485) * Remove usage of legacyRoot in RTL tests Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../structures/auth/ForgotPassword.tsx | 12 ++++--- .../components/structures/MatrixChat-test.tsx | 9 +++-- .../structures/TimelinePanel-test.tsx | 35 ++++++++----------- .../structures/auth/ForgotPassword-test.tsx | 5 +-- .../views/messages/DateSeparator-test.tsx | 1 - .../views/messages/MPollEndBody-test.tsx | 6 ++-- .../views/right_panel/UserInfo-test.tsx | 6 ++-- .../views/rooms/MessageComposer-test.tsx | 2 +- .../views/settings/JoinRuleSettings-test.tsx | 2 +- 9 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/components/structures/auth/ForgotPassword.tsx b/src/components/structures/auth/ForgotPassword.tsx index 5c631edb97..0cc6fbd04d 100644 --- a/src/components/structures/auth/ForgotPassword.tsx +++ b/src/components/structures/auth/ForgotPassword.tsx @@ -113,16 +113,16 @@ export default class ForgotPassword extends React.Component { this.unmounted = true; } - private async checkServerLiveliness(serverConfig: ValidatedServerConfig): Promise { + private async checkServerLiveliness(serverConfig: ValidatedServerConfig): Promise { try { await AutoDiscoveryUtils.validateServerConfigWithStaticUrls(serverConfig.hsUrl, serverConfig.isUrl); - if (this.unmounted) return; + if (this.unmounted) return false; this.setState({ serverIsAlive: true, }); } catch (e: any) { - if (this.unmounted) return; + if (this.unmounted) return false; const { serverIsAlive, serverDeadError } = AutoDiscoveryUtils.authComponentStateForError( e, "forgot_password", @@ -131,7 +131,9 @@ export default class ForgotPassword extends React.Component { serverIsAlive, errorText: serverDeadError, }); + return serverIsAlive; } + return true; } private async onPhaseEmailInputSubmit(): Promise { @@ -299,10 +301,10 @@ export default class ForgotPassword extends React.Component { }); // Refresh the server errors. Just in case the server came back online of went offline. - await this.checkServerLiveliness(this.props.serverConfig); + const serverIsAlive = await this.checkServerLiveliness(this.props.serverConfig); // Server error - if (!this.state.serverIsAlive) return; + if (!serverIsAlive) return; switch (this.state.phase) { case Phase.EnterEmail: diff --git a/test/unit-tests/components/structures/MatrixChat-test.tsx b/test/unit-tests/components/structures/MatrixChat-test.tsx index c9662c64f4..5cee50ef29 100644 --- a/test/unit-tests/components/structures/MatrixChat-test.tsx +++ b/test/unit-tests/components/structures/MatrixChat-test.tsx @@ -162,8 +162,11 @@ describe("", () => { }; let initPromise: Promise | undefined; let defaultProps: ComponentProps; - const getComponent = (props: Partial> = {}) => - render(, { legacyRoot: true }); + const getComponent = (props: Partial> = {}) => { + // MatrixChat does many questionable things which bomb tests in modern React mode, + // we'll want to refactor and break up MatrixChat before turning off legacyRoot mode + return render(, { legacyRoot: true }); + }; // make test results readable filterConsole( @@ -1128,7 +1131,7 @@ describe("", () => { await getComponentAndLogin(); - act(() => bootstrapDeferred.resolve()); + bootstrapDeferred.resolve(); await expect( screen.findByRole("heading", { name: "You're in", level: 1 }), diff --git a/test/unit-tests/components/structures/TimelinePanel-test.tsx b/test/unit-tests/components/structures/TimelinePanel-test.tsx index cee7e143d5..2f85843000 100644 --- a/test/unit-tests/components/structures/TimelinePanel-test.tsx +++ b/test/unit-tests/components/structures/TimelinePanel-test.tsx @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only Please see LICENSE files in the repository root for full details. */ -import { render, waitFor, screen, act } from "jest-matrix-react"; +import { render, waitFor, screen, act, cleanup } from "jest-matrix-react"; import { ReceiptType, EventTimelineSet, @@ -28,7 +28,7 @@ import { ThreadFilterType, } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; -import React, { createRef } from "react"; +import React from "react"; import { Mocked, mocked } from "jest-mock"; import { forEachRight } from "lodash"; @@ -178,7 +178,7 @@ describe("TimelinePanel", () => { const roomId = "#room:example.com"; let room: Room; let timelineSet: EventTimelineSet; - let timelinePanel: TimelinePanel; + let timelinePanel: TimelinePanel | null = null; const ev1 = new MatrixEvent({ event_id: "ev1", @@ -197,19 +197,16 @@ describe("TimelinePanel", () => { }); const renderTimelinePanel = async (): Promise => { - const ref = createRef(); render( (timelinePanel = ref)} />, - { legacyRoot: true }, ); await flushPromises(); - await waitFor(() => expect(ref.current).toBeTruthy()); - timelinePanel = ref.current!; + await waitFor(() => expect(timelinePanel).toBeTruthy()); }; const setUpTimelineSet = (threadRoot?: MatrixEvent) => { @@ -234,8 +231,9 @@ describe("TimelinePanel", () => { room = new Room(roomId, client, userId, { pendingEventOrdering: PendingEventOrdering.Detached }); }); - afterEach(() => { + afterEach(async () => { TimelinePanel.roomReadMarkerTsMap = {}; + cleanup(); }); it("when there is no event, it should not send any receipt", async () => { @@ -257,16 +255,13 @@ describe("TimelinePanel", () => { describe("and reading the timeline", () => { beforeEach(async () => { - await act(async () => { - await renderTimelinePanel(); - timelineSet.addLiveEvent(ev1, {}); - await flushPromises(); - - // @ts-ignore - await timelinePanel.sendReadReceipts(); - // @ts-ignore Simulate user activity by calling updateReadMarker on the TimelinePanel. - await timelinePanel.updateReadMarker(); - }); + await renderTimelinePanel(); + timelineSet.addLiveEvent(ev1, {}); + await flushPromises(); + // @ts-ignore + await timelinePanel.sendReadReceipts(); + // @ts-ignore Simulate user activity by calling updateReadMarker on the TimelinePanel. + await timelinePanel.updateReadMarker(); }); it("should send a fully read marker and a public receipt", async () => { @@ -295,7 +290,7 @@ describe("TimelinePanel", () => { // setup, timelineSet is not actually the timelineSet of the room. await room.addLiveEvents([ev2], {}); room.addEphemeralEvents([newReceipt(ev2.getId()!, userId, 222, 200)]); - await timelinePanel.forgetReadMarker(); + await timelinePanel!.forgetReadMarker(); expect(client.setRoomReadMarkers).toHaveBeenCalledWith(roomId, ev2.getId()); }); }); diff --git a/test/unit-tests/components/structures/auth/ForgotPassword-test.tsx b/test/unit-tests/components/structures/auth/ForgotPassword-test.tsx index 413acfbafa..d57c4c6e77 100644 --- a/test/unit-tests/components/structures/auth/ForgotPassword-test.tsx +++ b/test/unit-tests/components/structures/auth/ForgotPassword-test.tsx @@ -8,7 +8,7 @@ Please see LICENSE files in the repository root for full details. import React from "react"; import { mocked } from "jest-mock"; -import { render, RenderResult, screen, waitFor } from "jest-matrix-react"; +import { render, RenderResult, screen, waitFor, cleanup } from "jest-matrix-react"; import userEvent from "@testing-library/user-event"; import { MatrixClient, createClient } from "matrix-js-sdk/src/matrix"; @@ -68,13 +68,13 @@ describe("", () => { afterEach(async () => { // clean up modals await clearAllModals(); + cleanup(); }); describe("when starting a password reset flow", () => { beforeEach(() => { renderResult = render( , - { legacyRoot: true }, ); }); @@ -120,6 +120,7 @@ describe("", () => { describe("and submitting an unknown email", () => { beforeEach(async () => { + mocked(AutoDiscoveryUtils.validateServerConfigWithStaticUrls).mockResolvedValue(serverConfig); await typeIntoField("Email address", testEmail); mocked(client).requestPasswordEmailToken.mockRejectedValue({ errcode: "M_THREEPID_NOT_FOUND", diff --git a/test/unit-tests/components/views/messages/DateSeparator-test.tsx b/test/unit-tests/components/views/messages/DateSeparator-test.tsx index aade46a2e2..9b5b32cfca 100644 --- a/test/unit-tests/components/views/messages/DateSeparator-test.tsx +++ b/test/unit-tests/components/views/messages/DateSeparator-test.tsx @@ -48,7 +48,6 @@ describe("DateSeparator", () => { , - { legacyRoot: true }, ); type TestCase = [string, number, string]; diff --git a/test/unit-tests/components/views/messages/MPollEndBody-test.tsx b/test/unit-tests/components/views/messages/MPollEndBody-test.tsx index 5bf7ab55ea..7bd2ad0a08 100644 --- a/test/unit-tests/components/views/messages/MPollEndBody-test.tsx +++ b/test/unit-tests/components/views/messages/MPollEndBody-test.tsx @@ -7,7 +7,7 @@ Please see LICENSE files in the repository root for full details. */ import React from "react"; -import { render, waitFor, waitForElementToBeRemoved } from "jest-matrix-react"; +import { render, waitFor } from "jest-matrix-react"; import { EventTimeline, MatrixEvent, Room, M_TEXT } from "matrix-js-sdk/src/matrix"; import { logger } from "matrix-js-sdk/src/logger"; @@ -121,13 +121,13 @@ describe("", () => { describe("when poll start event does not exist in current timeline", () => { it("fetches the related poll start event and displays a poll tile", async () => { await setupRoomWithEventsTimeline(pollEndEvent); - const { container, getByTestId, getByRole } = getComponent(); + const { container, getByTestId, getByRole, queryByRole } = getComponent(); // while fetching event, only icon is shown expect(container).toMatchSnapshot(); await waitFor(() => expect(getByRole("progressbar")).toBeInTheDocument()); - await waitForElementToBeRemoved(() => getByRole("progressbar")); + await waitFor(() => expect(queryByRole("progressbar")).not.toBeInTheDocument()); expect(mockClient.fetchRoomEvent).toHaveBeenCalledWith(roomId, pollStartEvent.getId()); diff --git a/test/unit-tests/components/views/right_panel/UserInfo-test.tsx b/test/unit-tests/components/views/right_panel/UserInfo-test.tsx index 441afec700..7e23679fc2 100644 --- a/test/unit-tests/components/views/right_panel/UserInfo-test.tsx +++ b/test/unit-tests/components/views/right_panel/UserInfo-test.tsx @@ -7,7 +7,7 @@ Please see LICENSE files in the repository root for full details. */ import React from "react"; -import { fireEvent, render, screen, cleanup, act, within } from "jest-matrix-react"; +import { fireEvent, render, screen, cleanup, act, within, waitForElementToBeRemoved } from "jest-matrix-react"; import userEvent from "@testing-library/user-event"; import { Mocked, mocked } from "jest-mock"; import { Room, User, MatrixClient, RoomMember, MatrixEvent, EventType, Device } from "matrix-js-sdk/src/matrix"; @@ -199,7 +199,6 @@ describe("", () => { return render(, { wrapper: Wrapper, - legacyRoot: true, }); }; @@ -655,6 +654,9 @@ describe("", () => { }); await expect(screen.findByRole("button", { name: "Deactivate user" })).resolves.toBeInTheDocument(); + if (screen.queryAllByRole("progressbar").length) { + await waitForElementToBeRemoved(() => screen.queryAllByRole("progressbar")); + } expect(container).toMatchSnapshot(); }); }); diff --git a/test/unit-tests/components/views/rooms/MessageComposer-test.tsx b/test/unit-tests/components/views/rooms/MessageComposer-test.tsx index 7d8112c2f8..3bd9a6cf62 100644 --- a/test/unit-tests/components/views/rooms/MessageComposer-test.tsx +++ b/test/unit-tests/components/views/rooms/MessageComposer-test.tsx @@ -519,7 +519,7 @@ function wrapAndRender( ); return { rawComponent: getRawComponent(props, roomContext, mockClient), - renderResult: render(getRawComponent(props, roomContext, mockClient), { legacyRoot: true }), + renderResult: render(getRawComponent(props, roomContext, mockClient)), roomContext, }; } diff --git a/test/unit-tests/components/views/settings/JoinRuleSettings-test.tsx b/test/unit-tests/components/views/settings/JoinRuleSettings-test.tsx index c14f018df0..36dd664ac6 100644 --- a/test/unit-tests/components/views/settings/JoinRuleSettings-test.tsx +++ b/test/unit-tests/components/views/settings/JoinRuleSettings-test.tsx @@ -59,7 +59,7 @@ describe("", () => { onError: jest.fn(), }; const getComponent = (props: Partial = {}) => - render(, { legacyRoot: false }); + render(); const setRoomStateEvents = ( room: Room,