Skip to content

Commit a87e505

Browse files
committed
[tests] Add unit tests for zoom events
1 parent a117360 commit a87e505

File tree

2 files changed

+190
-1
lines changed

2 files changed

+190
-1
lines changed

src/js/netjsongraph.render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class NetJSONGraphRender {
471471
const maxZoom = self.leaflet.getMaxZoom();
472472
const zoomIn = document.querySelector(".leaflet-control-zoom-in");
473473
const zoomOut = document.querySelector(".leaflet-control-zoom-out");
474-
474+
475475
if (zoomIn && zoomOut) {
476476
if (Math.round(currentZoom) >= maxZoom) {
477477
zoomIn.classList.add("leaflet-disabled");

test/netjsongraph.render.test.js

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,192 @@ describe("Test disableClusteringAtLevel: 0", () => {
872872
expect(mockMarkerClusterGroupInstance.addTo).not.toHaveBeenCalled();
873873
});
874874
});
875+
876+
describe("Test leaflet zoomend handler for label and zoom control state", () => {
877+
let renderInstance;
878+
let mockSelf;
879+
let mockLeafletInstance;
880+
let zoomInBtn;
881+
let zoomOutBtn;
882+
883+
beforeEach(() => {
884+
document
885+
.querySelectorAll(".leaflet-control-zoom-in, .leaflet-control-zoom-out")
886+
.forEach((el) => el.remove());
887+
zoomInBtn = document.createElement("a");
888+
zoomInBtn.className = "leaflet-control-zoom-in";
889+
zoomOutBtn = document.createElement("a");
890+
zoomOutBtn.className = "leaflet-control-zoom-out";
891+
document.body.appendChild(zoomInBtn);
892+
document.body.appendChild(zoomOutBtn);
893+
894+
mockLeafletInstance = {
895+
on: jest.fn(),
896+
getZoom: jest.fn(),
897+
getMinZoom: jest.fn(),
898+
getMaxZoom: jest.fn(),
899+
getBounds: jest.fn(),
900+
addLayer: jest.fn(),
901+
latLngToContainerPoint: jest.fn(() => ({x: 0, y: 0})),
902+
};
903+
904+
mockSelf = {
905+
type: "geojson",
906+
data: {type: "FeatureCollection", features: []},
907+
config: {
908+
clustering: false,
909+
disableClusteringAtLevel: 0,
910+
clusterRadius: 80,
911+
geoOptions: {},
912+
clusteringAttribute: null,
913+
prepareData: jest.fn((d) => d),
914+
onClickElement: jest.fn(),
915+
mapOptions: {},
916+
mapTileConfig: [{}],
917+
showLabelsAtZoomLevel: 3,
918+
},
919+
leaflet: mockLeafletInstance,
920+
echarts: {
921+
setOption: jest.fn(),
922+
_api: {
923+
getCoordinateSystems: jest.fn(() => [
924+
{getLeaflet: () => mockLeafletInstance},
925+
]),
926+
},
927+
},
928+
utils: {
929+
deepMergeObj: jest.fn((obj1, obj2) => ({...obj1, ...obj2})),
930+
},
931+
event: {
932+
emit: jest.fn(),
933+
},
934+
el: document.createElement("div"),
935+
};
936+
937+
jest.spyOn(L, "geoJSON").mockImplementation(() => ({
938+
addTo: jest.fn(),
939+
on: jest.fn(),
940+
}));
941+
jest.spyOn(L, "map").mockImplementation(() => mockLeafletInstance);
942+
943+
renderInstance = new NetJSONGraphRender();
944+
});
945+
946+
afterEach(() => {
947+
document.body.removeChild(zoomInBtn);
948+
document.body.removeChild(zoomOutBtn);
949+
jest.restoreAllMocks();
950+
});
951+
952+
test("should show label when zoom >= showLabelsAtZoomLevel and update zoom controls", () => {
953+
mockLeafletInstance.getZoom.mockReturnValue(4);
954+
mockLeafletInstance.getMinZoom.mockReturnValue(2);
955+
mockLeafletInstance.getMaxZoom.mockReturnValue(4);
956+
957+
let zoomendHandler;
958+
mockLeafletInstance.on.mockImplementation((event, handler) => {
959+
if (event === "zoomend") {
960+
zoomendHandler = handler;
961+
}
962+
});
963+
964+
renderInstance.mapRender(mockSelf.data, mockSelf);
965+
966+
zoomInBtn.classList.remove("leaflet-disabled");
967+
zoomOutBtn.classList.remove("leaflet-disabled");
968+
969+
expect(document.querySelector(".leaflet-control-zoom-in")).toBe(zoomInBtn);
970+
expect(document.querySelector(".leaflet-control-zoom-out")).toBe(
971+
zoomOutBtn,
972+
);
973+
974+
zoomendHandler();
975+
976+
expect(mockSelf.echarts.setOption).toHaveBeenCalledWith({
977+
series: [{label: {show: true}}],
978+
});
979+
980+
expect(zoomInBtn.classList.contains("leaflet-disabled")).toBe(true);
981+
expect(zoomOutBtn.classList.contains("leaflet-disabled")).toBe(false);
982+
});
983+
984+
test("should hide label when zoom < showLabelsAtZoomLevel and update zoom controls", () => {
985+
mockLeafletInstance.getZoom.mockReturnValue(1);
986+
mockLeafletInstance.getMinZoom.mockReturnValue(1);
987+
mockLeafletInstance.getMaxZoom.mockReturnValue(4);
988+
989+
let zoomendHandler;
990+
mockLeafletInstance.on.mockImplementation((event, handler) => {
991+
if (event === "zoomend") {
992+
zoomendHandler = handler;
993+
}
994+
});
995+
996+
renderInstance.mapRender(mockSelf.data, mockSelf);
997+
998+
zoomInBtn.classList.remove("leaflet-disabled");
999+
zoomOutBtn.classList.remove("leaflet-disabled");
1000+
1001+
expect(document.querySelector(".leaflet-control-zoom-in")).toBe(zoomInBtn);
1002+
expect(document.querySelector(".leaflet-control-zoom-out")).toBe(
1003+
zoomOutBtn,
1004+
);
1005+
1006+
zoomendHandler();
1007+
1008+
expect(mockSelf.echarts.setOption).toHaveBeenCalledWith({
1009+
series: [{label: {show: false}}],
1010+
});
1011+
expect(zoomInBtn.classList.contains("leaflet-disabled")).toBe(false);
1012+
expect(zoomOutBtn.classList.contains("leaflet-disabled")).toBe(true);
1013+
});
1014+
1015+
test("should remove disabled class when zoom is between min and max zoom", () => {
1016+
mockLeafletInstance.getZoom.mockReturnValue(3);
1017+
mockLeafletInstance.getMinZoom.mockReturnValue(1);
1018+
mockLeafletInstance.getMaxZoom.mockReturnValue(4);
1019+
1020+
zoomInBtn.classList.add("leaflet-disabled");
1021+
zoomOutBtn.classList.add("leaflet-disabled");
1022+
1023+
let zoomendHandler;
1024+
mockLeafletInstance.on.mockImplementation((event, handler) => {
1025+
if (event === "zoomend") {
1026+
zoomendHandler = handler;
1027+
}
1028+
});
1029+
1030+
renderInstance.mapRender(mockSelf.data, mockSelf);
1031+
1032+
expect(document.querySelector(".leaflet-control-zoom-in")).toBe(zoomInBtn);
1033+
expect(document.querySelector(".leaflet-control-zoom-out")).toBe(
1034+
zoomOutBtn,
1035+
);
1036+
1037+
zoomendHandler();
1038+
1039+
expect(zoomInBtn.classList.contains("leaflet-disabled")).toBe(false);
1040+
expect(zoomOutBtn.classList.contains("leaflet-disabled")).toBe(false);
1041+
});
1042+
1043+
test("should handle float zoom values correctly", () => {
1044+
mockLeafletInstance.getZoom.mockReturnValue(3.6);
1045+
mockLeafletInstance.getMinZoom.mockReturnValue(1);
1046+
mockLeafletInstance.getMaxZoom.mockReturnValue(4);
1047+
1048+
zoomInBtn.classList.remove("leaflet-disabled");
1049+
zoomOutBtn.classList.remove("leaflet-disabled");
1050+
1051+
let zoomendHandler;
1052+
mockLeafletInstance.on.mockImplementation((event, handler) => {
1053+
if (event === "zoomend") {
1054+
zoomendHandler = handler;
1055+
}
1056+
});
1057+
renderInstance.mapRender(mockSelf.data, mockSelf);
1058+
zoomendHandler();
1059+
1060+
expect(zoomInBtn.classList.contains("leaflet-disabled")).toBe(true);
1061+
expect(zoomOutBtn.classList.contains("leaflet-disabled")).toBe(false);
1062+
});
1063+
});

0 commit comments

Comments
 (0)