From 2e097a00c7fd0bf9707f89985646b49083f54b0a Mon Sep 17 00:00:00 2001 From: alunturner <56027671+alunturner@users.noreply.github.com> Date: Wed, 28 Dec 2022 16:24:46 +0000 Subject: [PATCH] Convert enzyme to rtl: BeaconStatus (#9836) * rewrite in rtl * update snapshots --- .../views/beacon/BeaconStatus-test.tsx | 38 ++-- .../__snapshots__/BeaconStatus-test.tsx.snap | 171 ++++-------------- 2 files changed, 54 insertions(+), 155 deletions(-) diff --git a/test/components/views/beacon/BeaconStatus-test.tsx b/test/components/views/beacon/BeaconStatus-test.tsx index 06ff6aa7e85..02ed4620b88 100644 --- a/test/components/views/beacon/BeaconStatus-test.tsx +++ b/test/components/views/beacon/BeaconStatus-test.tsx @@ -15,13 +15,12 @@ limitations under the License. */ import React from "react"; -// eslint-disable-next-line deprecate/import -import { mount } from "enzyme"; +import { render, screen } from "@testing-library/react"; import { Beacon } from "matrix-js-sdk/src/matrix"; import BeaconStatus from "../../../../src/components/views/beacon/BeaconStatus"; import { BeaconDisplayStatus } from "../../../../src/components/views/beacon/displayStatus"; -import { findByTestId, makeBeaconInfoEvent } from "../../../test-utils"; +import { makeBeaconInfoEvent } from "../../../test-utils"; describe("", () => { const defaultProps = { @@ -29,21 +28,22 @@ describe("", () => { label: "test label", withIcon: true, }; - const getComponent = (props = {}) => mount(); + const renderComponent = (props = {}) => render(); it("renders loading state", () => { - const component = getComponent({ displayStatus: BeaconDisplayStatus.Loading }); - expect(component).toMatchSnapshot(); + const { asFragment } = renderComponent({ displayStatus: BeaconDisplayStatus.Loading }); + expect(asFragment()).toMatchSnapshot(); }); it("renders stopped state", () => { - const component = getComponent({ displayStatus: BeaconDisplayStatus.Stopped }); - expect(component).toMatchSnapshot(); + const { asFragment } = renderComponent({ displayStatus: BeaconDisplayStatus.Stopped }); + expect(asFragment()).toMatchSnapshot(); }); it("renders without icon", () => { - const component = getComponent({ withIcon: false, displayStatus: BeaconDisplayStatus.Stopped }); - expect(component.find("StyledLiveBeaconIcon").length).toBeFalsy(); + const iconClassName = "mx_StyledLiveBeaconIcon"; + const { container } = renderComponent({ withIcon: false, displayStatus: BeaconDisplayStatus.Stopped }); + expect(container.getElementsByClassName(iconClassName)).toHaveLength(0); }); describe("active state", () => { @@ -51,38 +51,38 @@ describe("", () => { // mock for stable snapshot jest.spyOn(Date, "now").mockReturnValue(123456789); const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:server", { isLive: false }, "$1")); - const component = getComponent({ beacon, displayStatus: BeaconDisplayStatus.Active }); - expect(component).toMatchSnapshot(); + const { asFragment } = renderComponent({ beacon, displayStatus: BeaconDisplayStatus.Active }); + expect(asFragment()).toMatchSnapshot(); }); it("renders with children", () => { const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:sever", { isLive: false })); - const component = getComponent({ + renderComponent({ beacon, - children: test, + children: test, displayStatus: BeaconDisplayStatus.Active, }); - expect(findByTestId(component, "test-child")).toMatchSnapshot(); + expect(screen.getByTestId("test-child")).toMatchSnapshot(); }); it("renders static remaining time when displayLiveTimeRemaining is falsy", () => { // mock for stable snapshot jest.spyOn(Date, "now").mockReturnValue(123456789); const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:server", { isLive: false }, "$1")); - const component = getComponent({ beacon, displayStatus: BeaconDisplayStatus.Active }); - expect(component.text().includes("Live until 11:17")).toBeTruthy(); + renderComponent({ beacon, displayStatus: BeaconDisplayStatus.Active }); + expect(screen.getByText("Live until 11:17")).toBeInTheDocument(); }); it("renders live time remaining when displayLiveTimeRemaining is truthy", () => { // mock for stable snapshot jest.spyOn(Date, "now").mockReturnValue(123456789); const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:server", { isLive: false }, "$1")); - const component = getComponent({ + renderComponent({ beacon, displayStatus: BeaconDisplayStatus.Active, displayLiveTimeRemaining: true, }); - expect(component.text().includes("1h left")).toBeTruthy(); + expect(screen.getByText("1h left")).toBeInTheDocument(); }); }); }); diff --git a/test/components/views/beacon/__snapshots__/BeaconStatus-test.tsx.snap b/test/components/views/beacon/__snapshots__/BeaconStatus-test.tsx.snap index 765859b41f5..b0def808248 100644 --- a/test/components/views/beacon/__snapshots__/BeaconStatus-test.tsx.snap +++ b/test/components/views/beacon/__snapshots__/BeaconStatus-test.tsx.snap @@ -1,178 +1,77 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[` active state renders with children 1`] = `null`; +exports[` active state renders with children 1`] = ` + + test + +`; exports[` active state renders without children 1`] = ` - + - - - + test label - - - Live until 11:17 - - + Live until 11:17 + - + `; exports[` renders loading state 1`] = ` - + - - - + Loading live location... - + `; exports[` renders stopped state 1`] = ` - + - - - + Live location ended - + `;