2022-04-14 15:41:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import React from "react";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { Beacon } from "matrix-js-sdk/src/matrix";
|
2023-02-10 17:16:08 +03:00
|
|
|
import { render, screen } from "@testing-library/react";
|
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-04-14 15:41:28 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import OwnBeaconStatus from "../../../../src/components/views/beacon/OwnBeaconStatus";
|
|
|
|
import { BeaconDisplayStatus } from "../../../../src/components/views/beacon/displayStatus";
|
|
|
|
import { useOwnLiveBeacons } from "../../../../src/utils/beacon";
|
2023-02-10 17:16:08 +03:00
|
|
|
import { makeBeaconInfoEvent } from "../../../test-utils";
|
2022-04-14 15:41:28 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.mock("../../../../src/utils/beacon/useOwnLiveBeacons", () => ({
|
2022-04-14 15:41:28 +03:00
|
|
|
useOwnLiveBeacons: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2023-03-02 12:58:05 +03:00
|
|
|
const defaultLiveBeaconsState = {
|
|
|
|
onStopSharing: jest.fn(),
|
|
|
|
onResetLocationPublishError: jest.fn(),
|
|
|
|
stoppingInProgress: false,
|
|
|
|
hasStopSharingError: false,
|
|
|
|
hasLocationPublishError: false,
|
|
|
|
};
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("<OwnBeaconStatus />", () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const defaultProps = {
|
|
|
|
displayStatus: BeaconDisplayStatus.Loading,
|
|
|
|
};
|
2022-12-12 14:24:14 +03:00
|
|
|
const userId = "@user:server";
|
|
|
|
const roomId = "!room:server";
|
2023-02-10 17:16:08 +03:00
|
|
|
let defaultBeacon: Beacon;
|
|
|
|
const renderComponent = (props: Partial<React.ComponentProps<typeof OwnBeaconStatus>> = {}) =>
|
|
|
|
render(<OwnBeaconStatus {...defaultProps} {...props} />);
|
|
|
|
const getRetryButton = () => screen.getByRole("button", { name: "Retry" });
|
|
|
|
const getStopButton = () => screen.getByRole("button", { name: "Stop" });
|
2022-04-14 15:41:28 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(123456789);
|
2023-03-02 12:58:05 +03:00
|
|
|
mocked(useOwnLiveBeacons).mockClear().mockReturnValue(defaultLiveBeaconsState);
|
2022-04-14 15:41:28 +03:00
|
|
|
|
|
|
|
defaultBeacon = new Beacon(makeBeaconInfoEvent(userId, roomId));
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("renders without a beacon instance", () => {
|
2023-02-10 17:16:08 +03:00
|
|
|
const { asFragment } = renderComponent();
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("Active state", () => {
|
|
|
|
it("renders stop button", () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const displayStatus = BeaconDisplayStatus.Active;
|
|
|
|
mocked(useOwnLiveBeacons).mockReturnValue({
|
2023-03-02 12:58:05 +03:00
|
|
|
...defaultLiveBeaconsState,
|
2022-04-14 15:41:28 +03:00
|
|
|
onStopSharing: jest.fn(),
|
|
|
|
});
|
2023-02-10 17:16:08 +03:00
|
|
|
renderComponent({ displayStatus, beacon: defaultBeacon });
|
|
|
|
expect(screen.getByText("Live location enabled")).toBeInTheDocument();
|
|
|
|
expect(getStopButton()).toBeInTheDocument();
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
|
|
|
|
2023-02-10 17:16:08 +03:00
|
|
|
it("stops sharing on stop button click", async () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const displayStatus = BeaconDisplayStatus.Active;
|
|
|
|
const onStopSharing = jest.fn();
|
|
|
|
mocked(useOwnLiveBeacons).mockReturnValue({
|
2023-03-02 12:58:05 +03:00
|
|
|
...defaultLiveBeaconsState,
|
2022-04-14 15:41:28 +03:00
|
|
|
onStopSharing,
|
|
|
|
});
|
2023-02-10 17:16:08 +03:00
|
|
|
renderComponent({ displayStatus, beacon: defaultBeacon });
|
|
|
|
await userEvent.click(getStopButton());
|
2022-04-14 15:41:28 +03:00
|
|
|
expect(onStopSharing).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("errors", () => {
|
|
|
|
it("renders in error mode when displayStatus is error", () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const displayStatus = BeaconDisplayStatus.Error;
|
2023-02-10 17:16:08 +03:00
|
|
|
renderComponent({ displayStatus });
|
|
|
|
expect(screen.getByText("Live location error")).toBeInTheDocument();
|
2022-04-14 15:41:28 +03:00
|
|
|
|
|
|
|
// no actions for plain error
|
2023-02-10 17:16:08 +03:00
|
|
|
expect(screen.queryByRole("button")).not.toBeInTheDocument();
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("with location publish error", () => {
|
|
|
|
it("renders in error mode", () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const displayStatus = BeaconDisplayStatus.Active;
|
|
|
|
mocked(useOwnLiveBeacons).mockReturnValue({
|
2023-03-02 12:58:05 +03:00
|
|
|
...defaultLiveBeaconsState,
|
2022-04-25 15:44:18 +03:00
|
|
|
hasLocationPublishError: true,
|
|
|
|
onResetLocationPublishError: jest.fn(),
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
2023-02-10 17:16:08 +03:00
|
|
|
renderComponent({ displayStatus, beacon: defaultBeacon });
|
|
|
|
expect(screen.getByText("Live location error")).toBeInTheDocument();
|
2022-04-14 15:41:28 +03:00
|
|
|
// retry button
|
2023-02-10 17:16:08 +03:00
|
|
|
expect(getRetryButton()).toBeInTheDocument();
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
|
|
|
|
2023-02-10 17:16:08 +03:00
|
|
|
it("retry button resets location publish error", async () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const displayStatus = BeaconDisplayStatus.Active;
|
2022-04-25 15:44:18 +03:00
|
|
|
const onResetLocationPublishError = jest.fn();
|
2022-04-14 15:41:28 +03:00
|
|
|
mocked(useOwnLiveBeacons).mockReturnValue({
|
2023-03-02 12:58:05 +03:00
|
|
|
...defaultLiveBeaconsState,
|
2022-04-25 15:44:18 +03:00
|
|
|
hasLocationPublishError: true,
|
|
|
|
onResetLocationPublishError,
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
2023-02-10 17:16:08 +03:00
|
|
|
renderComponent({ displayStatus, beacon: defaultBeacon });
|
|
|
|
await userEvent.click(getRetryButton());
|
2022-04-14 15:41:28 +03:00
|
|
|
|
2022-04-25 15:44:18 +03:00
|
|
|
expect(onResetLocationPublishError).toHaveBeenCalled();
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("with stopping error", () => {
|
|
|
|
it("renders in error mode", () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const displayStatus = BeaconDisplayStatus.Active;
|
|
|
|
mocked(useOwnLiveBeacons).mockReturnValue({
|
2023-03-02 12:58:05 +03:00
|
|
|
...defaultLiveBeaconsState,
|
2022-04-25 15:44:18 +03:00
|
|
|
hasLocationPublishError: false,
|
2022-04-14 15:41:28 +03:00
|
|
|
hasStopSharingError: true,
|
|
|
|
onStopSharing: jest.fn(),
|
|
|
|
});
|
2023-02-10 17:16:08 +03:00
|
|
|
renderComponent({ displayStatus, beacon: defaultBeacon });
|
|
|
|
expect(screen.getByText("Live location error")).toBeInTheDocument();
|
2022-04-14 15:41:28 +03:00
|
|
|
// retry button
|
2023-02-10 17:16:08 +03:00
|
|
|
expect(getRetryButton()).toBeInTheDocument();
|
2022-04-14 15:41:28 +03:00
|
|
|
});
|
|
|
|
|
2023-02-10 17:16:08 +03:00
|
|
|
it("retry button retries stop sharing", async () => {
|
2022-04-14 15:41:28 +03:00
|
|
|
const displayStatus = BeaconDisplayStatus.Active;
|
|
|
|
const onStopSharing = jest.fn();
|
|
|
|
mocked(useOwnLiveBeacons).mockReturnValue({
|
2023-03-02 12:58:05 +03:00
|
|
|
...defaultLiveBeaconsState,
|
2022-04-14 15:41:28 +03:00
|
|
|
hasStopSharingError: true,
|
|
|
|
onStopSharing,
|
|
|
|
});
|
2023-02-10 17:16:08 +03:00
|
|
|
renderComponent({ displayStatus, beacon: defaultBeacon });
|
|
|
|
await userEvent.click(getRetryButton());
|
2022-04-14 15:41:28 +03:00
|
|
|
|
|
|
|
expect(onStopSharing).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("renders loading state correctly", () => {
|
2023-02-10 17:16:08 +03:00
|
|
|
const component = renderComponent();
|
2022-04-14 15:41:28 +03:00
|
|
|
expect(component).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|