Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit b760ec9

Browse files
author
Kerry
authored
Location sharing - extract isSelfLocation util (#8279)
* extract isSelfLocation into utils Signed-off-by: Kerry Archibald <[email protected]> * replace use of isSelfLocation Signed-off-by: Kerry Archibald <[email protected]>
1 parent aecd71a commit b760ec9

File tree

5 files changed

+99
-68
lines changed

5 files changed

+99
-68
lines changed

src/components/views/messages/MLocationBody.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ limitations under the License.
1717
import React from 'react';
1818
import maplibregl from 'maplibre-gl';
1919
import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
20-
import {
21-
M_ASSET,
22-
LocationAssetType,
23-
ILocationContent,
24-
} from 'matrix-js-sdk/src/@types/location';
2520
import { ClientEvent, IClientWellKnown } from 'matrix-js-sdk/src/client';
2621

2722
import { IBodyProps } from "./IBodyProps";
@@ -33,6 +28,7 @@ import {
3328
createMapWithCoords,
3429
getLocationShareErrorMessage,
3530
LocationShareError,
31+
isSelfLocation,
3632
} from '../../../utils/location';
3733
import LocationViewDialog from '../location/LocationViewDialog';
3834
import TooltipTarget from '../elements/TooltipTarget';
@@ -132,12 +128,6 @@ export default class MLocationBody extends React.Component<IBodyProps, IState> {
132128
}
133129
}
134130

135-
export function isSelfLocation(locationContent: ILocationContent): boolean {
136-
const asset = M_ASSET.findIn(locationContent) as { type: string };
137-
const assetType = asset?.type ?? LocationAssetType.Self;
138-
return assetType == LocationAssetType.Self;
139-
}
140-
141131
export const LocationBodyFallbackContent: React.FC<{ event: MatrixEvent, error: Error }> = ({ error, event }) => {
142132
const errorType = error?.message as LocationShareError;
143133
const message = `${_t('Unable to load map')}: ${getLocationShareErrorMessage(errorType)}`;

src/utils/location/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
export * from './findMapStyleUrl';
18+
export * from './isSelfLocation';
1819
export * from './locationEventGeoUri';
1920
export * from './LocationShareErrors';
2021
export * from './map';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { ILocationContent, LocationAssetType, M_ASSET } from "matrix-js-sdk/src/@types/location";
18+
19+
export const isSelfLocation = (locationContent: ILocationContent): boolean => {
20+
const asset = M_ASSET.findIn(locationContent) as { type: string };
21+
const assetType = asset?.type ?? LocationAssetType.Self;
22+
return assetType == LocationAssetType.Self;
23+
};

test/components/views/messages/MLocationBody-test.tsx

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,11 @@ limitations under the License.
1717
import React from 'react';
1818
import { mount } from "enzyme";
1919
import { mocked } from 'jest-mock';
20-
import { makeLocationContent } from "matrix-js-sdk/src/content-helpers";
21-
import {
22-
M_ASSET,
23-
LocationAssetType,
24-
ILocationContent,
25-
M_LOCATION,
26-
M_TIMESTAMP,
27-
} from "matrix-js-sdk/src/@types/location";
28-
import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events";
20+
import { LocationAssetType } from "matrix-js-sdk/src/@types/location";
2921
import maplibregl from 'maplibre-gl';
3022
import { logger } from 'matrix-js-sdk/src/logger';
3123

32-
import MLocationBody, {
33-
isSelfLocation,
34-
} from "../../../../src/components/views/messages/MLocationBody";
24+
import MLocationBody from "../../../../src/components/views/messages/MLocationBody";
3525
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
3626
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
3727
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
@@ -44,51 +34,6 @@ jest.mock("../../../../src/utils/WellKnownUtils", () => ({
4434
}));
4535

4636
describe("MLocationBody", () => {
47-
describe("isSelfLocation", () => {
48-
it("Returns true for a full m.asset event", () => {
49-
const content = makeLocationContent("", '0');
50-
expect(isSelfLocation(content)).toBe(true);
51-
});
52-
53-
it("Returns true for a missing m.asset", () => {
54-
const content = {
55-
body: "",
56-
msgtype: "m.location",
57-
geo_uri: "",
58-
[M_LOCATION.name]: { uri: "" },
59-
[TEXT_NODE_TYPE.name]: "",
60-
[M_TIMESTAMP.name]: 0,
61-
// Note: no m.asset!
62-
};
63-
expect(isSelfLocation(content as ILocationContent)).toBe(true);
64-
});
65-
66-
it("Returns true for a missing m.asset type", () => {
67-
const content = {
68-
body: "",
69-
msgtype: "m.location",
70-
geo_uri: "",
71-
[M_LOCATION.name]: { uri: "" },
72-
[TEXT_NODE_TYPE.name]: "",
73-
[M_TIMESTAMP.name]: 0,
74-
[M_ASSET.name]: {
75-
// Note: no type!
76-
},
77-
};
78-
expect(isSelfLocation(content as ILocationContent)).toBe(true);
79-
});
80-
81-
it("Returns false for an unknown asset type", () => {
82-
const content = makeLocationContent(
83-
undefined, /* text */
84-
"geo:foo",
85-
0,
86-
undefined, /* description */
87-
"org.example.unknown" as unknown as LocationAssetType);
88-
expect(isSelfLocation(content)).toBe(false);
89-
});
90-
});
91-
9237
describe('<MLocationBody>', () => {
9338
describe('with error', () => {
9439
const mockClient = {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events";
18+
import {
19+
ILocationContent,
20+
LocationAssetType,
21+
M_ASSET,
22+
M_LOCATION,
23+
M_TIMESTAMP,
24+
} from "matrix-js-sdk/src/@types/location";
25+
import { makeLocationContent } from "matrix-js-sdk/src/content-helpers";
26+
27+
import { isSelfLocation } from "../../../src/utils/location";
28+
29+
describe("isSelfLocation", () => {
30+
it("Returns true for a full m.asset event", () => {
31+
const content = makeLocationContent("", '0');
32+
expect(isSelfLocation(content)).toBe(true);
33+
});
34+
35+
it("Returns true for a missing m.asset", () => {
36+
const content = {
37+
body: "",
38+
msgtype: "m.location",
39+
geo_uri: "",
40+
[M_LOCATION.name]: { uri: "" },
41+
[TEXT_NODE_TYPE.name]: "",
42+
[M_TIMESTAMP.name]: 0,
43+
// Note: no m.asset!
44+
};
45+
expect(isSelfLocation(content as ILocationContent)).toBe(true);
46+
});
47+
48+
it("Returns true for a missing m.asset type", () => {
49+
const content = {
50+
body: "",
51+
msgtype: "m.location",
52+
geo_uri: "",
53+
[M_LOCATION.name]: { uri: "" },
54+
[TEXT_NODE_TYPE.name]: "",
55+
[M_TIMESTAMP.name]: 0,
56+
[M_ASSET.name]: {
57+
// Note: no type!
58+
},
59+
};
60+
expect(isSelfLocation(content as ILocationContent)).toBe(true);
61+
});
62+
63+
it("Returns false for an unknown asset type", () => {
64+
const content = makeLocationContent(
65+
undefined, /* text */
66+
"geo:foo",
67+
0,
68+
undefined, /* description */
69+
"org.example.unknown" as unknown as LocationAssetType);
70+
expect(isSelfLocation(content)).toBe(false);
71+
});
72+
});

0 commit comments

Comments
 (0)