2022-04-13 16:44:15 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-13 16:44:15 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-04-13 16:44:15 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2022-12-29 00:24:46 +08:00
|
|
|
import { render, screen } from "@testing-library/react";
|
2022-04-13 16:44:15 +08:00
|
|
|
import { Beacon } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import BeaconStatus from "../../../../src/components/views/beacon/BeaconStatus";
|
|
|
|
import { BeaconDisplayStatus } from "../../../../src/components/views/beacon/displayStatus";
|
2022-12-29 00:24:46 +08:00
|
|
|
import { makeBeaconInfoEvent } from "../../../test-utils";
|
2022-04-13 16:44:15 +08:00
|
|
|
|
|
|
|
describe("<BeaconStatus />", () => {
|
|
|
|
const defaultProps = {
|
|
|
|
displayStatus: BeaconDisplayStatus.Loading,
|
2022-04-14 20:41:28 +08:00
|
|
|
label: "test label",
|
2022-04-20 19:57:50 +08:00
|
|
|
withIcon: true,
|
2022-04-13 16:44:15 +08:00
|
|
|
};
|
2022-12-29 00:24:46 +08:00
|
|
|
const renderComponent = (props = {}) => render(<BeaconStatus {...defaultProps} {...props} />);
|
2022-04-13 16:44:15 +08:00
|
|
|
|
|
|
|
it("renders loading state", () => {
|
2022-12-29 00:24:46 +08:00
|
|
|
const { asFragment } = renderComponent({ displayStatus: BeaconDisplayStatus.Loading });
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-04-13 16:44:15 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders stopped state", () => {
|
2022-12-29 00:24:46 +08:00
|
|
|
const { asFragment } = renderComponent({ displayStatus: BeaconDisplayStatus.Stopped });
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-04-13 16:44:15 +08:00
|
|
|
});
|
|
|
|
|
2022-04-20 19:57:50 +08:00
|
|
|
it("renders without icon", () => {
|
2022-12-29 00:24:46 +08:00
|
|
|
const iconClassName = "mx_StyledLiveBeaconIcon";
|
|
|
|
const { container } = renderComponent({ withIcon: false, displayStatus: BeaconDisplayStatus.Stopped });
|
|
|
|
expect(container.getElementsByClassName(iconClassName)).toHaveLength(0);
|
2022-04-20 19:57:50 +08:00
|
|
|
});
|
|
|
|
|
2022-04-14 20:41:28 +08:00
|
|
|
describe("active state", () => {
|
|
|
|
it("renders without children", () => {
|
|
|
|
// mock for stable snapshot
|
|
|
|
jest.spyOn(Date, "now").mockReturnValue(123456789);
|
2022-08-16 16:23:25 +08:00
|
|
|
const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:server", { isLive: false }, "$1"));
|
2022-12-29 00:24:46 +08:00
|
|
|
const { asFragment } = renderComponent({ beacon, displayStatus: BeaconDisplayStatus.Active });
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-04-14 20:41:28 +08:00
|
|
|
});
|
2022-04-13 16:44:15 +08:00
|
|
|
|
2022-04-14 20:41:28 +08:00
|
|
|
it("renders with children", () => {
|
2022-08-16 16:23:25 +08:00
|
|
|
const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:sever", { isLive: false }));
|
2022-12-29 00:24:46 +08:00
|
|
|
renderComponent({
|
2022-04-14 20:41:28 +08:00
|
|
|
beacon,
|
2022-12-29 00:24:46 +08:00
|
|
|
children: <span data-testid="test-child">test</span>,
|
2022-04-14 20:41:28 +08:00
|
|
|
displayStatus: BeaconDisplayStatus.Active,
|
|
|
|
});
|
2022-12-29 00:24:46 +08:00
|
|
|
expect(screen.getByTestId("test-child")).toMatchSnapshot();
|
2022-04-13 16:44:15 +08:00
|
|
|
});
|
|
|
|
|
2022-04-14 20:41:28 +08:00
|
|
|
it("renders static remaining time when displayLiveTimeRemaining is falsy", () => {
|
|
|
|
// mock for stable snapshot
|
|
|
|
jest.spyOn(Date, "now").mockReturnValue(123456789);
|
2022-08-16 16:23:25 +08:00
|
|
|
const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:server", { isLive: false }, "$1"));
|
2022-12-29 00:24:46 +08:00
|
|
|
renderComponent({ beacon, displayStatus: BeaconDisplayStatus.Active });
|
|
|
|
expect(screen.getByText("Live until 11:17")).toBeInTheDocument();
|
2022-04-13 16:44:15 +08:00
|
|
|
});
|
|
|
|
|
2022-04-14 20:41:28 +08:00
|
|
|
it("renders live time remaining when displayLiveTimeRemaining is truthy", () => {
|
|
|
|
// mock for stable snapshot
|
|
|
|
jest.spyOn(Date, "now").mockReturnValue(123456789);
|
2022-08-16 16:23:25 +08:00
|
|
|
const beacon = new Beacon(makeBeaconInfoEvent("@user:server", "!room:server", { isLive: false }, "$1"));
|
2022-12-29 00:24:46 +08:00
|
|
|
renderComponent({
|
2022-04-14 20:41:28 +08:00
|
|
|
beacon,
|
|
|
|
displayStatus: BeaconDisplayStatus.Active,
|
|
|
|
displayLiveTimeRemaining: true,
|
|
|
|
});
|
2022-12-29 00:24:46 +08:00
|
|
|
expect(screen.getByText("1h left")).toBeInTheDocument();
|
2022-04-14 20:41:28 +08:00
|
|
|
});
|
2022-04-13 16:44:15 +08:00
|
|
|
});
|
|
|
|
});
|