mobility-toolbox-js 2.0.1-beta.13 → 2.2.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/RealtimeAPI.d.ts +281 -0
- package/api/RealtimeAPI.d.ts.map +1 -0
- package/api/RealtimeAPI.js +490 -0
- package/api/RoutingAPI.d.ts +37 -0
- package/api/RoutingAPI.d.ts.map +1 -0
- package/api/RoutingAPI.js +32 -12
- package/api/StopsAPI.d.ts +38 -0
- package/api/StopsAPI.d.ts.map +1 -0
- package/api/StopsAPI.js +33 -9
- package/api/index.d.ts +4 -0
- package/api/index.d.ts.map +1 -0
- package/api/index.js +3 -3
- package/api/typedefs.d.ts +179 -0
- package/api/typedefs.d.ts.map +1 -0
- package/api/typedefs.js +75 -0
- package/common/api/HttpAPI.d.ts +31 -0
- package/common/api/HttpAPI.d.ts.map +1 -0
- package/common/api/HttpAPI.js +54 -27
- package/common/api/WebSocketAPI.d.ts +153 -0
- package/common/api/WebSocketAPI.d.ts.map +1 -0
- package/common/api/WebSocketAPI.js +330 -164
- package/common/controls/ControlCommon.d.ts +76 -0
- package/common/controls/ControlCommon.d.ts.map +1 -0
- package/common/controls/ControlCommon.js +150 -0
- package/common/controls/CopyrightControlCommon.d.ts +13 -0
- package/common/controls/CopyrightControlCommon.d.ts.map +1 -0
- package/common/controls/CopyrightControlCommon.js +34 -0
- package/common/controls/StopFinderControlCommon.d.ts +55 -0
- package/common/controls/StopFinderControlCommon.d.ts.map +1 -0
- package/common/controls/StopFinderControlCommon.js +144 -0
- package/common/index.d.ts +3 -0
- package/common/index.d.ts.map +1 -0
- package/common/index.js +2 -4
- package/common/layers/LayerCommon.d.ts +94 -0
- package/common/layers/LayerCommon.d.ts.map +1 -0
- package/common/layers/LayerCommon.js +244 -0
- package/common/mixins/RealtimeLayerMixin.d.ts +286 -0
- package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
- package/common/mixins/RealtimeLayerMixin.js +779 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts +60 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
- package/common/mixins/UserInteractionsLayerMixin.js +241 -0
- package/common/styles/index.d.ts +5 -0
- package/common/styles/index.d.ts.map +1 -0
- package/common/styles/index.js +4 -4
- package/common/styles/realtimeDefaultStyle.d.ts +36 -0
- package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
- package/common/styles/realtimeDefaultStyle.js +275 -0
- package/common/styles/realtimeDelayStyle.d.ts +12 -0
- package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
- package/common/styles/realtimeDelayStyle.js +13 -0
- package/common/styles/realtimeHeadingStyle.d.ts +12 -0
- package/common/styles/realtimeHeadingStyle.d.ts.map +1 -0
- package/common/styles/realtimeHeadingStyle.js +85 -0
- package/common/styles/realtimeSimpleStyle.d.ts +4 -0
- package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
- package/common/styles/realtimeSimpleStyle.js +23 -0
- package/common/typedefs.d.ts +111 -0
- package/common/typedefs.d.ts.map +1 -0
- package/common/typedefs.js +52 -0
- package/common/utils/compareDepartures.d.ts +11 -0
- package/common/utils/compareDepartures.d.ts.map +1 -0
- package/common/utils/compareDepartures.js +35 -0
- package/common/utils/createCanvas.d.ts +10 -0
- package/common/utils/createCanvas.d.ts.map +1 -0
- package/common/utils/createCanvas.js +27 -0
- package/common/utils/createRealtimeFilters.d.ts +13 -0
- package/common/utils/createRealtimeFilters.d.ts.map +1 -0
- package/common/utils/createRealtimeFilters.js +74 -0
- package/common/utils/debounceDeparturesMessages.d.ts +12 -0
- package/common/utils/debounceDeparturesMessages.d.ts.map +1 -0
- package/common/utils/debounceDeparturesMessages.js +24 -0
- package/common/utils/debounceWebsocketMessages.d.ts +11 -0
- package/common/utils/debounceWebsocketMessages.d.ts.map +1 -0
- package/common/utils/debounceWebsocketMessages.js +29 -0
- package/common/utils/getLayersAsFlatArray.d.ts +3 -0
- package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
- package/common/utils/getLayersAsFlatArray.js +15 -0
- package/common/utils/getMapboxMapCopyrights.d.ts +18 -0
- package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
- package/common/utils/getMapboxMapCopyrights.js +26 -15
- package/common/utils/getMapboxRender.d.ts +7 -0
- package/common/utils/getMapboxRender.d.ts.map +1 -0
- package/common/utils/getMapboxRender.js +87 -0
- package/common/utils/getMaplibreRender.d.ts +7 -0
- package/common/utils/getMaplibreRender.d.ts.map +1 -0
- package/common/utils/getMaplibreRender.js +38 -0
- package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
- package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
- package/common/utils/getRealtimeModeSuffix.js +7 -0
- package/common/utils/getUrlWithParams.d.ts +9 -0
- package/common/utils/getUrlWithParams.d.ts.map +1 -0
- package/common/utils/getUrlWithParams.js +18 -0
- package/common/utils/getVehiclePosition.d.ts +16 -0
- package/common/utils/getVehiclePosition.d.ts.map +1 -0
- package/common/utils/getVehiclePosition.js +67 -37
- package/common/utils/index.d.ts +17 -0
- package/common/utils/index.d.ts.map +1 -0
- package/common/utils/index.js +17 -5
- package/common/utils/realtimeConfig.d.ts +49 -0
- package/common/utils/realtimeConfig.d.ts.map +1 -0
- package/common/utils/realtimeConfig.js +173 -0
- package/common/utils/removeDuplicate.d.ts +10 -0
- package/common/utils/removeDuplicate.d.ts.map +1 -0
- package/common/utils/removeDuplicate.js +12 -5
- package/common/utils/renderTrajectories.d.ts +17 -0
- package/common/utils/renderTrajectories.d.ts.map +1 -0
- package/common/utils/renderTrajectories.js +110 -0
- package/common/utils/sortAndFilterDepartures.d.ts +16 -0
- package/common/utils/sortAndFilterDepartures.d.ts.map +1 -0
- package/common/utils/sortAndFilterDepartures.js +58 -0
- package/common/utils/sortByDelay.d.ts +3 -0
- package/common/utils/sortByDelay.d.ts.map +1 -0
- package/common/utils/sortByDelay.js +17 -15
- package/common/utils/timeUtils.d.ts +24 -0
- package/common/utils/timeUtils.d.ts.map +1 -0
- package/common/utils/timeUtils.js +34 -15
- package/iife.d.ts +3 -0
- package/iife.d.ts.map +1 -0
- package/iife.js +5 -0
- package/index.d.ts +10 -0
- package/index.d.ts.map +1 -0
- package/index.js +8 -6
- package/mapbox/controls/CopyrightControl.d.ts +29 -0
- package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
- package/mapbox/controls/CopyrightControl.js +44 -25
- package/mapbox/controls/index.d.ts +2 -0
- package/mapbox/controls/index.d.ts.map +1 -0
- package/mapbox/controls/index.js +2 -1
- package/mapbox/index.d.ts +6 -0
- package/mapbox/index.d.ts.map +1 -0
- package/mapbox/index.js +5 -4
- package/mapbox/layers/Layer.d.ts +59 -0
- package/mapbox/layers/Layer.d.ts.map +1 -0
- package/mapbox/layers/Layer.js +99 -55
- package/mapbox/layers/RealtimeLayer.d.ts +181 -0
- package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
- package/mapbox/layers/RealtimeLayer.js +276 -0
- package/mapbox/layers/index.d.ts +3 -0
- package/mapbox/layers/index.d.ts.map +1 -0
- package/mapbox/layers/index.js +2 -2
- package/mapbox/utils/getMercatorResolution.d.ts +9 -0
- package/mapbox/utils/getMercatorResolution.d.ts.map +1 -0
- package/mapbox/utils/getMercatorResolution.js +18 -0
- package/mapbox/utils/getSourceCoordinates.d.ts +9 -0
- package/mapbox/utils/getSourceCoordinates.d.ts.map +1 -0
- package/mapbox/utils/getSourceCoordinates.js +27 -0
- package/mapbox/utils/index.d.ts +3 -0
- package/mapbox/utils/index.d.ts.map +1 -0
- package/mapbox/utils/index.js +2 -0
- package/mbt.js +26074 -16501
- package/mbt.js.map +4 -4
- package/mbt.min.js +205 -126
- package/mbt.min.js.map +4 -4
- package/ol/controls/CopyrightControl.d.ts +31 -0
- package/ol/controls/CopyrightControl.d.ts.map +1 -0
- package/ol/controls/CopyrightControl.js +62 -36
- package/ol/controls/RoutingControl.d.ts +193 -0
- package/ol/controls/RoutingControl.d.ts.map +1 -0
- package/ol/controls/RoutingControl.js +601 -357
- package/ol/controls/StopFinderControl.d.ts +30 -0
- package/ol/controls/StopFinderControl.d.ts.map +1 -0
- package/ol/controls/StopFinderControl.js +30 -8
- package/ol/controls/index.d.ts +4 -0
- package/ol/controls/index.d.ts.map +1 -0
- package/ol/controls/index.js +3 -3
- package/ol/index.d.ts +6 -0
- package/ol/index.d.ts.map +1 -0
- package/ol/index.js +5 -5
- package/ol/layers/Layer.d.ts +86 -0
- package/ol/layers/Layer.d.ts.map +1 -0
- package/ol/layers/Layer.js +163 -77
- package/ol/layers/MapGlLayer.d.ts +67 -0
- package/ol/layers/MapGlLayer.d.ts.map +1 -0
- package/ol/layers/MapGlLayer.js +218 -0
- package/ol/layers/MapboxLayer.d.ts +50 -0
- package/ol/layers/MapboxLayer.d.ts.map +1 -0
- package/ol/layers/MapboxLayer.js +99 -193
- package/ol/layers/MapboxStyleLayer.d.ts +129 -0
- package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
- package/ol/layers/MapboxStyleLayer.js +362 -171
- package/ol/layers/MaplibreLayer.d.ts +28 -0
- package/ol/layers/MaplibreLayer.d.ts.map +1 -0
- package/ol/layers/MaplibreLayer.js +30 -135
- package/ol/layers/RealtimeLayer.d.ts +202 -0
- package/ol/layers/RealtimeLayer.d.ts.map +1 -0
- package/ol/layers/RealtimeLayer.js +340 -0
- package/ol/layers/RoutingLayer.d.ts +34 -0
- package/ol/layers/RoutingLayer.d.ts.map +1 -0
- package/ol/layers/RoutingLayer.js +72 -48
- package/ol/layers/VectorLayer.d.ts +25 -0
- package/ol/layers/VectorLayer.d.ts.map +1 -0
- package/ol/layers/VectorLayer.js +34 -18
- package/ol/layers/WMSLayer.d.ts +42 -0
- package/ol/layers/WMSLayer.d.ts.map +1 -0
- package/ol/layers/WMSLayer.js +84 -34
- package/ol/layers/index.d.ts +9 -0
- package/ol/layers/index.d.ts.map +1 -0
- package/ol/layers/index.js +8 -8
- package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryDelayStyle.js +26 -24
- package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
- package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryStyle.js +40 -39
- package/ol/styles/index.d.ts +3 -0
- package/ol/styles/index.d.ts.map +1 -0
- package/ol/styles/index.js +2 -2
- package/package.json +81 -133
- package/setupTests.d.ts +2 -0
- package/setupTests.d.ts.map +1 -0
- package/setupTests.js +26 -0
- package/types/common.d.ts +122 -0
- package/types/index.d.ts +13 -0
- package/types/realtime.d.ts +320 -0
- package/types/routing.d.ts +206 -0
- package/types/stops.d.ts +143 -0
- package/README.md +0 -23
- package/api/RoutingAPI.test.js +0 -25
- package/api/StopsAPI.test.js +0 -22
- package/api/TralisAPI.js +0 -359
- package/api/TralisAPI.test.js +0 -67
- package/api/TralisAPIUtils.js +0 -43
- package/common/Tracker.js +0 -93
- package/common/api/HttpAPI.test.js +0 -50
- package/common/api/WebSocketAPI.test.js +0 -311
- package/common/controls/Control.js +0 -81
- package/common/controls/Control.test.js +0 -87
- package/common/layers/Layer.js +0 -213
- package/common/layers/Layer.test.js +0 -526
- package/common/mixins/CopyrightMixin.js +0 -24
- package/common/mixins/SearchMixin.js +0 -110
- package/common/mixins/TralisLayerMixin.js +0 -479
- package/common/styles/trackerDefaultStyle.js +0 -197
- package/common/styles/trackerDelayStyle.js +0 -8
- package/common/styles/trackerSimpleStyle.js +0 -18
- package/common/trackerConfig.js +0 -152
- package/common/trackerConfig.test.js +0 -23
- package/common/utils/createTrackerFilters.js +0 -56
- package/common/utils/createTrackerFilters.test.js +0 -79
- package/common/utils/getMapboxMapCopyrights.test.js +0 -40
- package/common/utils/getMapboxStyleUrl.js +0 -22
- package/common/utils/removeDuplicate.test.js +0 -19
- package/common/utils/timeUtils.test.js +0 -10
- package/mapbox/layers/Layer.test.js +0 -182
- package/mapbox/layers/TralisLayer.js +0 -182
- package/mapbox/layers/TralisLayer.test.js +0 -38
- package/mapbox/utils.js +0 -32
- package/ol/controls/CopyrightControl.test.js +0 -165
- package/ol/controls/RoutingControl.test.js +0 -151
- package/ol/controls/StopFinderControl.test.js +0 -48
- package/ol/layers/Layer.test.js +0 -174
- package/ol/layers/MapboxLayer.test.js +0 -160
- package/ol/layers/MapboxStyleLayer.test.js +0 -231
- package/ol/layers/RoutingLayer.test.js +0 -40
- package/ol/layers/TralisLayer.js +0 -185
- package/ol/layers/TralisLayer.test.js +0 -79
- package/ol/layers/VectorLayer.test.js +0 -87
- package/ol/layers/WMSLayer.test.js +0 -76
package/common/Tracker.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { compose, apply, create } from "ol/transform";
|
|
2
|
-
import getVehiclePosition from "./utils/getVehiclePosition";
|
|
3
|
-
export default class Tracker {
|
|
4
|
-
constructor(options) {
|
|
5
|
-
this.style = options.style;
|
|
6
|
-
this.canvas = options.canvas || document.createElement("canvas");
|
|
7
|
-
}
|
|
8
|
-
renderTrajectories(trajectories, viewState, options) {
|
|
9
|
-
const {
|
|
10
|
-
time = Date.now(),
|
|
11
|
-
size = [],
|
|
12
|
-
center,
|
|
13
|
-
resolution,
|
|
14
|
-
rotation = 0,
|
|
15
|
-
pixelRatio
|
|
16
|
-
} = viewState;
|
|
17
|
-
const {
|
|
18
|
-
noInterpolate = false,
|
|
19
|
-
hoverVehicleId,
|
|
20
|
-
selectedVehicleId
|
|
21
|
-
} = options;
|
|
22
|
-
const { canvas } = this;
|
|
23
|
-
const context = canvas.getContext("2d");
|
|
24
|
-
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
25
|
-
const [width, height] = size;
|
|
26
|
-
if (width && height && (canvas.width !== width || canvas.height !== height)) {
|
|
27
|
-
[canvas.width, canvas.height] = [width * pixelRatio, height * pixelRatio];
|
|
28
|
-
}
|
|
29
|
-
const coordinateToPixelTransform = compose(create(), size[0] / 2, size[1] / 2, 1 / resolution, -1 / resolution, -rotation, -center[0], -center[1]);
|
|
30
|
-
if (canvas.style) {
|
|
31
|
-
canvas.style.width = `${canvas.width / pixelRatio}px`;
|
|
32
|
-
canvas.style.height = `${canvas.height / pixelRatio}px`;
|
|
33
|
-
}
|
|
34
|
-
let hoverVehicleImg;
|
|
35
|
-
let hoverVehiclePx;
|
|
36
|
-
let hoverVehicleWidth;
|
|
37
|
-
let hoverVehicleHeight;
|
|
38
|
-
let selectedVehicleImg;
|
|
39
|
-
let selectedVehiclePx;
|
|
40
|
-
let selectedVehicleWidth;
|
|
41
|
-
let selectedVehicleHeight;
|
|
42
|
-
let nbRendered = 0;
|
|
43
|
-
for (let i = trajectories.length - 1; i >= 0; i -= 1) {
|
|
44
|
-
const trajectory = trajectories[i];
|
|
45
|
-
const { train_id: id, timeOffset } = trajectory.properties;
|
|
46
|
-
const { coord, rotation: rotationIcon } = getVehiclePosition(time - (timeOffset || 0), trajectory, noInterpolate);
|
|
47
|
-
trajectories[i].properties.coordinate = coord;
|
|
48
|
-
trajectories[i].properties.rotation = rotationIcon;
|
|
49
|
-
if (!coord) {
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
let px = apply(coordinateToPixelTransform, [...coord]);
|
|
53
|
-
if (!px) {
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
px = px.map((p) => p * pixelRatio);
|
|
57
|
-
if (px[0] < 0 || px[0] > canvas.width || px[1] < 0 || px[1] > canvas.height) {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
const vehicleImg = this.style(trajectory, viewState, options);
|
|
61
|
-
if (!vehicleImg) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
nbRendered += 1;
|
|
65
|
-
const imgWidth = vehicleImg.width;
|
|
66
|
-
const imgHeight = vehicleImg.height;
|
|
67
|
-
if (hoverVehicleId !== id && selectedVehicleId !== id) {
|
|
68
|
-
context.drawImage(vehicleImg, px[0] - imgWidth / 2, px[1] - imgHeight / 2, imgWidth, imgHeight);
|
|
69
|
-
}
|
|
70
|
-
if (hoverVehicleId && hoverVehicleId === id) {
|
|
71
|
-
hoverVehicleImg = vehicleImg;
|
|
72
|
-
hoverVehiclePx = px;
|
|
73
|
-
hoverVehicleWidth = imgWidth;
|
|
74
|
-
hoverVehicleHeight = imgHeight;
|
|
75
|
-
}
|
|
76
|
-
if (selectedVehicleId && selectedVehicleId === id) {
|
|
77
|
-
selectedVehicleImg = vehicleImg;
|
|
78
|
-
selectedVehiclePx = px;
|
|
79
|
-
selectedVehicleWidth = imgWidth;
|
|
80
|
-
selectedVehicleHeight = imgHeight;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (selectedVehicleImg) {
|
|
84
|
-
context.drawImage(selectedVehicleImg, selectedVehiclePx[0] - selectedVehicleWidth / 2, selectedVehiclePx[1] - selectedVehicleHeight / 2, selectedVehicleWidth, selectedVehicleHeight);
|
|
85
|
-
}
|
|
86
|
-
if (hoverVehicleImg) {
|
|
87
|
-
context.drawImage(hoverVehicleImg, hoverVehiclePx[0] - hoverVehicleWidth / 2, hoverVehiclePx[1] - hoverVehicleHeight / 2, hoverVehicleWidth, hoverVehicleHeight);
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
nbTrajectoriesRendered: nbRendered
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import fetch from "jest-fetch-mock";
|
|
2
|
-
import API from "./HttpAPI";
|
|
3
|
-
let api;
|
|
4
|
-
describe("HttpAPI", () => {
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
global.fetch = fetch;
|
|
7
|
-
fetch.resetMocks();
|
|
8
|
-
api = new API({ url: "https://foo.ch", apiKey: "apiKey" });
|
|
9
|
-
});
|
|
10
|
-
describe("#fetch", () => {
|
|
11
|
-
test("should success", () => {
|
|
12
|
-
fetch.mockResponseOnce(JSON.stringify({ foo: "bar" }));
|
|
13
|
-
return api.fetch("/path", {
|
|
14
|
-
q: "Bern",
|
|
15
|
-
fooUndefined: void 0,
|
|
16
|
-
fooNull: null,
|
|
17
|
-
fooEmpty: ""
|
|
18
|
-
}).then((response) => {
|
|
19
|
-
expect(fetch.mock.calls[0][0]).toEqual("https://foo.ch/path?fooEmpty=&key=apiKey&q=Bern");
|
|
20
|
-
expect(response).toEqual({ foo: "bar" });
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
describe("should display error message", () => {
|
|
24
|
-
test("reject error", (done) => {
|
|
25
|
-
fetch.mockRejectOnce(new Error("Fake error message"));
|
|
26
|
-
return api.fetch().catch((err) => {
|
|
27
|
-
expect(err.name).toEqual("Error");
|
|
28
|
-
expect(err.message).toEqual("Fake error message");
|
|
29
|
-
done();
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
test("if the response is invalid json", (done) => {
|
|
33
|
-
fetch.mockResponseOnce("invalid json");
|
|
34
|
-
api.fetch().catch((err) => {
|
|
35
|
-
expect(err.name).toEqual("FetchError");
|
|
36
|
-
expect(err.message).toEqual("invalid json response body at reason: Unexpected token i in JSON at position 0");
|
|
37
|
-
done();
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
test("if the response contains an error message", (done) => {
|
|
41
|
-
fetch.mockResponseOnce('{"error":"foo2"}');
|
|
42
|
-
api.fetch().catch((err) => {
|
|
43
|
-
expect(err.name).toEqual("Error");
|
|
44
|
-
expect(err.message).toEqual("foo2");
|
|
45
|
-
done();
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
import WS from "jest-websocket-mock";
|
|
2
|
-
import WebSocketAPI from "./WebSocketAPI";
|
|
3
|
-
describe("WebSocketAPI", () => {
|
|
4
|
-
describe("#constructor", () => {
|
|
5
|
-
let server;
|
|
6
|
-
let server2;
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
server = new WS(`ws://foo:1234`);
|
|
9
|
-
server2 = new WS(`ws://foo:12345`);
|
|
10
|
-
});
|
|
11
|
-
afterEach(() => {
|
|
12
|
-
server.close();
|
|
13
|
-
server2.close();
|
|
14
|
-
WS.clean();
|
|
15
|
-
});
|
|
16
|
-
describe("#constructor", () => {
|
|
17
|
-
test("doesn't connect.", async () => {
|
|
18
|
-
const client = new WebSocketAPI();
|
|
19
|
-
expect(client.websocket).toBe();
|
|
20
|
-
expect(client.closed).toBe(false);
|
|
21
|
-
expect(client.closing).toBe(false);
|
|
22
|
-
expect(client.connecting).toBe(false);
|
|
23
|
-
expect(client.open).toBe(false);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
describe("#close", () => {
|
|
27
|
-
test("should close the websocket and clear some property", async () => {
|
|
28
|
-
const client = new WebSocketAPI();
|
|
29
|
-
const subsc2 = {
|
|
30
|
-
params: "foo",
|
|
31
|
-
cb: () => {
|
|
32
|
-
},
|
|
33
|
-
errorCb: () => {
|
|
34
|
-
},
|
|
35
|
-
quiet: false
|
|
36
|
-
};
|
|
37
|
-
client.subscriptions = [subsc2];
|
|
38
|
-
client.messagesOnOpen = ["GET foo"];
|
|
39
|
-
client.connect(`ws://foo:1234`);
|
|
40
|
-
client.websocket.addEventListener = jest.fn();
|
|
41
|
-
client.websocket.removeEventListener = jest.fn();
|
|
42
|
-
client.websocket.close = jest.fn();
|
|
43
|
-
await server.connected;
|
|
44
|
-
expect(client.websocket).toBeDefined();
|
|
45
|
-
expect(client.messagesOnOpen).toEqual(["GET foo"]);
|
|
46
|
-
client.close();
|
|
47
|
-
expect(client.messagesOnOpen).toEqual([]);
|
|
48
|
-
expect(client.websocket).toBeNull();
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
describe("#connect", () => {
|
|
52
|
-
test("create a new WebSocket.", async () => {
|
|
53
|
-
const client = new WebSocketAPI();
|
|
54
|
-
client.connect(`ws://foo:1234`);
|
|
55
|
-
await server.connected;
|
|
56
|
-
client.send("hello");
|
|
57
|
-
await expect(server).toReceiveMessage("hello");
|
|
58
|
-
expect(server).toHaveReceivedMessages(["hello"]);
|
|
59
|
-
});
|
|
60
|
-
test("close previous connection.", async () => {
|
|
61
|
-
const client = new WebSocketAPI();
|
|
62
|
-
client.connect(`ws://foo:1234`);
|
|
63
|
-
await server.connected;
|
|
64
|
-
expect(client.websocket).toBeDefined();
|
|
65
|
-
const old = client.websocket;
|
|
66
|
-
expect(old.readyState).toBe(WebSocket.OPEN);
|
|
67
|
-
client.connect(`ws://foo:12345`);
|
|
68
|
-
expect(old.readyState).toBe(WebSocket.CLOSING);
|
|
69
|
-
expect(client.websocket.readyState).toBe(WebSocket.CONNECTING);
|
|
70
|
-
});
|
|
71
|
-
test("call onOpen function", async () => {
|
|
72
|
-
const onOpen = jest.fn();
|
|
73
|
-
const client = new WebSocketAPI();
|
|
74
|
-
client.subscribe = jest.fn();
|
|
75
|
-
client.connect(`ws://foo:1234`, onOpen);
|
|
76
|
-
await server.connected;
|
|
77
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
78
|
-
expect(onOpen).toHaveBeenCalledTimes(1);
|
|
79
|
-
});
|
|
80
|
-
test("subscribe previous subscriptions on open (quiet or not)", async () => {
|
|
81
|
-
const client = new WebSocketAPI();
|
|
82
|
-
client.subscribe = jest.fn();
|
|
83
|
-
client.send = jest.fn();
|
|
84
|
-
const subsc = {
|
|
85
|
-
params: "foo",
|
|
86
|
-
cb: () => {
|
|
87
|
-
},
|
|
88
|
-
errorCb: () => {
|
|
89
|
-
},
|
|
90
|
-
quiet: true
|
|
91
|
-
};
|
|
92
|
-
const subsc2 = {
|
|
93
|
-
params: "foo",
|
|
94
|
-
cb: () => {
|
|
95
|
-
},
|
|
96
|
-
errorCb: () => {
|
|
97
|
-
},
|
|
98
|
-
quiet: false
|
|
99
|
-
};
|
|
100
|
-
client.subscriptions = [subsc, subsc2];
|
|
101
|
-
client.connect(`ws://foo:1234`);
|
|
102
|
-
await server.connected;
|
|
103
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
104
|
-
expect(client.subscribe).toHaveBeenCalledTimes(2);
|
|
105
|
-
client.subscribe.mockReset();
|
|
106
|
-
client.connect(`ws://foo:12345`);
|
|
107
|
-
await server2.connected;
|
|
108
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
109
|
-
expect(client.subscribe).toHaveBeenCalledTimes(2);
|
|
110
|
-
expect(client.subscribe).toHaveBeenCalledWith(subsc.params, subsc.cb, subsc.errorCb, subsc.quiet);
|
|
111
|
-
});
|
|
112
|
-
test("send GET and SUB for not quiet previous subscriptions", async () => {
|
|
113
|
-
const client = new WebSocketAPI();
|
|
114
|
-
client.send = jest.fn();
|
|
115
|
-
const subsc = {
|
|
116
|
-
params: { channel: "foo" },
|
|
117
|
-
cb: () => {
|
|
118
|
-
},
|
|
119
|
-
errorCb: () => {
|
|
120
|
-
},
|
|
121
|
-
quiet: false
|
|
122
|
-
};
|
|
123
|
-
client.subscriptions = [subsc];
|
|
124
|
-
client.connect(`ws://foo:1234`);
|
|
125
|
-
client.websocket.addEventListener = jest.fn();
|
|
126
|
-
client.websocket.removeEventListener = jest.fn();
|
|
127
|
-
await server.connected;
|
|
128
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
129
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
130
|
-
expect(client.send.mock.calls[0]).toEqual(["GET foo"]);
|
|
131
|
-
expect(client.send.mock.calls[1]).toEqual(["SUB foo"]);
|
|
132
|
-
client.send.mockReset();
|
|
133
|
-
client.connect(`ws://foo:12345`);
|
|
134
|
-
client.websocket.addEventListener = jest.fn();
|
|
135
|
-
client.websocket.removeEventListener = jest.fn();
|
|
136
|
-
await server2.connected;
|
|
137
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
138
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
139
|
-
expect(client.send.mock.calls[0]).toEqual(["GET foo"]);
|
|
140
|
-
expect(client.send.mock.calls[1]).toEqual(["SUB foo"]);
|
|
141
|
-
});
|
|
142
|
-
test("doesn send GET and SUB for quiet previous subscriptions", async () => {
|
|
143
|
-
const client = new WebSocketAPI();
|
|
144
|
-
client.send = jest.fn();
|
|
145
|
-
const subsc = {
|
|
146
|
-
params: { channel: "foo" },
|
|
147
|
-
cb: () => {
|
|
148
|
-
},
|
|
149
|
-
errorCb: () => {
|
|
150
|
-
},
|
|
151
|
-
quiet: true
|
|
152
|
-
};
|
|
153
|
-
client.subscriptions = [subsc];
|
|
154
|
-
client.connect(`ws://foo:1234`);
|
|
155
|
-
client.websocket.addEventListener = jest.fn();
|
|
156
|
-
client.websocket.removeEventListener = jest.fn();
|
|
157
|
-
await server.connected;
|
|
158
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
159
|
-
expect(client.send).toHaveBeenCalledTimes(0);
|
|
160
|
-
client.send.mockReset();
|
|
161
|
-
client.connect(`ws://foo:12345`);
|
|
162
|
-
client.websocket.addEventListener = jest.fn();
|
|
163
|
-
client.websocket.removeEventListener = jest.fn();
|
|
164
|
-
await server2.connected;
|
|
165
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
166
|
-
expect(client.send).toHaveBeenCalledTimes(0);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
describe("#subscribe", () => {
|
|
170
|
-
test("adds subscription to subscriptions array", async () => {
|
|
171
|
-
const client = new WebSocketAPI();
|
|
172
|
-
client.connect(`ws://foo:1234`);
|
|
173
|
-
await server.connected;
|
|
174
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
175
|
-
const cb = jest.fn();
|
|
176
|
-
const errorCb = jest.fn();
|
|
177
|
-
client.subscribe(params, cb, errorCb);
|
|
178
|
-
expect(client.subscriptions.length).toBe(1);
|
|
179
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
180
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
181
|
-
expect(client.subscriptions[0].errorCb).toBe(errorCb);
|
|
182
|
-
expect(client.subscriptions[0].quiet).toBe(false);
|
|
183
|
-
const obj = { source: "bar baz", client_reference: "id" };
|
|
184
|
-
server.send(JSON.stringify(obj));
|
|
185
|
-
expect(cb).toHaveBeenCalledTimes(1);
|
|
186
|
-
expect(cb).toHaveBeenCalledWith(obj);
|
|
187
|
-
});
|
|
188
|
-
test("doesn't duplicate subscriptions", async () => {
|
|
189
|
-
const client = new WebSocketAPI();
|
|
190
|
-
client.connect(`ws://foo:1234`);
|
|
191
|
-
await server.connected;
|
|
192
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
193
|
-
const cb = jest.fn();
|
|
194
|
-
const errorCb = jest.fn();
|
|
195
|
-
client.subscribe(params, cb, errorCb, true);
|
|
196
|
-
client.subscribe(params, cb, errorCb, true);
|
|
197
|
-
expect(client.subscriptions.length).toBe(1);
|
|
198
|
-
const obj = { source: "bar baz", client_reference: "id" };
|
|
199
|
-
server.send(JSON.stringify(obj));
|
|
200
|
-
expect(cb).toHaveBeenCalledTimes(1);
|
|
201
|
-
expect(cb).toHaveBeenCalledWith(obj);
|
|
202
|
-
});
|
|
203
|
-
test("send GET and SUB requests.", async () => {
|
|
204
|
-
const client = new WebSocketAPI();
|
|
205
|
-
client.connect(`ws://foo:1234`);
|
|
206
|
-
client.send = jest.fn();
|
|
207
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
208
|
-
const cb = jest.fn();
|
|
209
|
-
const errorCb = jest.fn();
|
|
210
|
-
client.subscribe(params, cb, errorCb);
|
|
211
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
212
|
-
expect(client.send).toHaveBeenCalledWith("GET bar baz id");
|
|
213
|
-
expect(client.send).toHaveBeenCalledWith("SUB bar baz id");
|
|
214
|
-
client.send.mockRestore();
|
|
215
|
-
});
|
|
216
|
-
test("should register callback without sending GET and SUB requests (quiet=true).", async () => {
|
|
217
|
-
const client = new WebSocketAPI();
|
|
218
|
-
client.connect(`ws://foo:1234`);
|
|
219
|
-
await server.connected;
|
|
220
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
221
|
-
const cb = jest.fn();
|
|
222
|
-
const errorCb = jest.fn();
|
|
223
|
-
client.send = jest.fn();
|
|
224
|
-
client.subscribe(params, cb, errorCb, true);
|
|
225
|
-
expect(client.subscriptions.length).toBe(1);
|
|
226
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
227
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
228
|
-
expect(client.subscriptions[0].errorCb).toBe(errorCb);
|
|
229
|
-
expect(client.subscriptions[0].quiet).toBe(true);
|
|
230
|
-
expect(client.send).toBeCalledTimes(0);
|
|
231
|
-
client.send.mockRestore();
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
describe("#unsubscribe", () => {
|
|
235
|
-
test("should only unsubscribe the subscription using the good cb", async () => {
|
|
236
|
-
const client = new WebSocketAPI();
|
|
237
|
-
client.connect(`ws://foo:1234`);
|
|
238
|
-
await server.connected;
|
|
239
|
-
const params = { channel: "foo", id: "id" };
|
|
240
|
-
const cb = jest.fn();
|
|
241
|
-
const cb2 = jest.fn();
|
|
242
|
-
client.subscribe(params, cb);
|
|
243
|
-
client.subscribe(params, cb2);
|
|
244
|
-
expect(client.subscriptions.length).toBe(2);
|
|
245
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
246
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
247
|
-
expect(client.subscriptions[1].params).toBe(params);
|
|
248
|
-
expect(client.subscriptions[1].cb).toBe(cb2);
|
|
249
|
-
client.unsubscribe("foo", cb);
|
|
250
|
-
expect(client.subscriptions.length).toBe(1);
|
|
251
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
252
|
-
expect(cb2).toHaveBeenCalledTimes(0);
|
|
253
|
-
const obj = { source: "foo", client_reference: "id" };
|
|
254
|
-
server.send(JSON.stringify(obj));
|
|
255
|
-
expect(cb2).toHaveBeenCalledTimes(1);
|
|
256
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
257
|
-
});
|
|
258
|
-
test("should unsubscribe all subscriptions related to a channel", () => {
|
|
259
|
-
const client = new WebSocketAPI();
|
|
260
|
-
client.connect(`ws://foo:1234`);
|
|
261
|
-
client.websocket.removeEventListener = jest.fn();
|
|
262
|
-
client.websocket.addEventListener = jest.fn();
|
|
263
|
-
const params = { channel: "foo" };
|
|
264
|
-
const params2 = { channel: "bar" };
|
|
265
|
-
const cb = jest.fn();
|
|
266
|
-
const cb2 = jest.fn();
|
|
267
|
-
client.subscribe(params, cb);
|
|
268
|
-
client.subscribe(params, cb);
|
|
269
|
-
client.subscribe(params, cb);
|
|
270
|
-
client.subscribe(params, cb2);
|
|
271
|
-
client.subscribe(params2, cb2);
|
|
272
|
-
expect(client.subscriptions.length).toBe(3);
|
|
273
|
-
expect(client.websocket.removeEventListener).toBeCalledTimes(2);
|
|
274
|
-
expect(client.websocket.addEventListener.mock.calls.filter((c) => c[0] === "message").length).toBe(5);
|
|
275
|
-
client.unsubscribe("foo");
|
|
276
|
-
expect(client.subscriptions.length).toBe(1);
|
|
277
|
-
expect(client.subscriptions[0].params).toBe(params2);
|
|
278
|
-
expect(client.subscriptions[0].cb).toBe(cb2);
|
|
279
|
-
});
|
|
280
|
-
test("send DEL when there is no more unquiet subscriptions on the channel", async () => {
|
|
281
|
-
const client = new WebSocketAPI();
|
|
282
|
-
client.connect(`ws://foo:1234`);
|
|
283
|
-
await server.connected;
|
|
284
|
-
client.send = jest.fn();
|
|
285
|
-
client.websocket.removeEventListener = jest.fn();
|
|
286
|
-
client.websocket.addEventListener = jest.fn();
|
|
287
|
-
const params = { channel: "foo" };
|
|
288
|
-
const cb = jest.fn();
|
|
289
|
-
client.subscribe(params, cb);
|
|
290
|
-
expect(client.send).toHaveBeenCalledWith("GET foo");
|
|
291
|
-
expect(client.send).toHaveBeenCalledWith("SUB foo");
|
|
292
|
-
client.unsubscribe("foo");
|
|
293
|
-
expect(client.send).toHaveBeenCalledWith("DEL foo");
|
|
294
|
-
});
|
|
295
|
-
test("doesn't send DEL when we unsubscribe a quiet channel", () => {
|
|
296
|
-
const client = new WebSocketAPI();
|
|
297
|
-
client.connect(`ws://foo:1234`);
|
|
298
|
-
client.send = jest.fn();
|
|
299
|
-
client.websocket.removeEventListener = jest.fn();
|
|
300
|
-
client.websocket.addEventListener = jest.fn();
|
|
301
|
-
const params = { channel: "foo" };
|
|
302
|
-
const cb = jest.fn();
|
|
303
|
-
client.subscribe(params, cb, null, true);
|
|
304
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
305
|
-
client.unsubscribe("foo");
|
|
306
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
307
|
-
client.send.mockRestore();
|
|
308
|
-
});
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import BaseObject from "ol/Object";
|
|
2
|
-
class Control extends BaseObject {
|
|
3
|
-
constructor(options = {}) {
|
|
4
|
-
super(options);
|
|
5
|
-
this.defineProperties(options);
|
|
6
|
-
const { active } = {
|
|
7
|
-
active: options.active !== false,
|
|
8
|
-
...options
|
|
9
|
-
};
|
|
10
|
-
this.active = active;
|
|
11
|
-
}
|
|
12
|
-
defineProperties(options) {
|
|
13
|
-
const { target, element, render } = {
|
|
14
|
-
...options
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperties(this, {
|
|
17
|
-
active: {
|
|
18
|
-
get: () => this.get("active"),
|
|
19
|
-
set: (newActive) => {
|
|
20
|
-
this.set("active", newActive);
|
|
21
|
-
if (newActive) {
|
|
22
|
-
this.activate();
|
|
23
|
-
} else {
|
|
24
|
-
this.deactivate();
|
|
25
|
-
}
|
|
26
|
-
this.render();
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
map: {
|
|
30
|
-
get: () => this.get("map"),
|
|
31
|
-
set: (map) => {
|
|
32
|
-
if (this.map && this.element && this.element.parentNode) {
|
|
33
|
-
this.element.parentNode.removeChild(this.element);
|
|
34
|
-
}
|
|
35
|
-
this.deactivate();
|
|
36
|
-
this.set("map", map);
|
|
37
|
-
if (this.map) {
|
|
38
|
-
const targett = this.target || this.map.getTargetElement && this.map.getTargetElement() || this.map.getContainer && this.map.getContainer();
|
|
39
|
-
if (!this.element) {
|
|
40
|
-
this.createDefaultElement();
|
|
41
|
-
}
|
|
42
|
-
if (this.element) {
|
|
43
|
-
targett.appendChild(this.element);
|
|
44
|
-
}
|
|
45
|
-
if (this.active) {
|
|
46
|
-
this.activate();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
this.render();
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
target: {
|
|
53
|
-
value: target
|
|
54
|
-
},
|
|
55
|
-
element: {
|
|
56
|
-
value: element,
|
|
57
|
-
writable: true
|
|
58
|
-
},
|
|
59
|
-
render: {
|
|
60
|
-
value: render || this.render,
|
|
61
|
-
writable: true
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
attachToMap(map) {
|
|
66
|
-
this.map = map;
|
|
67
|
-
}
|
|
68
|
-
detachFromMap() {
|
|
69
|
-
this.map = null;
|
|
70
|
-
}
|
|
71
|
-
activate() {
|
|
72
|
-
this.deactivate();
|
|
73
|
-
}
|
|
74
|
-
deactivate() {
|
|
75
|
-
}
|
|
76
|
-
render() {
|
|
77
|
-
}
|
|
78
|
-
createDefaultElement() {
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
export default Control;
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import Control from "./Control";
|
|
2
|
-
describe("Control", () => {
|
|
3
|
-
test("should be activated by default", () => {
|
|
4
|
-
const control = new Control();
|
|
5
|
-
expect(control.active).toBe(true);
|
|
6
|
-
});
|
|
7
|
-
test("should not be activated if set to false in the options", () => {
|
|
8
|
-
const control = new Control({ active: false });
|
|
9
|
-
expect(control.active).toBe(false);
|
|
10
|
-
});
|
|
11
|
-
test("should call activate/deactivate when active is set to true/false", () => {
|
|
12
|
-
const control = new Control();
|
|
13
|
-
const spy1 = jest.spyOn(control, "activate");
|
|
14
|
-
const spy2 = jest.spyOn(control, "deactivate");
|
|
15
|
-
const spy3 = jest.spyOn(control, "render");
|
|
16
|
-
control.active = false;
|
|
17
|
-
expect(spy1).toBeCalledTimes(0);
|
|
18
|
-
expect(spy2).toBeCalledTimes(1);
|
|
19
|
-
expect(spy3).toBeCalledTimes(1);
|
|
20
|
-
control.active = true;
|
|
21
|
-
expect(spy1).toBeCalledTimes(1);
|
|
22
|
-
expect(spy2).toBeCalledTimes(2);
|
|
23
|
-
expect(spy3).toBeCalledTimes(2);
|
|
24
|
-
});
|
|
25
|
-
test("should append/remove the element to the map container when map is set", () => {
|
|
26
|
-
const element = document.createElement("div");
|
|
27
|
-
const target = document.createElement("div");
|
|
28
|
-
const control = new Control({
|
|
29
|
-
element
|
|
30
|
-
});
|
|
31
|
-
const spy1 = jest.spyOn(control, "activate");
|
|
32
|
-
const spy2 = jest.spyOn(control, "deactivate");
|
|
33
|
-
const spy3 = jest.spyOn(control, "render");
|
|
34
|
-
control.map = {
|
|
35
|
-
getContainer() {
|
|
36
|
-
return target;
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
expect(target.childNodes[0]).toBe(element);
|
|
40
|
-
expect(spy1).toBeCalledTimes(1);
|
|
41
|
-
expect(spy2).toBeCalledTimes(2);
|
|
42
|
-
expect(spy3).toBeCalledTimes(1);
|
|
43
|
-
control.detachFromMap();
|
|
44
|
-
expect(target.childNodes[0]).toBe();
|
|
45
|
-
expect(spy1).toBeCalledTimes(1);
|
|
46
|
-
expect(spy2).toBeCalledTimes(3);
|
|
47
|
-
expect(spy3).toBeCalledTimes(2);
|
|
48
|
-
});
|
|
49
|
-
test("should append/remove the element to the target property when map is set", () => {
|
|
50
|
-
const element = document.createElement("div");
|
|
51
|
-
const target = document.createElement("div");
|
|
52
|
-
const control = new Control({
|
|
53
|
-
target,
|
|
54
|
-
element
|
|
55
|
-
});
|
|
56
|
-
const spy1 = jest.spyOn(control, "activate");
|
|
57
|
-
const spy2 = jest.spyOn(control, "deactivate");
|
|
58
|
-
const spy3 = jest.spyOn(control, "render");
|
|
59
|
-
control.map = {};
|
|
60
|
-
expect(target.childNodes[0]).toBe(element);
|
|
61
|
-
expect(spy1).toBeCalledTimes(1);
|
|
62
|
-
expect(spy2).toBeCalledTimes(2);
|
|
63
|
-
expect(spy3).toBeCalledTimes(1);
|
|
64
|
-
control.detachFromMap();
|
|
65
|
-
expect(target.childNodes[0]).toBe();
|
|
66
|
-
expect(spy1).toBeCalledTimes(1);
|
|
67
|
-
expect(spy2).toBeCalledTimes(3);
|
|
68
|
-
expect(spy3).toBeCalledTimes(2);
|
|
69
|
-
});
|
|
70
|
-
test("set a custom render method", () => {
|
|
71
|
-
const spy = jest.fn();
|
|
72
|
-
const control = new Control({
|
|
73
|
-
render: spy
|
|
74
|
-
});
|
|
75
|
-
expect(spy).toBeCalledTimes(1);
|
|
76
|
-
});
|
|
77
|
-
test("pass function params to custom render method", () => {
|
|
78
|
-
const spy = jest.fn();
|
|
79
|
-
const control = new Control({
|
|
80
|
-
render: spy
|
|
81
|
-
});
|
|
82
|
-
control.render("foo", "bar");
|
|
83
|
-
expect(spy).toBeCalledTimes(2);
|
|
84
|
-
expect(spy.mock.calls[1][0]).toBe("foo");
|
|
85
|
-
expect(spy.mock.calls[1][1]).toBe("bar");
|
|
86
|
-
});
|
|
87
|
-
});
|