mirror of
https://github.com/symfony/ux-map.git
synced 2026-03-23 23:42:07 +01:00
77 lines
3.4 KiB
TypeScript
77 lines
3.4 KiB
TypeScript
import { Application } from '@hotwired/stimulus';
|
|
import { getByTestId, waitFor } from '@testing-library/dom';
|
|
import { clearDOM, mountDOM } from '@symfony/stimulus-testing';
|
|
import AbstractMapController from '../src/abstract_map_controller.ts';
|
|
|
|
class MyMapController extends AbstractMapController {
|
|
doCreateMap({ center, zoom, options }) {
|
|
return { map: 'map', center, zoom, options };
|
|
}
|
|
|
|
doCreateMarker(definition) {
|
|
const marker = { marker: 'marker', title: definition.title };
|
|
|
|
if (definition.infoWindow) {
|
|
this.createInfoWindow({ definition: definition.infoWindow, marker });
|
|
}
|
|
|
|
return marker;
|
|
}
|
|
|
|
doCreateInfoWindow({ definition, marker }) {
|
|
return { infoWindow: 'infoWindow', headerContent: definition.headerContent, marker: marker.title };
|
|
}
|
|
|
|
doFitBoundsToMarkers() {
|
|
// no-op
|
|
}
|
|
}
|
|
|
|
const startStimulus = () => {
|
|
const application = Application.start();
|
|
application.register('map', MyMapController);
|
|
return application;
|
|
};
|
|
|
|
describe('AbstractMapController', () => {
|
|
let container: HTMLElement;
|
|
|
|
beforeEach(() => {
|
|
container = mountDOM(`
|
|
<div
|
|
data-testid="map"
|
|
data-controller="map"
|
|
style="height: 700px; margin: 10px"
|
|
data-map-provider-options-value="{}"
|
|
data-map-view-value="{"center":{"lat":48.8566,"lng":2.3522},"zoom":4,"fitBoundsToMarkers":true,"options":{},"markers":[{"position":{"lat":48.8566,"lng":2.3522},"title":"Paris","infoWindow":null},{"position":{"lat":45.764,"lng":4.8357},"title":"Lyon","infoWindow":{"headerContent":"<b>Lyon<\/b>","content":"The French town in the historic Rh\u00f4ne-Alpes region, located at the junction of the Rh\u00f4ne and Sa\u00f4ne rivers.","position":null,"opened":false,"autoClose":true}}]}"
|
|
></div>
|
|
`);
|
|
});
|
|
|
|
afterEach(() => {
|
|
clearDOM();
|
|
});
|
|
|
|
it('connect and create map, marker and info window', async () => {
|
|
const div = getByTestId(container, 'map');
|
|
expect(div).not.toHaveClass('connected');
|
|
|
|
const application = startStimulus();
|
|
await waitFor(() => expect(application.getControllerForElementAndIdentifier(div, 'map')).not.toBeNull());
|
|
|
|
const controller = application.getControllerForElementAndIdentifier(div, 'map');
|
|
expect(controller.map).toEqual({ map: 'map', center: { lat: 48.8566, lng: 2.3522 }, zoom: 4, options: {} });
|
|
expect(controller.markers).toEqual([
|
|
{ marker: 'marker', title: 'Paris' },
|
|
{ marker: 'marker', title: 'Lyon' },
|
|
]);
|
|
expect(controller.infoWindows).toEqual([
|
|
{
|
|
headerContent: '<b>Lyon</b>',
|
|
infoWindow: 'infoWindow',
|
|
marker: 'Lyon',
|
|
},
|
|
]);
|
|
});
|
|
});
|