2022-03-01 17:00:07 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2022-06-03 08:28:19 +00:00
|
|
|
import { mocked } from "jest-mock";
|
2023-08-04 11:22:08 +00:00
|
|
|
import { RoomMember, RelationType } from "matrix-js-sdk/src/matrix";
|
2022-03-02 13:00:40 +00:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2022-03-18 09:52:24 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-06-03 08:28:19 +00:00
|
|
|
import { M_ASSET, LocationAssetType } from "matrix-js-sdk/src/@types/location";
|
2023-02-22 10:52:55 +00:00
|
|
|
import { act, fireEvent, render, RenderResult } from "@testing-library/react";
|
2023-02-20 18:35:39 +00:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2022-03-01 17:00:07 +00:00
|
|
|
import LocationShareMenu from "../../../../src/components/views/location/LocationShareMenu";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { ChevronFace } from "../../../../src/components/structures/ContextMenu";
|
2022-03-02 13:00:40 +00:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-03-09 17:14:07 +00:00
|
|
|
import { LocationShareType } from "../../../../src/components/views/location/shareLocation";
|
2022-04-22 12:05:36 +00:00
|
|
|
import {
|
2022-04-28 11:37:20 +00:00
|
|
|
flushPromisesWithFakeTimers,
|
2022-04-22 12:05:36 +00:00
|
|
|
getMockClientWithEventEmitter,
|
2023-02-28 08:55:59 +00:00
|
|
|
mockClientMethodsUser,
|
2022-04-22 12:05:36 +00:00
|
|
|
setupAsyncStoreWithClient,
|
|
|
|
} from "../../../test-utils";
|
2022-03-18 09:52:24 +00:00
|
|
|
import Modal from "../../../../src/Modal";
|
2022-03-21 11:42:58 +00:00
|
|
|
import { DEFAULT_DURATION_MS } from "../../../../src/components/views/location/LiveDurationDropdown";
|
2022-04-22 12:05:36 +00:00
|
|
|
import { OwnBeaconStore } from "../../../../src/stores/OwnBeaconStore";
|
2022-04-28 11:37:20 +00:00
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
2022-07-13 10:55:08 +00:00
|
|
|
import QuestionDialog from "../../../../src/components/views/dialogs/QuestionDialog";
|
2022-04-28 11:37:20 +00:00
|
|
|
|
|
|
|
jest.useFakeTimers();
|
2022-03-02 13:00:40 +00:00
|
|
|
|
2022-03-25 14:36:22 +00:00
|
|
|
jest.mock("../../../../src/utils/location/findMapStyleUrl", () => ({
|
2022-03-02 13:00:40 +00:00
|
|
|
findMapStyleUrl: jest.fn().mockReturnValue("test"),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../../../src/settings/SettingsStore", () => ({
|
|
|
|
getValue: jest.fn(),
|
2022-04-28 11:37:20 +00:00
|
|
|
setValue: jest.fn(),
|
2022-03-02 13:00:40 +00:00
|
|
|
monitorSetting: jest.fn(),
|
2022-04-28 11:37:20 +00:00
|
|
|
watchSetting: jest.fn(),
|
|
|
|
unwatchSetting: jest.fn(),
|
2022-03-02 13:00:40 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../../../src/stores/OwnProfileStore", () => ({
|
|
|
|
OwnProfileStore: {
|
|
|
|
instance: {
|
|
|
|
displayName: "Ernie",
|
|
|
|
getHttpAvatarUrl: jest.fn().mockReturnValue("image.com/img"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
2022-03-01 17:00:07 +00:00
|
|
|
|
2022-03-18 09:52:24 +00:00
|
|
|
jest.mock("../../../../src/Modal", () => ({
|
2022-06-14 16:51:51 +00:00
|
|
|
createDialog: jest.fn(),
|
2022-11-09 15:33:09 +00:00
|
|
|
on: jest.fn(),
|
|
|
|
off: jest.fn(),
|
|
|
|
ModalManagerEvent: { Opened: "opened" },
|
2022-03-18 09:52:24 +00:00
|
|
|
}));
|
|
|
|
|
2023-04-21 09:48:48 +00:00
|
|
|
// Fake random strings to give a predictable snapshot for IDs
|
|
|
|
jest.mock("matrix-js-sdk/src/randomstring", () => ({
|
|
|
|
randomString: () => "abdefghi",
|
|
|
|
}));
|
|
|
|
|
2022-03-01 17:00:07 +00:00
|
|
|
describe("<LocationShareMenu />", () => {
|
2022-03-02 13:00:40 +00:00
|
|
|
const userId = "@ernie:server.org";
|
2022-04-22 12:05:36 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2023-02-28 08:55:59 +00:00
|
|
|
...mockClientMethodsUser(userId),
|
2022-03-02 13:00:40 +00:00
|
|
|
getClientWellKnown: jest.fn().mockResolvedValue({
|
|
|
|
map_style_url: "maps.com",
|
|
|
|
}),
|
2022-03-09 17:14:07 +00:00
|
|
|
sendMessage: jest.fn(),
|
2022-04-22 12:05:36 +00:00
|
|
|
unstable_createLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
|
2022-06-30 07:33:51 +00:00
|
|
|
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
|
2022-04-22 12:05:36 +00:00
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
|
|
|
});
|
2022-03-01 17:00:07 +00:00
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
menuPosition: {
|
|
|
|
top: 1,
|
|
|
|
left: 1,
|
|
|
|
chevronFace: ChevronFace.Bottom,
|
|
|
|
},
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
openMenu: jest.fn(),
|
|
|
|
roomId: "!room:server.org",
|
2022-03-02 13:00:40 +00:00
|
|
|
sender: new RoomMember("!room:server.org", userId),
|
2022-03-01 17:00:07 +00:00
|
|
|
};
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const mockGeolocate = new maplibregl.GeolocateControl({});
|
|
|
|
jest.spyOn(mockGeolocate, "on");
|
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
|
|
|
jest.spyOn(mockMap, "on");
|
|
|
|
|
2022-03-09 17:14:07 +00:00
|
|
|
const position = {
|
|
|
|
coords: {
|
|
|
|
latitude: -36.24484561954707,
|
|
|
|
longitude: 175.46884959563613,
|
|
|
|
accuracy: 10,
|
|
|
|
},
|
|
|
|
timestamp: 1646305006802,
|
|
|
|
type: "geolocate",
|
|
|
|
};
|
|
|
|
|
2022-04-22 12:05:36 +00:00
|
|
|
const makeOwnBeaconStore = async () => {
|
|
|
|
const store = OwnBeaconStore.instance;
|
|
|
|
|
|
|
|
await setupAsyncStoreWithClient(store, mockClient);
|
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const getComponent = (props = {}): RenderResult =>
|
|
|
|
render(<LocationShareMenu {...defaultProps} {...props} />, {
|
|
|
|
wrapper: ({ children }) => (
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>{children}</MatrixClientContext.Provider>
|
|
|
|
),
|
2022-03-01 17:00:07 +00:00
|
|
|
});
|
|
|
|
|
2022-04-22 12:05:36 +00:00
|
|
|
beforeEach(async () => {
|
2022-03-18 09:52:24 +00:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-03-15 15:04:52 +00:00
|
|
|
mocked(SettingsStore).getValue.mockReturnValue(false);
|
2022-03-09 17:14:07 +00:00
|
|
|
mockClient.sendMessage.mockClear();
|
2022-04-22 12:05:36 +00:00
|
|
|
mockClient.unstable_createLiveBeacon.mockClear().mockResolvedValue({ event_id: "1" });
|
2022-03-02 13:00:40 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient as unknown as MatrixClient);
|
2022-06-14 16:51:51 +00:00
|
|
|
mocked(Modal).createDialog.mockClear();
|
2022-04-22 12:05:36 +00:00
|
|
|
|
2022-04-28 11:37:20 +00:00
|
|
|
jest.clearAllMocks();
|
|
|
|
|
2022-04-22 12:05:36 +00:00
|
|
|
await makeOwnBeaconStore();
|
2022-03-02 13:00:40 +00:00
|
|
|
});
|
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const getBackButton = (getByLabelText: RenderResult["getByLabelText"]) => getByLabelText("Back");
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const getCancelButton = (getByLabelText: RenderResult["getByLabelText"]) => getByLabelText("Close");
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const setLocationGeolocate = () => {
|
|
|
|
// get the callback LocationShareMenu registered for geolocate
|
|
|
|
expect(mocked(mockGeolocate.on)).toHaveBeenCalledWith("geolocate", expect.any(Function));
|
2023-02-24 15:28:40 +00:00
|
|
|
const [, onGeolocateCallback] = mocked(mockGeolocate.on).mock.calls.find(([event]) => event === "geolocate")!;
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
// set the location
|
|
|
|
onGeolocateCallback(position);
|
|
|
|
};
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const setLocationClick = () => {
|
|
|
|
// get the callback LocationShareMenu registered for geolocate
|
|
|
|
expect(mocked(mockMap.on)).toHaveBeenCalledWith("click", expect.any(Function));
|
2023-02-24 15:28:40 +00:00
|
|
|
const [, onMapClickCallback] = mocked(mockMap.on).mock.calls.find(([event]) => event === "click")!;
|
2023-02-20 18:35:39 +00:00
|
|
|
|
|
|
|
const event = {
|
|
|
|
lngLat: { lng: position.coords.longitude, lat: position.coords.latitude },
|
|
|
|
} as unknown as maplibregl.MapMouseEvent;
|
2022-03-09 17:14:07 +00:00
|
|
|
// set the location
|
2023-02-20 18:35:39 +00:00
|
|
|
onMapClickCallback(event);
|
2022-03-09 17:14:07 +00:00
|
|
|
};
|
2022-03-02 13:00:40 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const shareTypeLabels: Record<LocationShareType, string> = {
|
|
|
|
[LocationShareType.Own]: "My current location",
|
|
|
|
[LocationShareType.Live]: "My live location",
|
|
|
|
[LocationShareType.Pin]: "Drop a Pin",
|
|
|
|
};
|
|
|
|
const setShareType = (getByText: RenderResult["getByText"], shareType: LocationShareType) => {
|
|
|
|
fireEvent.click(getByText(shareTypeLabels[shareType]));
|
|
|
|
};
|
2022-03-02 13:00:40 +00:00
|
|
|
|
2022-03-15 15:04:52 +00:00
|
|
|
describe("when only Own share type is enabled", () => {
|
|
|
|
beforeEach(() => enableSettings([]));
|
|
|
|
|
2022-04-28 11:37:20 +00:00
|
|
|
it("renders own and live location options", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
|
|
|
expect(getByText(shareTypeLabels[LocationShareType.Own])).toBeInTheDocument();
|
|
|
|
expect(getByText(shareTypeLabels[LocationShareType.Live])).toBeInTheDocument();
|
2022-03-03 10:30:46 +00:00
|
|
|
});
|
|
|
|
|
2022-04-28 11:37:20 +00:00
|
|
|
it("renders back button from location picker screen", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText, getByLabelText } = getComponent();
|
|
|
|
setShareType(getByText, LocationShareType.Own);
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getBackButton(getByLabelText)).toBeInTheDocument();
|
2022-03-03 10:30:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("clicking cancel button from location picker closes dialog", () => {
|
|
|
|
const onFinished = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByLabelText } = getComponent({ onFinished });
|
2022-03-02 13:00:40 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getCancelButton(getByLabelText));
|
2022-03-03 10:30:46 +00:00
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
it("creates static own location share event on submission", () => {
|
|
|
|
const onFinished = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent({ onFinished });
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Own);
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setLocationGeolocate();
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByText("Share location"));
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
const [messageRoomId, relation, messageBody] = mockClient.sendMessage.mock.calls[0];
|
|
|
|
expect(messageRoomId).toEqual(defaultProps.roomId);
|
|
|
|
expect(relation).toEqual(null);
|
|
|
|
expect(messageBody).toEqual(
|
|
|
|
expect.objectContaining({
|
2022-03-10 18:03:31 +00:00
|
|
|
[M_ASSET.name]: {
|
2022-03-09 17:14:07 +00:00
|
|
|
type: LocationAssetType.Self,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2022-03-02 13:00:40 +00:00
|
|
|
});
|
|
|
|
|
2022-03-03 10:30:46 +00:00
|
|
|
describe("with pin drop share type enabled", () => {
|
|
|
|
it("renders share type switch with own and pin drop options", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
|
|
|
expect(document.querySelector(".mx_LocationPicker")).not.toBeInTheDocument();
|
2022-03-03 10:30:46 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByText(shareTypeLabels[LocationShareType.Own])).toBeInTheDocument();
|
|
|
|
expect(getByText(shareTypeLabels[LocationShareType.Pin])).toBeInTheDocument();
|
2022-03-02 13:00:40 +00:00
|
|
|
});
|
|
|
|
|
2022-03-03 10:30:46 +00:00
|
|
|
it("does not render back button on share type screen", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { queryByLabelText } = getComponent();
|
|
|
|
expect(queryByLabelText("Back")).not.toBeInTheDocument();
|
2022-03-03 10:30:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("clicking cancel button from share type screen closes dialog", () => {
|
|
|
|
const onFinished = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByLabelText } = getComponent({ onFinished });
|
2022-03-03 10:30:46 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getCancelButton(getByLabelText));
|
2022-03-02 13:00:40 +00:00
|
|
|
|
2022-03-03 10:30:46 +00:00
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("selecting own location share type advances to location picker", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
2022-03-03 10:30:46 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Own);
|
2022-03-03 10:30:46 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(document.querySelector(".mx_LocationPicker")).toBeInTheDocument();
|
2022-03-03 10:30:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("clicking back button from location picker screen goes back to share screen", () => {
|
|
|
|
const onFinished = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText, getByLabelText } = getComponent({ onFinished });
|
2022-03-03 10:30:46 +00:00
|
|
|
|
|
|
|
// advance to location picker
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Own);
|
2022-03-03 10:30:46 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(document.querySelector(".mx_LocationPicker")).toBeInTheDocument();
|
2022-03-03 10:30:46 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getBackButton(getByLabelText));
|
2022-03-03 10:30:46 +00:00
|
|
|
|
|
|
|
// back to share type
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByText("What location type do you want to share?")).toBeInTheDocument();
|
2022-03-03 10:30:46 +00:00
|
|
|
});
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
it("creates pin drop location share event on submission", () => {
|
|
|
|
const onFinished = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent({ onFinished });
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
// advance to location picker
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Pin);
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setLocationClick();
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByText("Share location"));
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
const [messageRoomId, relation, messageBody] = mockClient.sendMessage.mock.calls[0];
|
|
|
|
expect(messageRoomId).toEqual(defaultProps.roomId);
|
|
|
|
expect(relation).toEqual(null);
|
|
|
|
expect(messageBody).toEqual(
|
|
|
|
expect.objectContaining({
|
2022-03-10 18:03:31 +00:00
|
|
|
[M_ASSET.name]: {
|
2022-03-09 17:14:07 +00:00
|
|
|
type: LocationAssetType.Pin,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2022-03-01 17:00:07 +00:00
|
|
|
});
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2022-04-28 11:37:20 +00:00
|
|
|
describe("with live location disabled", () => {
|
|
|
|
beforeEach(() => enableSettings([]));
|
|
|
|
|
|
|
|
it("goes to labs flag screen after live options is clicked", () => {
|
|
|
|
const onFinished = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText, getByTestId } = getComponent({ onFinished });
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByTestId("location-picker-enable-live-share")).toMatchSnapshot();
|
2022-04-28 11:37:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("disables OK button when labs flag is not enabled", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByText("OK").hasAttribute("disabled")).toBeTruthy();
|
2022-04-28 11:37:20 +00:00
|
|
|
});
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2022-04-28 11:37:20 +00:00
|
|
|
it("enables OK button when labs flag is toggled to enabled", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText, getByLabelText } = getComponent();
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
2022-03-15 15:04:52 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByLabelText("Enable live location sharing"));
|
|
|
|
|
|
|
|
expect(getByText("OK").hasAttribute("disabled")).toBeFalsy();
|
2022-04-28 11:37:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("enables live share setting on ok button submit", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText, getByLabelText } = getComponent();
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByLabelText("Enable live location sharing"));
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByText("OK"));
|
2022-04-28 11:37:20 +00:00
|
|
|
|
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith(
|
|
|
|
"feature_location_share_live",
|
2023-02-28 08:55:59 +00:00
|
|
|
null,
|
2022-04-28 11:37:20 +00:00
|
|
|
SettingLevel.DEVICE,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("navigates to location picker when live share is enabled in settings store", () => {
|
|
|
|
// @ts-ignore
|
|
|
|
mocked(SettingsStore.watchSetting).mockImplementation((featureName, roomId, callback) => {
|
|
|
|
callback(featureName, roomId, SettingLevel.DEVICE, "", "");
|
2022-11-30 11:32:56 +00:00
|
|
|
window.setTimeout(() => {
|
2022-04-28 11:37:20 +00:00
|
|
|
callback(featureName, roomId, SettingLevel.DEVICE, "", "");
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
mocked(SettingsStore.getValue).mockReturnValue(false);
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText, getByLabelText } = getComponent();
|
2022-04-28 11:37:20 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
2022-04-28 11:37:20 +00:00
|
|
|
|
|
|
|
// we're on enable live share screen
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByLabelText("Enable live location sharing")).toBeInTheDocument();
|
2022-04-28 11:37:20 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
mocked(SettingsStore.getValue).mockReturnValue(true);
|
|
|
|
// advance so watchSetting will update the value
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
// advanced to location picker
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(document.querySelector(".mx_LocationPicker")).toBeInTheDocument();
|
2022-03-15 15:04:52 +00:00
|
|
|
});
|
|
|
|
});
|
2022-03-18 09:52:24 +00:00
|
|
|
|
|
|
|
describe("Live location share", () => {
|
|
|
|
beforeEach(() => enableSettings(["feature_location_share_live"]));
|
|
|
|
|
2022-06-03 08:28:19 +00:00
|
|
|
it("does not display live location share option when composer has a relation", () => {
|
|
|
|
const relation = {
|
|
|
|
rel_type: RelationType.Thread,
|
|
|
|
event_id: "12345",
|
|
|
|
};
|
2023-02-20 18:35:39 +00:00
|
|
|
const { queryByText } = getComponent({ relation });
|
2022-06-03 08:28:19 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(queryByText(shareTypeLabels[LocationShareType.Live])).not.toBeInTheDocument();
|
2022-06-03 08:28:19 +00:00
|
|
|
});
|
|
|
|
|
2022-06-30 07:33:51 +00:00
|
|
|
it("creates beacon info event on submission", async () => {
|
2022-03-18 09:52:24 +00:00
|
|
|
const onFinished = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent({ onFinished });
|
2022-03-18 09:52:24 +00:00
|
|
|
|
|
|
|
// advance to location picker
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
|
|
|
setLocationGeolocate();
|
2022-03-18 09:52:24 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByText("Share location"));
|
2022-03-18 09:52:24 +00:00
|
|
|
|
2022-06-30 07:33:51 +00:00
|
|
|
// flush stopping existing beacons promises
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2022-03-18 09:52:24 +00:00
|
|
|
expect(onFinished).toHaveBeenCalled();
|
2022-04-08 08:53:06 +00:00
|
|
|
const [eventRoomId, eventContent] = mockClient.unstable_createLiveBeacon.mock.calls[0];
|
2022-03-18 09:52:24 +00:00
|
|
|
expect(eventRoomId).toEqual(defaultProps.roomId);
|
|
|
|
expect(eventContent).toEqual(
|
|
|
|
expect.objectContaining({
|
2022-04-08 08:53:06 +00:00
|
|
|
// default timeout
|
|
|
|
timeout: DEFAULT_DURATION_MS,
|
|
|
|
description: `Ernie's live location`,
|
|
|
|
live: true,
|
2022-03-18 09:52:24 +00:00
|
|
|
[M_ASSET.name]: {
|
|
|
|
type: LocationAssetType.Self,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-07-13 10:55:08 +00:00
|
|
|
it("opens error dialog when beacon creation fails", async () => {
|
2022-03-18 09:52:24 +00:00
|
|
|
// stub logger to keep console clean from expected error
|
|
|
|
const logSpy = jest.spyOn(logger, "error").mockReturnValue(undefined);
|
|
|
|
const error = new Error("oh no");
|
|
|
|
mockClient.unstable_createLiveBeacon.mockRejectedValue(error);
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
2022-03-18 09:52:24 +00:00
|
|
|
|
|
|
|
// advance to location picker
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
|
|
|
setLocationGeolocate();
|
2022-03-18 09:52:24 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByText("Share location"));
|
2022-03-18 09:52:24 +00:00
|
|
|
|
2022-06-30 07:33:51 +00:00
|
|
|
await flushPromisesWithFakeTimers();
|
2022-04-28 11:37:20 +00:00
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-03-18 09:52:24 +00:00
|
|
|
|
|
|
|
expect(logSpy).toHaveBeenCalledWith("We couldn't start sharing your live location", error);
|
2022-07-13 10:55:08 +00:00
|
|
|
expect(mocked(Modal).createDialog).toHaveBeenCalledWith(
|
|
|
|
QuestionDialog,
|
|
|
|
expect.objectContaining({
|
|
|
|
button: "Try again",
|
|
|
|
description: "Element could not send your location. Please try again later.",
|
|
|
|
title: `We couldn't send your location`,
|
|
|
|
cancelButton: "Cancel",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("opens error dialog when beacon creation fails with permission error", async () => {
|
|
|
|
// stub logger to keep console clean from expected error
|
|
|
|
const logSpy = jest.spyOn(logger, "error").mockReturnValue(undefined);
|
|
|
|
const error = { errcode: "M_FORBIDDEN" } as unknown as Error;
|
|
|
|
mockClient.unstable_createLiveBeacon.mockRejectedValue(error);
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
2022-07-13 10:55:08 +00:00
|
|
|
|
|
|
|
// advance to location picker
|
2023-02-20 18:35:39 +00:00
|
|
|
setShareType(getByText, LocationShareType.Live);
|
|
|
|
setLocationGeolocate();
|
2022-07-13 10:55:08 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByText("Share location"));
|
2022-07-13 10:55:08 +00:00
|
|
|
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(logSpy).toHaveBeenCalledWith("Insufficient permissions to start sharing your live location", error);
|
|
|
|
expect(mocked(Modal).createDialog).toHaveBeenCalledWith(
|
|
|
|
QuestionDialog,
|
|
|
|
expect.objectContaining({
|
|
|
|
button: "OK",
|
|
|
|
description: "You need to have the right permissions in order to share locations in this room.",
|
|
|
|
title: `You don't have permission to share locations`,
|
|
|
|
hasCancelButton: false,
|
|
|
|
}),
|
|
|
|
);
|
2022-03-18 09:52:24 +00:00
|
|
|
});
|
|
|
|
});
|
2022-03-01 17:00:07 +00:00
|
|
|
});
|
2022-03-15 15:04:52 +00:00
|
|
|
|
|
|
|
function enableSettings(settings: string[]) {
|
|
|
|
mocked(SettingsStore).getValue.mockReturnValue(false);
|
2022-11-04 10:48:08 +00:00
|
|
|
mocked(SettingsStore).getValue.mockImplementation((settingName: string): any => settings.includes(settingName));
|
2022-03-15 15:04:52 +00:00
|
|
|
}
|