// __tests__/components/pois/PoiUtils.test.js import { createPoiMarkers, addMarkersToMap, updateLocationInDatabase } from "../../../components/pois/PoiUtils"; // Mock Leaflet jest.mock("leaflet", () => { const Leaflet = { marker: jest.fn((latlng, options) => ({ addTo: jest.fn(), on: jest.fn(), options: options, getLatLng: jest.fn(() => ({ lat: latlng[0], lng: latlng[1], })), })), icon: jest.fn((options) => options), }; return Leaflet; }); describe("PoiUtils", () => { describe("createPoiMarkers", () => { it("should create markers with correct coordinates and icon", () => { const poiData = [ { idPoi: "1", latitude: 52.52, longitude: 13.405 }, { idPoi: "2", latitude: 48.8566, longitude: 2.3522 }, ]; const iconPath = "path/to/icon.png"; const markers = createPoiMarkers(poiData, iconPath); expect(markers).toHaveLength(2); markers.forEach((marker, index) => { expect(marker.options.icon.iconUrl).toBe(iconPath); expect(marker.options.icon.iconSize).toEqual([25, 41]); expect(marker.options.id).toBe(poiData[index].idPoi); }); }); }); describe("addMarkersToMap", () => { it("should add markers to map and bind events", () => { const mockMap = {}; const mockLayerGroup = { addLayer: jest.fn() }; const mockMarker = { addTo: jest.fn(), on: jest.fn(), }; const markers = [mockMarker, mockMarker]; addMarkersToMap(markers, mockMap, mockLayerGroup); markers.forEach((marker) => { expect(marker.addTo).toHaveBeenCalledWith(mockLayerGroup); expect(marker.on).toHaveBeenCalledWith("mouseover", expect.any(Function)); expect(marker.on).toHaveBeenCalledWith("mouseout", expect.any(Function)); expect(marker.on).toHaveBeenCalledWith("dragend", expect.any(Function)); }); }); }); describe("updateLocationInDatabase", () => { beforeEach(() => { global.fetch = jest.fn(() => Promise.resolve({ ok: true, json: () => Promise.resolve({}), }) ); }); afterEach(() => { jest.clearAllMocks(); }); it("should call fetch with correct parameters", async () => { const id = "1"; const newLatitude = 52.52; const newLongitude = 13.405; await updateLocationInDatabase(id, newLatitude, newLongitude); expect(fetch).toHaveBeenCalledWith("/api/talas_v5_DB/pois/updateLocation", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id, latitude: newLatitude, longitude: newLongitude, }), }); }); it("should log error when fetch response is not ok", async () => { console.error = jest.fn(); global.fetch = jest.fn(() => Promise.resolve({ ok: false, json: () => Promise.resolve({}), }) ); await updateLocationInDatabase("1", 52.52, 13.405); expect(console.error).toHaveBeenCalledWith("Fehler beim Aktualisieren der Position"); }); }); });