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
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,30 @@ const testAttributes = {
};

describe('<MlFeatureEditor>', () => {
it('should register 2 event listeners to the maplibre instance', async () => {
it('should register 3 event listeners to the maplibre instance', async () => {
render(
<MapComponentsProvider>
<MlFeatureEditorTestComponent {...testAttributes} />
</MapComponentsProvider>
);

// MapLibreGlWrapper now subscribes to "data", "move" events on its own
await waitFor(() => expect(mockMapLibreMethods.on).toHaveBeenCalledTimes(7));
await waitFor(() => expect(mockMapLibreMethods.on).toHaveBeenCalledTimes(8));
});

it('should deregister 2 event listeners to the maplibre instance', async () => {
it('should deregister 3 event listeners to the maplibre instance', async () => {
render(
<MapComponentsProvider>
<MlFeatureEditorTestComponent {...testAttributes} />
</MapComponentsProvider>
);

// MapLibreGlWrapper now subscribes to "data", "move" events on its own
expect(mockMapLibreMethods.on).toHaveBeenCalledTimes(7);
expect(mockMapLibreMethods.on).toHaveBeenCalledTimes(8);

await userEvent.click(screen.getByTestId('toggle_layer_visible'));

expect(mockMapLibreMethods.off).toHaveBeenCalledTimes(3);
expect(mockMapLibreMethods.off).toHaveBeenCalledTimes(7);
});

it('should add MapBox-Gl-draw instance using map.addControl to the maplibre instance', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,10 @@ const useFeatureEditor = (props: useFeatureEditorProps) => {
}, [mapHook.map, props, drawToolsInitialized, modeChangeHandler]);

useEffect(() => {
if (!mapHook.map || !drawToolsReady) return;
if (!mapHook.map || !drawToolsReady || !draw.current) return;

const changeHandler = (ev: unknown) => {
let features: Feature[] = [];

if (ev && typeof ev === 'object' && 'features' in ev && ev.features) {
features = ev.features as Feature[];
}
const features = (ev as { features: Feature[] })?.features || [];

setFeature(features);

Expand All @@ -126,16 +122,18 @@ const useFeatureEditor = (props: useFeatureEditorProps) => {
}
};

mapHook.map.on('mouseup', changeHandler);
mapHook.map.on('touchend', changeHandler);
mapHook.map.on('draw.create' as MapLibreGlEventName, changeHandler, mapHook.componentId);
mapHook.map.on('draw.update' as MapLibreGlEventName, changeHandler, mapHook.componentId);
mapHook.map.on('draw.delete' as MapLibreGlEventName, changeHandler, mapHook.componentId);

return () => {
if (!mapHook.map) return;

mapHook.map.map.off('mouseup', changeHandler);
mapHook.map.map.off('touchend', changeHandler);
mapHook.map.map.off('draw.create', changeHandler);
mapHook.map.map.off('draw.update', changeHandler);
mapHook.map.map.off('draw.delete', changeHandler);
};
}, [drawToolsReady, mapHook.map]);
}, [drawToolsReady, mapHook.map, props.onChange]);

useEffect(() => {
if (draw.current && props.geojson?.geometry) {
Expand Down
Loading