Skip to content

Commit

Permalink
🎨 [#445] Ensure map interaction tests are not flaky
Browse files Browse the repository at this point in the history
* Added a test id for testing-library to the map container so that the
  interaction tests only start running when the container is visible
* Added robustness for interaction elements that may exist in the DOM
  but not be visible yet because map tiles are still loading

waitFor has longer default timeouts than canvas.findBy* queries, and
while the latter can be specified, the element is find before it's
visible and there are still race conditions. For this reason, the
visibility check is decoupled from the lookup expectation.
  • Loading branch information
sergei-maertens committed Mar 5, 2025
1 parent c6b1f69 commit 2cf3ec0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/components/Map/LeafletMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const LeaftletMap = ({
duration: 3000,
}}
>
<EnsureTestId />
<TileLayer {...TILE_LAYER_RD} url={tileLayerUrl} />
<FeatureGroup ref={featureGroupRef}>
{!disabled && (
Expand Down Expand Up @@ -172,6 +173,17 @@ LeaftletMap.propTypes = {
tileLayerUrl: PropTypes.string,
};

const EnsureTestId = () => {
const map = useMap();
const container = map.getContainer();
useEffect(() => {

Check warning on line 179 in src/components/Map/LeafletMap.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/Map/LeafletMap.jsx#L176-L179

Added lines #L176 - L179 were not covered by tests
if (!container.dataset.testid) {
container.dataset.testid = 'leaflet-map';

Check warning on line 181 in src/components/Map/LeafletMap.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/Map/LeafletMap.jsx#L181

Added line #L181 was not covered by tests
}
}, [container]);
return null;

Check warning on line 184 in src/components/Map/LeafletMap.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/Map/LeafletMap.jsx#L184

Added line #L184 was not covered by tests
};

const Geometry = ({geoJsonGeometry, featureGroupRef}) => {
useEffect(() => {
if (!featureGroupRef.current) {
Expand Down
18 changes: 14 additions & 4 deletions src/components/Map/Map.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,22 @@ export const MapWithInteractions = {
},
play: async ({canvasElement, step, args}) => {
const canvas = within(canvasElement);
const map = canvasElement.querySelector('.leaflet-pane.leaflet-map-pane');
const map = await canvas.findByTestId('leaflet-map');

await waitFor(() => {
expect(map).not.toBeNull();
expect(map).toBeVisible();
});

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();
const pin = await canvas.findByTitle('Pin/punt');
await waitFor(() => expect(pin).toBeVisible());

const polygon = await canvas.findByTitle('Veelhoek (polygoon)');
await waitFor(() => expect(polygon).toBeVisible());

const line = await canvas.findByTitle('Lijn');
await waitFor(() => expect(line).toBeVisible());
});

await step('Draw a marker', async () => {
Expand Down

0 comments on commit 2cf3ec0

Please sign in to comment.