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