2022-03-01 18:00:07 +01: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 12:24:14 +01:00
|
|
|
import React from "react";
|
2022-08-02 15:10:43 +02:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-12-12 12:24:14 +01:00
|
|
|
import { mount, ReactWrapper } from "enzyme";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { RelationType } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
import { M_ASSET, LocationAssetType } from "matrix-js-sdk/src/@types/location";
|
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
|
|
|
|
import LocationShareMenu from "../../../../src/components/views/location/LocationShareMenu";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { ChevronFace } from "../../../../src/components/structures/ContextMenu";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import { LocationShareType } from "../../../../src/components/views/location/shareLocation";
|
2022-04-22 14:05:36 +02:00
|
|
|
import {
|
|
|
|
findByTagAndTestId,
|
2022-04-28 13:37:20 +02:00
|
|
|
findByTestId,
|
|
|
|
flushPromisesWithFakeTimers,
|
2022-04-22 14:05:36 +02:00
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
setupAsyncStoreWithClient,
|
2022-12-12 12:24:14 +01:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import Modal from "../../../../src/Modal";
|
|
|
|
import { DEFAULT_DURATION_MS } from "../../../../src/components/views/location/LiveDurationDropdown";
|
|
|
|
import { OwnBeaconStore } from "../../../../src/stores/OwnBeaconStore";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
|
|
|
import QuestionDialog from "../../../../src/components/views/dialogs/QuestionDialog";
|
2022-04-28 13:37:20 +02:00
|
|
|
|
|
|
|
jest.useFakeTimers();
|
2022-03-02 14:00:40 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/utils/location/findMapStyleUrl", () => ({
|
|
|
|
findMapStyleUrl: jest.fn().mockReturnValue("test"),
|
2022-03-02 14:00:40 +01:00
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/settings/SettingsStore", () => ({
|
2022-03-02 14:00:40 +01:00
|
|
|
getValue: jest.fn(),
|
2022-04-28 13:37:20 +02:00
|
|
|
setValue: jest.fn(),
|
2022-03-02 14:00:40 +01:00
|
|
|
monitorSetting: jest.fn(),
|
2022-04-28 13:37:20 +02:00
|
|
|
watchSetting: jest.fn(),
|
|
|
|
unwatchSetting: jest.fn(),
|
2022-03-02 14:00:40 +01:00
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/stores/OwnProfileStore", () => ({
|
2022-03-02 14:00:40 +01:00
|
|
|
OwnProfileStore: {
|
|
|
|
instance: {
|
2022-12-12 12:24:14 +01:00
|
|
|
displayName: "Ernie",
|
|
|
|
getHttpAvatarUrl: jest.fn().mockReturnValue("image.com/img"),
|
2022-03-02 14:00:40 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
2022-03-01 18:00:07 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/Modal", () => ({
|
2022-06-14 18:51:51 +02:00
|
|
|
createDialog: jest.fn(),
|
2022-11-09 16:33:09 +01:00
|
|
|
on: jest.fn(),
|
|
|
|
off: jest.fn(),
|
|
|
|
ModalManagerEvent: { Opened: "opened" },
|
2022-03-18 10:52:24 +01:00
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("<LocationShareMenu />", () => {
|
|
|
|
const userId = "@ernie:server.org";
|
2022-04-22 14:05:36 +02:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2022-03-02 14:00:40 +01:00
|
|
|
getUserId: jest.fn().mockReturnValue(userId),
|
|
|
|
getClientWellKnown: jest.fn().mockResolvedValue({
|
2022-12-12 12:24:14 +01:00
|
|
|
map_style_url: "maps.com",
|
2022-03-02 14:00:40 +01:00
|
|
|
}),
|
2022-03-09 18:14:07 +01:00
|
|
|
sendMessage: jest.fn(),
|
2022-12-12 12:24:14 +01:00
|
|
|
unstable_createLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
|
|
|
|
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
|
2022-04-22 14:05:36 +02:00
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
|
|
|
});
|
2022-03-01 18:00:07 +01:00
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
menuPosition: {
|
2022-12-12 12:24:14 +01:00
|
|
|
top: 1,
|
|
|
|
left: 1,
|
2022-03-01 18:00:07 +01:00
|
|
|
chevronFace: ChevronFace.Bottom,
|
|
|
|
},
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
openMenu: jest.fn(),
|
2022-12-12 12:24:14 +01:00
|
|
|
roomId: "!room:server.org",
|
|
|
|
sender: new RoomMember("!room:server.org", userId),
|
2022-03-01 18:00:07 +01:00
|
|
|
};
|
2022-03-09 18:14:07 +01:00
|
|
|
|
|
|
|
const position = {
|
|
|
|
coords: {
|
|
|
|
latitude: -36.24484561954707,
|
|
|
|
longitude: 175.46884959563613,
|
|
|
|
accuracy: 10,
|
|
|
|
},
|
|
|
|
timestamp: 1646305006802,
|
2022-12-12 12:24:14 +01:00
|
|
|
type: "geolocate",
|
2022-03-09 18:14:07 +01:00
|
|
|
};
|
|
|
|
|
2022-04-22 14:05:36 +02:00
|
|
|
const makeOwnBeaconStore = async () => {
|
|
|
|
const store = OwnBeaconStore.instance;
|
|
|
|
|
|
|
|
await setupAsyncStoreWithClient(store, mockClient);
|
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
2022-03-01 18:00:07 +01:00
|
|
|
const getComponent = (props = {}) =>
|
|
|
|
mount(<LocationShareMenu {...defaultProps} {...props} />, {
|
|
|
|
wrappingComponent: MatrixClientContext.Provider,
|
|
|
|
wrappingComponentProps: { value: mockClient },
|
|
|
|
});
|
|
|
|
|
2022-04-22 14:05:36 +02:00
|
|
|
beforeEach(async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-03-15 16:04:52 +01:00
|
|
|
mocked(SettingsStore).getValue.mockReturnValue(false);
|
2022-03-09 18:14:07 +01:00
|
|
|
mockClient.sendMessage.mockClear();
|
2022-12-12 12:24:14 +01:00
|
|
|
mockClient.unstable_createLiveBeacon.mockClear().mockResolvedValue({ event_id: "1" });
|
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient as unknown as MatrixClient);
|
2022-06-14 18:51:51 +02:00
|
|
|
mocked(Modal).createDialog.mockClear();
|
2022-04-22 14:05:36 +02:00
|
|
|
|
2022-04-28 13:37:20 +02:00
|
|
|
jest.clearAllMocks();
|
|
|
|
|
2022-04-22 14:05:36 +02:00
|
|
|
await makeOwnBeaconStore();
|
2022-03-02 14:00:40 +01:00
|
|
|
});
|
|
|
|
|
2022-03-15 16:04:52 +01:00
|
|
|
const getShareTypeOption = (component: ReactWrapper, shareType: LocationShareType) =>
|
2022-12-12 12:24:14 +01:00
|
|
|
findByTagAndTestId(component, `share-location-option-${shareType}`, "button");
|
2022-03-15 16:04:52 +01:00
|
|
|
|
|
|
|
const getBackButton = (component: ReactWrapper) =>
|
2022-12-12 12:24:14 +01:00
|
|
|
findByTagAndTestId(component, "share-dialog-buttons-back", "button");
|
2022-03-15 16:04:52 +01:00
|
|
|
|
|
|
|
const getCancelButton = (component: ReactWrapper) =>
|
2022-12-12 12:24:14 +01:00
|
|
|
findByTagAndTestId(component, "share-dialog-buttons-cancel", "button");
|
2022-03-15 16:04:52 +01:00
|
|
|
|
|
|
|
const getSubmitButton = (component: ReactWrapper) =>
|
2022-12-12 12:24:14 +01:00
|
|
|
findByTagAndTestId(component, "location-picker-submit-button", "button");
|
2022-03-15 16:04:52 +01:00
|
|
|
|
|
|
|
const setLocation = (component: ReactWrapper) => {
|
2022-03-09 18:14:07 +01:00
|
|
|
// set the location
|
2022-12-12 12:24:14 +01:00
|
|
|
const locationPickerInstance = component.find("LocationPicker").instance();
|
2022-03-09 18:14:07 +01:00
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
locationPickerInstance.onGeolocate(position);
|
|
|
|
// make sure button gets enabled
|
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
};
|
2022-03-02 14:00:40 +01:00
|
|
|
|
2022-03-15 16:04:52 +01:00
|
|
|
const setShareType = (component: ReactWrapper, shareType: LocationShareType) =>
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getShareTypeOption(component, shareType).at(0).simulate("click");
|
2022-03-15 16:04:52 +01:00
|
|
|
component.setProps({});
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
2022-03-02 14:00:40 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("when only Own share type is enabled", () => {
|
2022-03-15 16:04:52 +01:00
|
|
|
beforeEach(() => enableSettings([]));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders own and live location options", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const component = getComponent();
|
2022-04-28 13:37:20 +02:00
|
|
|
expect(getShareTypeOption(component, LocationShareType.Own).length).toBe(1);
|
|
|
|
expect(getShareTypeOption(component, LocationShareType.Live).length).toBe(1);
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders back button from location picker screen", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const component = getComponent();
|
2022-04-28 13:37:20 +02:00
|
|
|
setShareType(component, LocationShareType.Own);
|
|
|
|
|
|
|
|
expect(getBackButton(component).length).toBe(1);
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("clicking cancel button from location picker closes dialog", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
2022-03-02 14:00:40 +01:00
|
|
|
|
2022-03-03 11:30:46 +01:00
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getCancelButton(component).at(0).simulate("click");
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2022-03-09 18:14:07 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("creates static own location share event on submission", () => {
|
2022-03-09 18:14:07 +01:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
|
|
|
|
2022-04-28 13:37:20 +02:00
|
|
|
setShareType(component, LocationShareType.Own);
|
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
setLocation(component);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getSubmitButton(component).at(0).simulate("click");
|
2022-03-09 18:14:07 +01:00
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
const [messageRoomId, relation, messageBody] = mockClient.sendMessage.mock.calls[0];
|
|
|
|
expect(messageRoomId).toEqual(defaultProps.roomId);
|
|
|
|
expect(relation).toEqual(null);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(messageBody).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
[M_ASSET.name]: {
|
|
|
|
type: LocationAssetType.Self,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2022-03-09 18:14:07 +01:00
|
|
|
});
|
2022-03-02 14:00:40 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("with pin drop share type enabled", () => {
|
|
|
|
it("renders share type switch with own and pin drop options", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const component = getComponent();
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(component.find("LocationPicker").length).toBe(0);
|
2022-03-03 11:30:46 +01:00
|
|
|
|
2022-03-16 10:37:09 +01:00
|
|
|
expect(getShareTypeOption(component, LocationShareType.Own).length).toBe(1);
|
|
|
|
expect(getShareTypeOption(component, LocationShareType.Pin).length).toBe(1);
|
2022-03-02 14:00:40 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not render back button on share type screen", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const component = getComponent();
|
2022-03-16 10:37:09 +01:00
|
|
|
expect(getBackButton(component).length).toBe(0);
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("clicking cancel button from share type screen closes dialog", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getCancelButton(component).at(0).simulate("click");
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
2022-03-02 14:00:40 +01:00
|
|
|
|
2022-03-03 11:30:46 +01:00
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("selecting own location share type advances to location picker", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const component = getComponent();
|
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
setShareType(component, LocationShareType.Own);
|
2022-03-03 11:30:46 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(component.find("LocationPicker").length).toBe(1);
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("clicking back button from location picker screen goes back to share screen", () => {
|
2022-03-03 11:30:46 +01:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
|
|
|
|
|
|
|
// advance to location picker
|
2022-03-09 18:14:07 +01:00
|
|
|
setShareType(component, LocationShareType.Own);
|
2022-03-03 11:30:46 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(component.find("LocationPicker").length).toBe(1);
|
2022-03-03 11:30:46 +01:00
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getBackButton(component).at(0).simulate("click");
|
2022-03-03 11:30:46 +01:00
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
|
|
|
// back to share type
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(component.find("ShareType").length).toBe(1);
|
2022-03-03 11:30:46 +01:00
|
|
|
});
|
2022-03-09 18:14:07 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("creates pin drop location share event on submission", () => {
|
2022-03-09 18:14:07 +01:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
|
|
|
|
|
|
|
// advance to location picker
|
|
|
|
setShareType(component, LocationShareType.Pin);
|
|
|
|
|
|
|
|
setLocation(component);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getSubmitButton(component).at(0).simulate("click");
|
2022-03-09 18:14:07 +01:00
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
const [messageRoomId, relation, messageBody] = mockClient.sendMessage.mock.calls[0];
|
|
|
|
expect(messageRoomId).toEqual(defaultProps.roomId);
|
|
|
|
expect(relation).toEqual(null);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(messageBody).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
[M_ASSET.name]: {
|
|
|
|
type: LocationAssetType.Pin,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2022-03-09 18:14:07 +01:00
|
|
|
});
|
2022-03-01 18:00:07 +01:00
|
|
|
});
|
2022-03-15 16:04:52 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("with live location disabled", () => {
|
2022-04-28 13:37:20 +02:00
|
|
|
beforeEach(() => enableSettings([]));
|
|
|
|
|
|
|
|
const getToggle = (component: ReactWrapper) =>
|
2022-12-12 12:24:14 +01:00
|
|
|
findByTestId(component, "enable-live-share-toggle").find('[role="switch"]').at(0);
|
2022-04-28 13:37:20 +02:00
|
|
|
const getSubmitEnableButton = (component: ReactWrapper) =>
|
2022-12-12 12:24:14 +01:00
|
|
|
findByTestId(component, "enable-live-share-submit").at(0);
|
2022-04-28 13:37:20 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("goes to labs flag screen after live options is clicked", () => {
|
2022-04-28 13:37:20 +02:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
2022-03-15 16:04:52 +01:00
|
|
|
|
2022-04-28 13:37:20 +02:00
|
|
|
setShareType(component, LocationShareType.Live);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(findByTestId(component, "location-picker-enable-live-share")).toMatchSnapshot();
|
2022-04-28 13:37:20 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("disables OK button when labs flag is not enabled", () => {
|
2022-03-15 16:04:52 +01:00
|
|
|
const component = getComponent();
|
|
|
|
|
2022-04-28 13:37:20 +02:00
|
|
|
setShareType(component, LocationShareType.Live);
|
2022-03-15 16:04:52 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(getSubmitEnableButton(component).props()["disabled"]).toBeTruthy();
|
2022-04-28 13:37:20 +02:00
|
|
|
});
|
2022-03-15 16:04:52 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("enables OK button when labs flag is toggled to enabled", () => {
|
2022-04-28 13:37:20 +02:00
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
setShareType(component, LocationShareType.Live);
|
2022-03-15 16:04:52 +01:00
|
|
|
|
2022-04-28 13:37:20 +02:00
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getToggle(component).simulate("click");
|
2022-04-28 13:37:20 +02:00
|
|
|
component.setProps({});
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(getSubmitEnableButton(component).props()["disabled"]).toBeFalsy();
|
2022-04-28 13:37:20 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("enables live share setting on ok button submit", () => {
|
2022-04-28 13:37:20 +02:00
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
setShareType(component, LocationShareType.Live);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getToggle(component).simulate("click");
|
2022-04-28 13:37:20 +02:00
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getSubmitEnableButton(component).simulate("click");
|
2022-04-28 13:37:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith(
|
2022-12-12 12:24:14 +01:00
|
|
|
"feature_location_share_live",
|
|
|
|
undefined,
|
|
|
|
SettingLevel.DEVICE,
|
|
|
|
true,
|
2022-04-28 13:37:20 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("navigates to location picker when live share is enabled in settings store", () => {
|
2022-04-28 13:37:20 +02:00
|
|
|
// @ts-ignore
|
|
|
|
mocked(SettingsStore.watchSetting).mockImplementation((featureName, roomId, callback) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
callback(featureName, roomId, SettingLevel.DEVICE, "", "");
|
2022-11-30 12:32:56 +01:00
|
|
|
window.setTimeout(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
callback(featureName, roomId, SettingLevel.DEVICE, "", "");
|
2022-04-28 13:37:20 +02:00
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
mocked(SettingsStore.getValue).mockReturnValue(false);
|
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
setShareType(component, LocationShareType.Live);
|
|
|
|
|
|
|
|
// we're on enable live share screen
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(findByTestId(component, "location-picker-enable-live-share").length).toBeTruthy();
|
2022-04-28 13:37:20 +02:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
mocked(SettingsStore.getValue).mockReturnValue(true);
|
|
|
|
// advance so watchSetting will update the value
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
component.setProps({});
|
2022-03-16 10:37:09 +01:00
|
|
|
|
2022-04-28 13:37:20 +02:00
|
|
|
// advanced to location picker
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(component.find("LocationPicker").length).toBeTruthy();
|
2022-03-15 16:04:52 +01:00
|
|
|
});
|
|
|
|
});
|
2022-03-18 10:52:24 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Live location share", () => {
|
2022-03-18 10:52:24 +01:00
|
|
|
beforeEach(() => enableSettings(["feature_location_share_live"]));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not display live location share option when composer has a relation", () => {
|
2022-06-03 10:28:19 +02:00
|
|
|
const relation = {
|
|
|
|
rel_type: RelationType.Thread,
|
2022-12-12 12:24:14 +01:00
|
|
|
event_id: "12345",
|
2022-06-03 10:28:19 +02:00
|
|
|
};
|
|
|
|
const component = getComponent({ relation });
|
|
|
|
|
|
|
|
expect(getShareTypeOption(component, LocationShareType.Live).length).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("creates beacon info event on submission", async () => {
|
2022-03-18 10:52:24 +01:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
|
|
|
|
|
|
|
// advance to location picker
|
|
|
|
setShareType(component, LocationShareType.Live);
|
|
|
|
setLocation(component);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getSubmitButton(component).at(0).simulate("click");
|
2022-03-18 10:52:24 +01:00
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
2022-06-30 09:33:51 +02:00
|
|
|
// flush stopping existing beacons promises
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2022-03-18 10:52:24 +01:00
|
|
|
expect(onFinished).toHaveBeenCalled();
|
2022-04-08 10:53:06 +02:00
|
|
|
const [eventRoomId, eventContent] = mockClient.unstable_createLiveBeacon.mock.calls[0];
|
2022-03-18 10:52:24 +01:00
|
|
|
expect(eventRoomId).toEqual(defaultProps.roomId);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(eventContent).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
// default timeout
|
|
|
|
timeout: DEFAULT_DURATION_MS,
|
|
|
|
description: `Ernie's live location`,
|
|
|
|
live: true,
|
|
|
|
[M_ASSET.name]: {
|
|
|
|
type: LocationAssetType.Self,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2022-03-18 10:52:24 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("opens error dialog when beacon creation fails", async () => {
|
2022-03-18 10:52:24 +01:00
|
|
|
// stub logger to keep console clean from expected error
|
2022-12-12 12:24:14 +01:00
|
|
|
const logSpy = jest.spyOn(logger, "error").mockReturnValue(undefined);
|
|
|
|
const error = new Error("oh no");
|
2022-03-18 10:52:24 +01:00
|
|
|
mockClient.unstable_createLiveBeacon.mockRejectedValue(error);
|
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
// advance to location picker
|
|
|
|
setShareType(component, LocationShareType.Live);
|
|
|
|
setLocation(component);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getSubmitButton(component).at(0).simulate("click");
|
2022-03-18 10:52:24 +01:00
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
2022-06-30 09:33:51 +02:00
|
|
|
await flushPromisesWithFakeTimers();
|
2022-04-28 13:37:20 +02:00
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-03-18 10:52:24 +01:00
|
|
|
|
|
|
|
expect(logSpy).toHaveBeenCalledWith("We couldn't start sharing your live location", error);
|
2022-12-12 12:24:14 +01: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",
|
|
|
|
}),
|
|
|
|
);
|
2022-07-13 12:55:08 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("opens error dialog when beacon creation fails with permission error", async () => {
|
2022-07-13 12:55:08 +02:00
|
|
|
// stub logger to keep console clean from expected error
|
2022-12-12 12:24:14 +01:00
|
|
|
const logSpy = jest.spyOn(logger, "error").mockReturnValue(undefined);
|
|
|
|
const error = { errcode: "M_FORBIDDEN" } as unknown as Error;
|
2022-07-13 12:55:08 +02:00
|
|
|
mockClient.unstable_createLiveBeacon.mockRejectedValue(error);
|
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
// advance to location picker
|
|
|
|
setShareType(component, LocationShareType.Live);
|
|
|
|
setLocation(component);
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getSubmitButton(component).at(0).simulate("click");
|
2022-07-13 12:55:08 +02:00
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(logSpy).toHaveBeenCalledWith("Insufficient permissions to start sharing your live location", error);
|
2022-12-12 12:24:14 +01:00
|
|
|
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 10:52:24 +01:00
|
|
|
});
|
|
|
|
});
|
2022-03-01 18:00:07 +01:00
|
|
|
});
|
2022-03-15 16:04:52 +01:00
|
|
|
|
|
|
|
function enableSettings(settings: string[]) {
|
|
|
|
mocked(SettingsStore).getValue.mockReturnValue(false);
|
2022-11-04 11:48:08 +01:00
|
|
|
mocked(SettingsStore).getValue.mockImplementation((settingName: string): any => settings.includes(settingName));
|
2022-03-15 16:04:52 +01:00
|
|
|
}
|