Live location share - set time limit (#8082)
* add mocking helpers for platform peg Signed-off-by: Kerry Archibald <kerrya@element.io> * basic working live duration dropdown Signed-off-by: Kerry Archibald <kerrya@element.io> * add duration format utility Signed-off-by: Kerry Archibald <kerrya@element.io> * add duration dropdown to live location picker Signed-off-by: Kerry Archibald <kerrya@element.io> * adjust style to allow overflow and variable height chin Signed-off-by: Kerry Archibald <kerrya@element.io> * tidy comments Signed-off-by: Kerry Archibald <kerrya@element.io> * arrow fn change Signed-off-by: Kerry Archibald <kerrya@element.io> * lint Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
75
test/components/views/location/LiveDurationDropdown-test.tsx
Normal file
75
test/components/views/location/LiveDurationDropdown-test.tsx
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
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';
|
||||
import { act } from 'react-dom/test-utils';
|
||||
|
||||
import '../../../skinned-sdk';
|
||||
import LiveDurationDropdown, { DEFAULT_DURATION_MS }
|
||||
from '../../../../src/components/views/location/LiveDurationDropdown';
|
||||
import { findById, mockPlatformPeg } from '../../../test-utils';
|
||||
|
||||
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
|
||||
|
||||
describe('<LiveDurationDropdown />', () => {
|
||||
const defaultProps = {
|
||||
timeout: DEFAULT_DURATION_MS,
|
||||
onChange: jest.fn(),
|
||||
};
|
||||
const getComponent = (props = {}) =>
|
||||
mount(<LiveDurationDropdown {...defaultProps} {...props} />);
|
||||
|
||||
const getOption = (wrapper, timeout) => findById(wrapper, `live-duration__${timeout}`).at(0);
|
||||
const getSelectedOption = (wrapper) => findById(wrapper, 'live-duration_value');
|
||||
const openDropdown = (wrapper) => act(() => {
|
||||
wrapper.find('[role="button"]').at(0).simulate('click');
|
||||
wrapper.setProps({});
|
||||
});
|
||||
|
||||
it('renders timeout as selected option', () => {
|
||||
const wrapper = getComponent();
|
||||
expect(getSelectedOption(wrapper).text()).toEqual('Share for 15m');
|
||||
});
|
||||
|
||||
it('renders non-default timeout as selected option', () => {
|
||||
const timeout = 1234567;
|
||||
const wrapper = getComponent({ timeout });
|
||||
expect(getSelectedOption(wrapper).text()).toEqual(`Share for 21m`);
|
||||
});
|
||||
|
||||
it('renders a dropdown option for a non-default timeout value', () => {
|
||||
const timeout = 1234567;
|
||||
const wrapper = getComponent({ timeout });
|
||||
openDropdown(wrapper);
|
||||
expect(getOption(wrapper, timeout).text()).toEqual(`Share for 21m`);
|
||||
});
|
||||
|
||||
it('updates value on option selection', () => {
|
||||
const onChange = jest.fn();
|
||||
const wrapper = getComponent({ onChange });
|
||||
|
||||
const ONE_HOUR = 3600000;
|
||||
|
||||
openDropdown(wrapper);
|
||||
|
||||
act(() => {
|
||||
getOption(wrapper, ONE_HOUR).simulate('click');
|
||||
});
|
||||
|
||||
expect(onChange).toHaveBeenCalledWith(ONE_HOUR);
|
||||
});
|
||||
});
|
||||
@@ -28,7 +28,7 @@ import LocationPicker, { getGeoUri } from "../../../../src/components/views/loca
|
||||
import { LocationShareType } from "../../../../src/components/views/location/shareLocation";
|
||||
import MatrixClientContext from '../../../../src/contexts/MatrixClientContext';
|
||||
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
||||
import { findByTestId } from '../../../test-utils';
|
||||
import { findById, findByTestId, mockPlatformPeg } from '../../../test-utils';
|
||||
import { findMapStyleUrl } from '../../../../src/components/views/location/findMapStyleUrl';
|
||||
import { LocationShareError } from '../../../../src/components/views/location/LocationShareErrors';
|
||||
|
||||
@@ -36,6 +36,9 @@ jest.mock('../../../../src/components/views/location/findMapStyleUrl', () => ({
|
||||
findMapStyleUrl: jest.fn().mockReturnValue('tileserver.com'),
|
||||
}));
|
||||
|
||||
// dropdown uses this
|
||||
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
|
||||
|
||||
describe("LocationPicker", () => {
|
||||
describe("getGeoUri", () => {
|
||||
it("Renders a URI with only lat and lon", () => {
|
||||
@@ -108,6 +111,7 @@ describe("LocationPicker", () => {
|
||||
};
|
||||
const mockClient = {
|
||||
on: jest.fn(),
|
||||
removeListener: jest.fn(),
|
||||
off: jest.fn(),
|
||||
isGuest: jest.fn(),
|
||||
getClientWellKnown: jest.fn(),
|
||||
@@ -206,7 +210,7 @@ describe("LocationPicker", () => {
|
||||
});
|
||||
|
||||
const testUserLocationShareTypes = (shareType: LocationShareType.Own | LocationShareType.Live) => {
|
||||
describe(`for ${shareType} location share type`, () => {
|
||||
describe('user location behaviours', () => {
|
||||
it('closes and displays error when geolocation errors', () => {
|
||||
// suppress expected error log
|
||||
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
||||
@@ -263,8 +267,42 @@ describe("LocationPicker", () => {
|
||||
});
|
||||
};
|
||||
|
||||
testUserLocationShareTypes(LocationShareType.Own);
|
||||
testUserLocationShareTypes(LocationShareType.Live);
|
||||
describe('for Own location share type', () => {
|
||||
testUserLocationShareTypes(LocationShareType.Own);
|
||||
});
|
||||
|
||||
describe('for Live location share type', () => {
|
||||
const shareType = LocationShareType.Live;
|
||||
testUserLocationShareTypes(shareType);
|
||||
|
||||
const getOption = (wrapper, timeout) => findById(wrapper, `live-duration__${timeout}`).at(0);
|
||||
const getDropdown = wrapper => findByTestId(wrapper, 'live-duration-dropdown');
|
||||
const getSelectedOption = (wrapper) => findById(wrapper, 'live-duration_value');
|
||||
|
||||
const openDropdown = (wrapper) => act(() => {
|
||||
const dropdown = getDropdown(wrapper);
|
||||
dropdown.find('[role="button"]').at(0).simulate('click');
|
||||
wrapper.setProps({});
|
||||
});
|
||||
|
||||
it('renders live duration dropdown with default option', () => {
|
||||
const wrapper = getComponent({ shareType });
|
||||
expect(getSelectedOption(getDropdown(wrapper)).text()).toEqual('Share for 15m');
|
||||
});
|
||||
|
||||
it('updates selected duration', () => {
|
||||
const wrapper = getComponent({ shareType });
|
||||
|
||||
openDropdown(wrapper);
|
||||
const dropdown = getDropdown(wrapper);
|
||||
act(() => {
|
||||
getOption(dropdown, 3600000).simulate('click');
|
||||
});
|
||||
|
||||
// value updated
|
||||
expect(getSelectedOption(getDropdown(wrapper)).text()).toEqual('Share for 1h');
|
||||
});
|
||||
});
|
||||
|
||||
describe('for Pin drop location share type', () => {
|
||||
const shareType = LocationShareType.Pin;
|
||||
@@ -298,14 +336,15 @@ describe("LocationPicker", () => {
|
||||
});
|
||||
|
||||
it('does not set position on geolocate event', () => {
|
||||
getComponent({ shareType });
|
||||
mocked(maplibregl.Marker).mockClear();
|
||||
const wrapper = getComponent({ shareType });
|
||||
act(() => {
|
||||
// @ts-ignore
|
||||
mocked(mockGeolocate).emit('geolocate', mockGeolocationPosition);
|
||||
});
|
||||
|
||||
// marker added
|
||||
expect(maplibregl.Marker).not.toHaveBeenCalled();
|
||||
// marker not added
|
||||
expect(wrapper.find('.mx_MLocationBody_markerBorder').length).toBeFalsy();
|
||||
});
|
||||
|
||||
it('sets position on click event', () => {
|
||||
|
||||
@@ -33,6 +33,7 @@ import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
||||
import { LocationShareType } from '../../../../src/components/views/location/shareLocation';
|
||||
import { findByTagAndTestId, flushPromises } from '../../../test-utils';
|
||||
import Modal from '../../../../src/Modal';
|
||||
import { DEFAULT_DURATION_MS } from '../../../../src/components/views/location/LiveDurationDropdown';
|
||||
|
||||
jest.mock('../../../../src/components/views/location/findMapStyleUrl', () => ({
|
||||
findMapStyleUrl: jest.fn().mockReturnValue('test'),
|
||||
@@ -316,7 +317,7 @@ describe('<LocationShareMenu />', () => {
|
||||
expect(eventContent).toEqual(expect.objectContaining({
|
||||
[M_BEACON_INFO.name]: {
|
||||
// default timeout
|
||||
timeout: 300000,
|
||||
timeout: DEFAULT_DURATION_MS,
|
||||
description: `Ernie's live location`,
|
||||
live: true,
|
||||
},
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './beacon';
|
||||
export * from './client';
|
||||
export * from './platform';
|
||||
export * from './test-utils';
|
||||
export * from './wrappers';
|
||||
export * from './utilities';
|
||||
|
||||
46
test/test-utils/platform.ts
Normal file
46
test/test-utils/platform.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
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 { MethodKeysOf, mocked, MockedObject } from "jest-mock";
|
||||
|
||||
import BasePlatform from "../../src/BasePlatform";
|
||||
import PlatformPeg from "../../src/PlatformPeg";
|
||||
|
||||
// doesn't implement abstract
|
||||
// @ts-ignore
|
||||
class MockPlatform extends BasePlatform {
|
||||
constructor(platformMocks: Partial<Record<MethodKeysOf<BasePlatform>, unknown>>) {
|
||||
super();
|
||||
Object.assign(this, platformMocks);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Mock Platform Peg
|
||||
* Creates a mock BasePlatform class
|
||||
* spys on PlatformPeg.get and returns mock platform
|
||||
* @returns MockPlatform instance
|
||||
*/
|
||||
export const mockPlatformPeg = (
|
||||
platformMocks: Partial<Record<MethodKeysOf<BasePlatform>, unknown>> = {},
|
||||
): MockedObject<BasePlatform> => {
|
||||
const mockPlatform = new MockPlatform(platformMocks);
|
||||
jest.spyOn(PlatformPeg, 'get').mockReturnValue(mockPlatform);
|
||||
return mocked(mockPlatform);
|
||||
};
|
||||
|
||||
export const unmockPlatformPeg = () => {
|
||||
jest.spyOn(PlatformPeg, 'get').mockRestore();
|
||||
};
|
||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { formatSeconds, formatRelativeTime } from "../../src/DateUtils";
|
||||
import { formatSeconds, formatRelativeTime, formatDuration } from "../../src/DateUtils";
|
||||
|
||||
describe("formatSeconds", () => {
|
||||
it("correctly formats time with hours", () => {
|
||||
@@ -70,3 +70,25 @@ describe("formatRelativeTime", () => {
|
||||
expect(formatRelativeTime(date, true)).toBe("Oct 31, 2020");
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDuration()', () => {
|
||||
type TestCase = [string, string, number];
|
||||
|
||||
const MINUTE_MS = 60000;
|
||||
const HOUR_MS = MINUTE_MS * 60;
|
||||
|
||||
it.each<TestCase>([
|
||||
['rounds up to nearest day when more than 24h - 40 hours', '2d', 40 * HOUR_MS],
|
||||
['rounds down to nearest day when more than 24h - 26 hours', '1d', 26 * HOUR_MS],
|
||||
['24 hours', '1d', 24 * HOUR_MS],
|
||||
['rounds to nearest hour when less than 24h - 23h', '23h', 23 * HOUR_MS],
|
||||
['rounds to nearest hour when less than 24h - 6h and 10min', '6h', 6 * HOUR_MS + 10 * MINUTE_MS],
|
||||
['rounds to nearest hours when less than 24h', '2h', 2 * HOUR_MS + 124234],
|
||||
['rounds to nearest minute when less than 1h - 59 minutes', '59m', 59 * MINUTE_MS],
|
||||
['rounds to nearest minute when less than 1h - 1 minute', '1m', MINUTE_MS],
|
||||
['rounds to nearest second when less than 1min - 59 seconds', '59s', 59000],
|
||||
['rounds to 0 seconds when less than a second - 123ms', '0s', 123],
|
||||
])('%s formats to %s', (_description, expectedResult, input) => {
|
||||
expect(formatDuration(input)).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user