2022-03-01 20:00:07 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
2022-03-02 02:51:05 +03:00
|
|
|
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
|
2022-03-02 16:00:40 +03:00
|
|
|
import { MatrixClient } from 'matrix-js-sdk/src/client';
|
|
|
|
import { mocked } from 'jest-mock';
|
|
|
|
import { act } from 'react-dom/test-utils';
|
2022-03-01 20:00:07 +03:00
|
|
|
|
|
|
|
import '../../../skinned-sdk';
|
|
|
|
import LocationShareMenu from '../../../../src/components/views/location/LocationShareMenu';
|
|
|
|
import MatrixClientContext from '../../../../src/contexts/MatrixClientContext';
|
|
|
|
import { ChevronFace } from '../../../../src/components/structures/ContextMenu';
|
2022-03-02 16:00:40 +03:00
|
|
|
import SettingsStore from '../../../../src/settings/SettingsStore';
|
|
|
|
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
|
|
|
import { LocationShareType } from '../../../../src/components/views/location/ShareType';
|
|
|
|
import { findByTestId } from '../../../test-utils';
|
|
|
|
|
|
|
|
jest.mock('../../../../src/components/views/messages/MLocationBody', () => ({
|
|
|
|
findMapStyleUrl: jest.fn().mockReturnValue('test'),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('../../../../src/settings/SettingsStore', () => ({
|
|
|
|
getValue: jest.fn(),
|
|
|
|
monitorSetting: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('../../../../src/stores/OwnProfileStore', () => ({
|
|
|
|
OwnProfileStore: {
|
|
|
|
instance: {
|
|
|
|
displayName: 'Ernie',
|
|
|
|
getHttpAvatarUrl: jest.fn().mockReturnValue('image.com/img'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
2022-03-01 20:00:07 +03:00
|
|
|
|
|
|
|
describe('<LocationShareMenu />', () => {
|
2022-03-02 16:00:40 +03:00
|
|
|
const userId = '@ernie:server.org';
|
2022-03-01 20:00:07 +03:00
|
|
|
const mockClient = {
|
|
|
|
on: jest.fn(),
|
2022-03-03 13:30:46 +03:00
|
|
|
off: jest.fn(),
|
2022-03-02 16:00:40 +03:00
|
|
|
removeListener: jest.fn(),
|
|
|
|
getUserId: jest.fn().mockReturnValue(userId),
|
|
|
|
getClientWellKnown: jest.fn().mockResolvedValue({
|
|
|
|
map_style_url: 'maps.com',
|
|
|
|
}),
|
2022-03-01 20:00:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
menuPosition: {
|
|
|
|
top: 1, left: 1,
|
|
|
|
chevronFace: ChevronFace.Bottom,
|
|
|
|
},
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
openMenu: jest.fn(),
|
|
|
|
roomId: '!room:server.org',
|
2022-03-02 16:00:40 +03:00
|
|
|
sender: new RoomMember('!room:server.org', userId),
|
2022-03-01 20:00:07 +03:00
|
|
|
};
|
|
|
|
const getComponent = (props = {}) =>
|
|
|
|
mount(<LocationShareMenu {...defaultProps} {...props} />, {
|
|
|
|
wrappingComponent: MatrixClientContext.Provider,
|
|
|
|
wrappingComponentProps: { value: mockClient },
|
|
|
|
});
|
|
|
|
|
2022-03-02 16:00:40 +03:00
|
|
|
beforeEach(() => {
|
|
|
|
mocked(SettingsStore).getValue.mockImplementation(
|
|
|
|
(settingName) => settingName === "feature_location_share_pin_drop",
|
|
|
|
);
|
|
|
|
|
|
|
|
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue(mockClient as unknown as MatrixClient);
|
|
|
|
});
|
|
|
|
|
|
|
|
const getShareTypeOption = (component, shareType: LocationShareType) =>
|
|
|
|
findByTestId(component, `share-location-option-${shareType}`);
|
2022-03-03 13:30:46 +03:00
|
|
|
const getBackButton = component => findByTestId(component, 'share-dialog-buttons-back');
|
|
|
|
const getCancelButton = component => findByTestId(component, 'share-dialog-buttons-cancel');
|
2022-03-02 16:00:40 +03:00
|
|
|
|
2022-03-03 13:30:46 +03:00
|
|
|
describe('when only Own share type is enabled', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(SettingsStore).getValue.mockReturnValue(false);
|
|
|
|
});
|
2022-03-02 16:00:40 +03:00
|
|
|
|
2022-03-03 13:30:46 +03:00
|
|
|
it('renders location picker when only Own share type is enabled', () => {
|
|
|
|
const component = getComponent();
|
|
|
|
expect(component.find('ShareType').length).toBeFalsy();
|
|
|
|
expect(component.find('LocationPicker').length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not render back button when only Own share type is enabled', () => {
|
|
|
|
const component = getComponent();
|
|
|
|
expect(getBackButton(component).length).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clicking cancel button from location picker closes dialog', () => {
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
2022-03-02 16:00:40 +03:00
|
|
|
|
2022-03-03 13:30:46 +03:00
|
|
|
act(() => {
|
|
|
|
getCancelButton(component).at(0).simulate('click');
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2022-03-02 16:00:40 +03:00
|
|
|
});
|
|
|
|
|
2022-03-03 13:30:46 +03:00
|
|
|
describe('with pin drop share type enabled', () => {
|
2022-03-02 16:00:40 +03:00
|
|
|
// feature_location_share_pin_drop is set to enabled by default mocking
|
|
|
|
|
2022-03-03 13:30:46 +03:00
|
|
|
it('renders share type switch with own and pin drop options', () => {
|
|
|
|
const component = getComponent();
|
|
|
|
expect(component.find('LocationPicker').length).toBeFalsy();
|
|
|
|
|
|
|
|
expect(getShareTypeOption(component, LocationShareType.Own).length).toBeTruthy();
|
|
|
|
expect(getShareTypeOption(component, LocationShareType.Pin).length).toBeTruthy();
|
2022-03-02 16:00:40 +03:00
|
|
|
});
|
|
|
|
|
2022-03-03 13:30:46 +03:00
|
|
|
it('does not render back button on share type screen', () => {
|
|
|
|
const component = getComponent();
|
|
|
|
expect(getBackButton(component).length).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clicking cancel button from share type screen closes dialog', () => {
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
getCancelButton(component).at(0).simulate('click');
|
|
|
|
});
|
2022-03-02 16:00:40 +03:00
|
|
|
|
2022-03-03 13:30:46 +03:00
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('selecting own location share type advances to location picker', () => {
|
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
getShareTypeOption(component, LocationShareType.Own).at(0).simulate('click');
|
|
|
|
});
|
|
|
|
|
|
|
|
component.setProps({});
|
|
|
|
|
|
|
|
expect(component.find('LocationPicker').length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clicking back button from location picker screen goes back to share screen', () => {
|
|
|
|
// feature_location_share_pin_drop is set to enabled by default mocking
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const component = getComponent({ onFinished });
|
|
|
|
|
|
|
|
// advance to location picker
|
|
|
|
act(() => {
|
|
|
|
getShareTypeOption(component, LocationShareType.Own).at(0).simulate('click');
|
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(component.find('LocationPicker').length).toBeTruthy();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
getBackButton(component).at(0).simulate('click');
|
|
|
|
component.setProps({});
|
|
|
|
});
|
|
|
|
|
|
|
|
// back to share type
|
|
|
|
expect(component.find('ShareType').length).toBeTruthy();
|
|
|
|
});
|
2022-03-01 20:00:07 +03:00
|
|
|
});
|
|
|
|
});
|