From bd226cd0621bb136fd01580f33d659d5ab24dec5 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Fri, 17 Dec 2021 10:58:24 +0000 Subject: [PATCH] Tests for getGeoUri (#7400) Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/location/LocationPicker.tsx | 20 ++--- .../views/location/LocationPicker-test.tsx | 86 +++++++++++++++++++ 2 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 test/components/views/location/LocationPicker-test.tsx diff --git a/src/components/views/location/LocationPicker.tsx b/src/components/views/location/LocationPicker.tsx index f3f4d64986..5571f64d86 100644 --- a/src/components/views/location/LocationPicker.tsx +++ b/src/components/views/location/LocationPicker.tsx @@ -185,21 +185,12 @@ class LocationPicker extends React.Component { this.setState({ description: ev.target.value }); }; - private getGeoUri = (position) => { - return (`geo:${ position.coords.latitude },` + - position.coords.longitude + - ( position.coords.altitude !== undefined ? - `,${ position.coords.altitude }` : '' ) + - ( position.coords.accuracy !== undefined ? - `;u=${ position.coords.accuracy }` : '' )); - }; - private onOk = () => { const position = (this.state.type == LocationShareType.Custom) ? this.state.manualPosition : this.state.position; this.props.onChoose( - position ? this.getGeoUri(position) : undefined, + position ? getGeoUri(position) : undefined, position ? position.timestamp : undefined, this.state.type, this.state.description, @@ -263,4 +254,13 @@ class LocationPicker extends React.Component { } } +export function getGeoUri(position: GeolocationPosition): string { + return (`geo:${ position.coords.latitude },` + + position.coords.longitude + + ( position.coords.altitude !== undefined ? + `,${ position.coords.altitude }` : '' ) + + ( position.coords.accuracy !== undefined ? + `;u=${ position.coords.accuracy }` : '' )); +} + export default LocationPicker; diff --git a/test/components/views/location/LocationPicker-test.tsx b/test/components/views/location/LocationPicker-test.tsx new file mode 100644 index 0000000000..65c76e113a --- /dev/null +++ b/test/components/views/location/LocationPicker-test.tsx @@ -0,0 +1,86 @@ +/* +Copyright 2021 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 "../../../skinned-sdk"; +import { getGeoUri } from "../../../../src/components/views/location/LocationPicker"; + +describe("LocationPicker", () => { + describe("getGeoUri", () => { + it("Renders a URI with only lat and lon", () => { + const pos: GeolocationPosition = { + coords: { + latitude: 43.2, + longitude: 12.4, + altitude: undefined, + accuracy: undefined, + altitudeAccuracy: undefined, + heading: undefined, + speed: undefined, + }, + timestamp: 12334, + }; + expect(getGeoUri(pos)).toEqual("geo:43.2,12.4"); + }); + + it("Renders a URI with 3 coords", () => { + const pos: GeolocationPosition = { + coords: { + latitude: 43.2, + longitude: 12.4, + altitude: 332.54, + accuracy: undefined, + altitudeAccuracy: undefined, + heading: undefined, + speed: undefined, + }, + timestamp: 12334, + }; + expect(getGeoUri(pos)).toEqual("geo:43.2,12.4,332.54"); + }); + + it("Renders a URI with accuracy", () => { + const pos: GeolocationPosition = { + coords: { + latitude: 43.2, + longitude: 12.4, + altitude: undefined, + accuracy: 21, + altitudeAccuracy: undefined, + heading: undefined, + speed: undefined, + }, + timestamp: 12334, + }; + expect(getGeoUri(pos)).toEqual("geo:43.2,12.4;u=21"); + }); + + it("Renders a URI with accuracy and altitude", () => { + const pos: GeolocationPosition = { + coords: { + latitude: 43.2, + longitude: 12.4, + altitude: 12.3, + accuracy: 21, + altitudeAccuracy: undefined, + heading: undefined, + speed: undefined, + }, + timestamp: 12334, + }; + expect(getGeoUri(pos)).toEqual("geo:43.2,12.4,12.3;u=21"); + }); + }); +});