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