Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions package/src/overlays/__tests__/descriptorEquality.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ const basePolyline: PolylineDescriptor = {
strokeColor: '#FF0000',
strokeWidth: 3,
tappable: true,
zIndex: 4,
};

describeFieldCoverage(
Expand All @@ -209,6 +210,8 @@ describeFieldCoverage(
['strokeColor', (d) => ({ ...d, strokeColor: '#00FF00' })],
['strokeWidth', (d) => ({ ...d, strokeWidth: 4 })],
['tappable', (d) => ({ ...d, tappable: false })],
['zIndex', (d) => ({ ...d, zIndex: 9 })],
['a cleared zIndex', (d) => ({ ...d, zIndex: undefined })],
],
);

Expand All @@ -223,6 +226,19 @@ const basePolygon: PolygonDescriptor = {
strokeColor: '#0000FF',
strokeWidth: 2,
tappable: true,
zIndex: 5,
holes: [
[
{ latitude: 52.231, longitude: 21.015 },
{ latitude: 52.232, longitude: 21.016 },
{ latitude: 52.233, longitude: 21.015 },
],
[
{ latitude: 52.235, longitude: 21.02 },
{ latitude: 52.236, longitude: 21.021 },
{ latitude: 52.237, longitude: 21.02 },
],
],
};

describeFieldCoverage(
Expand All @@ -249,9 +265,36 @@ describeFieldCoverage(
['strokeColor', (d) => ({ ...d, strokeColor: '#00FF00' })],
['strokeWidth', (d) => ({ ...d, strokeWidth: 5 })],
['tappable', (d) => ({ ...d, tappable: false })],
['zIndex', (d) => ({ ...d, zIndex: 9 })],
['a cleared zIndex', (d) => ({ ...d, zIndex: undefined })],
[
'a hole coordinate',
(d) => ({
...d,
holes: d.holes?.map((ring, index) =>
index === 0
? ring.map((coordinate, coordinateIndex) =>
coordinateIndex === 0
? { ...coordinate, longitude: 22 }
: coordinate,
)
: ring,
),
}),
],
[
'the hole ring count',
(d) => ({ ...d, holes: d.holes?.slice(0, -1) }),
],
['cleared holes', (d) => ({ ...d, holes: undefined })],
],
);

test('polygonDescriptorsEqual detects holes added to an unset polygon', () => {
const withoutHoles = { ...basePolygon, holes: undefined };
expect(polygonDescriptorsEqual(withoutHoles, basePolygon)).toBe(false);
});

const baseCircle: CircleDescriptor = {
id: 'circle-1',
center: { latitude: 52.2297, longitude: 21.0122 },
Expand Down
24 changes: 24 additions & 0 deletions package/src/overlays/descriptorEquality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ function coordinateListsEqual(
return true;
}

function coordinateRingsEqual(
left: Coordinate[][] | undefined,
right: Coordinate[][] | undefined,
): boolean {
if (left === right) {
return true;
}

if (left == null || right == null || left.length !== right.length) {
return false;
}

for (let index = 0; index < left.length; index += 1) {
if (!coordinateListsEqual(left[index], right[index])) {
return false;
}
}

return true;
}

function pointsEqual(
left: MarkerAnchor | MarkerPoint | undefined,
right: MarkerAnchor | MarkerPoint | undefined,
Expand Down Expand Up @@ -138,6 +159,7 @@ export function polylineDescriptorsEqual(
left.strokeColor === right.strokeColor &&
left.strokeWidth === right.strokeWidth &&
left.tappable === right.tappable &&
left.zIndex === right.zIndex &&
coordinateListsEqual(left.coordinates, right.coordinates))
);
}
Expand All @@ -153,6 +175,8 @@ export function polygonDescriptorsEqual(
left.strokeColor === right.strokeColor &&
left.strokeWidth === right.strokeWidth &&
left.tappable === right.tappable &&
left.zIndex === right.zIndex &&
coordinateRingsEqual(left.holes, right.holes) &&
coordinateListsEqual(left.coordinates, right.coordinates))
);
}
Expand Down
Loading