import { beforeEach, describe, expect, it, vi } from 'vitest' import ResultMap from '@/components/ResultMap.vue' import { mountWithPlugins } from './helpers' // MapLibre a besoin de WebGL, absent de jsdom : on vérifie les appels. type Fn = (...args: unknown[]) => unknown const mapInstance = { addControl: vi.fn(), resize: vi.fn(), jumpTo: vi.fn(), setLayoutProperty: vi.fn(), remove: vi.fn(), } const marker = { setLngLat: vi.fn().mockReturnThis(), addTo: vi.fn().mockReturnThis(), remove: vi.fn(), } vi.mock('maplibre-gl', () => ({ Map: vi.fn<() => typeof mapInstance>(function () { return mapInstance }), Marker: vi.fn<() => typeof marker>(function () { return marker }), NavigationControl: vi.fn(), ScaleControl: vi.fn(), setWorkerUrl: vi.fn(), })) describe('ResultMap', () => { // On ne vide que les mocks de la carte : l'appel à setWorkerUrl a lieu à l'import. beforeEach(() => { for (const fn of [...Object.values(mapInstance), ...Object.values(marker)]) fn.mockClear() }) it('indique à MapLibre où trouver son worker', async () => { const { setWorkerUrl } = await import('maplibre-gl') expect(setWorkerUrl).toHaveBeenCalledWith(expect.stringContaining('maplibre-gl-worker')) }) it('occupe toute la place tant qu’aucune adresse n’est analysée', async () => { const wrapper = mountWithPlugins(ResultMap, { props: { location: null } }) expect(wrapper.classes()).toContain('expanded') await wrapper.setProps({ location: { lat: 50.668, lon: 4.617, region: 'WAL' } }) expect(wrapper.classes()).not.toContain('expanded') }) it('interroge les trois plages d’échelle de l’aléa d’inondation', async () => { const { Map } = await import('maplibre-gl') mountWithPlugins(ResultMap, { props: { location: null } }) const style = vi.mocked(Map).mock.lastCall![0].style as { sources: Record } expect(style.sources.flood!.tiles![0]).toContain('layers=1%2C2%2C3') expect(style.sources.flood!.tiles![0]).toContain('bbox={bbox-epsg-3857}') }) it("centre la carte et place le marqueur sur l'adresse", async () => { const wrapper = mountWithPlugins(ResultMap, { props: { location: null } }) expect(marker.setLngLat).not.toHaveBeenCalled() await wrapper.setProps({ location: { lat: 50.668, lon: 4.617, region: 'WAL' } }) expect(marker.setLngLat).toHaveBeenCalledWith([4.617, 50.668]) expect(mapInstance.jumpTo).toHaveBeenCalledWith( expect.objectContaining({ center: [4.617, 50.668], zoom: 15 }), ) }) it('affiche ou masque une couche WMS', async () => { const wrapper = mountWithPlugins(ResultMap, { props: { location: null } }) await wrapper.find('#layer-noise').setValue(true) expect(mapInstance.setLayoutProperty).toHaveBeenCalledWith('noise', 'visibility', 'visible') await wrapper.find('#layer-flood').trigger('change') expect(mapInstance.setLayoutProperty).toHaveBeenCalledWith('flood', 'visibility', 'none') }) it('libère la carte au démontage', () => { mountWithPlugins(ResultMap, { props: { location: null } }).unmount() expect(mapInstance.remove).toHaveBeenCalled() }) })