Skip to content

Commit

Permalink
Merge pull request #751 from open-formulieren/feature/2177-map-intera…
Browse files Browse the repository at this point in the history
…ctions

Using multiple map interactions
  • Loading branch information
sergei-maertens authored Jan 22, 2025
2 parents de56b14 + 73607c2 commit 470f3b4
Show file tree
Hide file tree
Showing 14 changed files with 1,007 additions and 126 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"ibantools": "^3.3.0",
"immer": "^9.0.6",
"leaflet": "^1.9.4",
"leaflet-draw": "^1.0.4",
"leaflet-geosearch": "^3.8.0",
"leaflet-gesture-handling": "^1.2.2",
"microscope-sass": "^2.0.0",
Expand All @@ -51,6 +52,7 @@
"react-formio": "^4.3.0",
"react-intl": "^6.4.4",
"react-leaflet": "4.2.1",
"react-leaflet-draw": "^0.20.4",
"react-modal": "3.16.1",
"react-number-format": "5.2.2",
"react-router-dom": "^6.11.2",
Expand Down
2 changes: 1 addition & 1 deletion src/components/FormStepSummary/ComponentValueDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const MapDisplay = ({component, value}) => {
return <EmptyDisplay />;
}

return <Map markerCoordinates={value} disabled tileLayerUrl={component.tileLayerUrl} />;
return <Map geoJsonGeometry={value} disabled tileLayerUrl={component.tileLayerUrl} />;
};

const CoSignDisplay = ({value}) => {
Expand Down
86 changes: 81 additions & 5 deletions src/components/Map/Map.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {userEvent, within} from '@storybook/test';
import {expect, fn, userEvent, waitFor, within} from '@storybook/test';
import {useState} from 'react';

import {ConfigDecorator} from 'story-utils/decorators';

Expand All @@ -11,12 +12,29 @@ const withMapLayout = Story => (
</div>
);

const StorybookLeafletMap = props => {
const [geoJson, setGeoJson] = useState(props?.geoJsonGeometry);
const handleGeoJsonChange = args => {
if (props?.onGeoJsonGeometrySet) {
props?.onGeoJsonGeometrySet(args);
}
setGeoJson(args);
};
return (
<LeafletMap {...props} geoJsonGeometry={geoJson} onGeoJsonGeometrySet={handleGeoJsonChange} />
);
};

export default {
title: 'Private API / Map',
component: LeafletMap,
decorators: [withMapLayout, ConfigDecorator],
render: StorybookLeafletMap,
args: {
markerCoordinates: [52.1326332, 5.291266],
geoJsonGeometry: {
type: 'Point',
coordinates: [5.291266, 52.1326332],
},
defaultCenter: [52.1326332, 5.291266],
defaultZoomLevel: 12,
disabled: false,
Expand All @@ -31,17 +49,32 @@ export default {
export const Map = {};

export const MapWithAddressSearch = {
play: async ({canvasElement}) => {
args: {
onGeoJsonGeometrySet: fn(),
},
play: async ({canvasElement, args}) => {
const canvas = within(canvasElement);
const button = await canvas.findByLabelText('Zoeken');
await userEvent.click(button);

await waitFor(async () => {
const button = await canvas.findByLabelText('Zoeken');
await userEvent.click(button);
expect(await canvas.findByPlaceholderText('Zoek adres')).toBeVisible();
});

const searchField = await canvas.findByPlaceholderText('Zoek adres');
const searchBox = within(searchField.parentNode);
await userEvent.type(searchField, 'Gemeente Utrecht');
const searchResult = await searchBox.findByText('Utrecht, Utrecht, Utrecht');

await userEvent.click(searchResult);
await waitFor(async () => {
// A marker is placed on the search result
expect(args.onGeoJsonGeometrySet).toBeCalledWith({
type: 'Point',
// To make sure that this test doesn't magically fail, just expect any 2 values
coordinates: [expect.anything(), expect.anything()],
});
});
},
};

Expand All @@ -59,3 +92,46 @@ export const MapWithAerialPhotoBackground = {
'https://service.pdok.nl/hwh/luchtfotorgb/wmts/v1_0/Actueel_orthoHR/EPSG:28992/{z}/{x}/{y}.png',
},
};

export const MapWithInteractions = {
args: {
interactions: {
polygon: true,
polyline: true,
marker: true,
},
onGeoJsonGeometrySet: fn(),
},
parameters: {
msw: {
handlers: [mockAddressSearchGet, mockLatLngSearchEmptyGet],
},
},
play: async ({canvasElement, step, args}) => {
const canvas = within(canvasElement);
const map = canvasElement.querySelector('.leaflet-pane.leaflet-map-pane');

await step('All interactions are available', async () => {
expect(await canvas.findByTitle('Pin/punt')).toBeVisible();
expect(await canvas.findByTitle('Veelhoek (polygoon)')).toBeVisible();
expect(await canvas.findByTitle('Lijn')).toBeVisible();
});

await step('Draw a marker', async () => {
const markerButton = await canvas.findByTitle('Pin/punt');
await userEvent.click(markerButton);

await userEvent.click(map, {x: 100, y: 100});

// This 'button' is the placed marker on the map
expect(await canvas.findByRole('button', {name: 'Marker'})).toBeVisible();
expect(args.onGeoJsonGeometrySet).toBeCalledWith({
type: 'Point',
// Expect that the coordinates array contains 2 items.
// We cannot pin it to specific values, because they can differentiate.
// To make sure that this test doesn't magically fail, just expect any 2 values
coordinates: [expect.anything(), expect.anything()],
});
});
},
};
Loading

0 comments on commit 470f3b4

Please sign in to comment.