From ec92be9a28263c420b8c04f8210b186811a27719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eva-Marie=20Sch=C3=BCrg?= Date: Fri, 29 Aug 2025 11:56:47 +0200 Subject: [PATCH] add and remove maplibre instance --- src/components/MapLibreMap/MapLibreMap.cy.tsx | 54 +++++++++++++++---- .../MapLibreMap/MapLibreMap.stories.tsx | 44 +++++++++++++-- src/components/MapLibreMap/MapLibreMap.tsx | 1 + 3 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/components/MapLibreMap/MapLibreMap.cy.tsx b/src/components/MapLibreMap/MapLibreMap.cy.tsx index f895d43c5..ece8736f3 100644 --- a/src/components/MapLibreMap/MapLibreMap.cy.tsx +++ b/src/components/MapLibreMap/MapLibreMap.cy.tsx @@ -3,29 +3,65 @@ import { composeStories } from '@storybook/testing-react'; import { mount } from '@cypress/react18'; import * as stories from './MapLibreMap.stories'; -const { ExampleConfig }: any = composeStories(stories); +const { ExampleConfig, AddMaplibreInstance }: any = composeStories(stories); describe('MlTerrainLayer Tests', () => { - it('Should display Maplibre map with osm bright style', () => { + it('Should display Maplibre map with OSM Bright style', () => { mount(); - // Wait for the map to be initialized and verify, that the terrain layer is added to the map + // Wait for the map to be initialized and verify that the terrain layer is added to the map cy.window() .should((win) => { expect((win as any)._map).to.exist; }) .then((win) => { const { _map }: any = win; - cy.wrap(_map).should((_map: any) => { - // check for style name - expect(_map?.getStyle().name).to.equal('OSM Bright'); - // check for one layer + cy.wrap(_map).should((_mapInstance: any) => { + // Check for style name + expect(_mapInstance?.getStyle().name).to.equal('OSM Bright'); + // Check for specific layer existence expect( - _map + _mapInstance ?.getStyle() .layers.find((layer: { id: string }) => layer.id === 'landcover-glacier') - ).not.be.undefined; + ).to.exist; }); }); }); }); + +describe('AddMaplibreInstance Tests', () => { + beforeEach(() => { + mount(); + }); + + it('Map should be added by clicking button "Add Map"', () => { + // Initially, the map should not exist + cy.get('[data-testid="map"]').should('not.exist'); + + // Click the "Add Map" button + cy.contains('button', 'Add Map').click(); + + // Verify that the map element now exists in the DOM + cy.get('[data-testid="map"]').should('exist'); + + // Verify that the map instance exists in the window object + cy.window().should((win) => { + expect((win as any)._map).to.exist; + }); + }); + + it('Map should be removed by clicking button "Remove Map"', () => { + // Add the map first + cy.contains('button', 'Add Map').click(); + cy.get('[data-testid="map"]').should('exist'); + + // Click the "Remove Map" button + cy.contains('button', 'Remove Map').click(); + + cy.get('[data-testid="map"]').should('not.exist'); + cy.window().should((win) => { + expect((win as any)._map.getStyle()).to.be.undefined; + }); + }); +}); diff --git a/src/components/MapLibreMap/MapLibreMap.stories.tsx b/src/components/MapLibreMap/MapLibreMap.stories.tsx index 55a32e997..b2f65001f 100644 --- a/src/components/MapLibreMap/MapLibreMap.stories.tsx +++ b/src/components/MapLibreMap/MapLibreMap.stories.tsx @@ -1,11 +1,11 @@ -import React, { useState } from "react"; +import React, { useState } from 'react'; import MapLibreMap, { MapLibreMapProps } from './MapLibreMap'; import MlGeoJsonLayer from '../MlGeoJsonLayer/MlGeoJsonLayer'; import { Button } from '@mui/material'; import TopToolbar from '../../ui_components/TopToolbar'; import sample_geojson_1 from '../MlGeoJsonLayer/assets/sample_1.json'; -import {FeatureCollection} from 'geojson'; +import { FeatureCollection } from 'geojson'; import themeDecorator from '../../decorators/ThemeDecorator'; const storyoptions = { @@ -25,7 +25,7 @@ const storyoptions = { }; export default storyoptions; -const Template = (args:MapLibreMapProps) => { +const Template = (args: MapLibreMapProps) => { return ; }; @@ -46,7 +46,7 @@ const styles = [ }, ]; -const StyleChangeTemplate = (args:MapLibreMapProps) => { +const StyleChangeTemplate = (args: MapLibreMapProps) => { const [activeStyle, setActiveStyle] = useState(styles[1].url); return ( @@ -86,3 +86,39 @@ StyleChangeConfig.args = { }, }; StyleChangeConfig.parameters = {}; + +const AddMaplibreInstanceTemplate = (args: MapLibreMapProps) => { + const [isMap, setIsMap] = useState(false); + + const toggleMap = () => { + setIsMap((prev) => !prev); + }; + + return ( + <> + + {isMap ? 'Remove Map' : 'Add Map'} + + } + > + + {isMap && } + + ); +}; + +export const AddMaplibreInstance = AddMaplibreInstanceTemplate.bind({}); +AddMaplibreInstance.args = { + options: { + style: 'https://wms.wheregroup.com/tileserver/style/osm-bright.json', + center: [8.607, 53.1409349], + zoom: 14, + }, +}; +AddMaplibreInstance.parameters = {}; diff --git a/src/components/MapLibreMap/MapLibreMap.tsx b/src/components/MapLibreMap/MapLibreMap.tsx index de3ea3bf8..1b4fc97d0 100644 --- a/src/components/MapLibreMap/MapLibreMap.tsx +++ b/src/components/MapLibreMap/MapLibreMap.tsx @@ -131,6 +131,7 @@ const MapLibreMap: FC = (props: MapLibreMapProps) => { ref={mapContainer as RefObject} className="mapContainer" style={props.style} + data-testid={props.mapId ? `map-${props.mapId}` : 'map'} /> ); };