mobility-toolbox-js 2.0.0 → 2.1.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 +279 -0
- package/api/RealtimeAPI.d.ts.map +1 -0
- package/api/RealtimeAPI.js +466 -0
- package/api/RoutingAPI.d.ts +37 -0
- package/api/RoutingAPI.d.ts.map +1 -0
- package/api/RoutingAPI.js +35 -0
- package/api/StopsAPI.d.ts +38 -0
- package/api/StopsAPI.d.ts.map +1 -0
- package/api/StopsAPI.js +36 -0
- 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/{tralis/typedefs.js → typedefs.js} +4 -10
- package/common/api/HttpAPI.d.ts +31 -0
- package/common/api/HttpAPI.d.ts.map +1 -0
- package/common/api/HttpAPI.js +57 -0
- package/common/api/WebSocketAPI.d.ts +153 -0
- package/common/api/WebSocketAPI.d.ts.map +1 -0
- package/common/api/WebSocketAPI.js +341 -0
- 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 -0
- 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 +776 -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/{trackerDelayStyle.js → realtimeDelayStyle.d.ts} +4 -9
- 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 +35 -6
- 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 +21 -23
- 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 +60 -60
- package/common/utils/index.d.ts +17 -0
- package/common/utils/index.d.ts.map +1 -0
- package/common/utils/index.js +13 -2
- 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 +5 -12
- 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 -20
- package/common/utils/timeUtils.d.ts +24 -0
- package/common/utils/timeUtils.d.ts.map +1 -0
- package/common/utils/timeUtils.js +13 -18
- 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 +10 -2
- package/mapbox/controls/CopyrightControl.d.ts +29 -0
- package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
- package/mapbox/controls/CopyrightControl.js +25 -35
- package/mapbox/controls/index.d.ts +2 -0
- package/mapbox/controls/index.d.ts.map +1 -0
- package/mapbox/controls/index.js +2 -0
- package/mapbox/index.d.ts +6 -0
- package/mapbox/index.d.ts.map +1 -0
- package/mapbox/index.js +5 -3
- package/mapbox/layers/Layer.d.ts +59 -0
- package/mapbox/layers/Layer.d.ts.map +1 -0
- package/mapbox/layers/Layer.js +78 -95
- 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 -0
- 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 +60005 -0
- package/mbt.js.map +7 -0
- package/mbt.min.js +1084 -0
- package/mbt.min.js.map +7 -0
- package/ol/controls/CopyrightControl.d.ts +31 -0
- package/ol/controls/CopyrightControl.d.ts.map +1 -0
- package/ol/controls/CopyrightControl.js +44 -56
- package/ol/controls/RoutingControl.d.ts +193 -0
- package/ol/controls/RoutingControl.d.ts.map +1 -0
- package/ol/controls/RoutingControl.js +546 -667
- package/ol/controls/StopFinderControl.d.ts +30 -0
- package/ol/controls/StopFinderControl.d.ts.map +1 -0
- package/ol/controls/StopFinderControl.js +10 -16
- package/ol/controls/index.d.ts +4 -0
- package/ol/controls/index.d.ts.map +1 -0
- package/ol/controls/index.js +3 -0
- package/ol/index.d.ts +6 -0
- package/ol/index.d.ts.map +1 -0
- package/ol/index.js +4 -12
- package/ol/layers/Layer.d.ts +86 -0
- package/ol/layers/Layer.d.ts.map +1 -0
- package/ol/layers/Layer.js +147 -166
- 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 +79 -348
- package/ol/layers/MapboxStyleLayer.d.ts +129 -0
- package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
- package/ol/layers/MapboxStyleLayer.js +329 -368
- package/ol/layers/MaplibreLayer.d.ts +28 -0
- package/ol/layers/MaplibreLayer.d.ts.map +1 -0
- package/ol/layers/MaplibreLayer.js +13 -259
- 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 +62 -68
- package/ol/layers/VectorLayer.d.ts +25 -0
- package/ol/layers/VectorLayer.d.ts.map +1 -0
- package/ol/layers/VectorLayer.js +27 -32
- package/ol/layers/WMSLayer.d.ts +42 -0
- package/ol/layers/WMSLayer.d.ts.map +1 -0
- package/ol/layers/WMSLayer.js +74 -66
- package/ol/layers/index.d.ts +9 -0
- package/ol/layers/index.d.ts.map +1 -0
- package/ol/layers/index.js +8 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryDelayStyle.js +23 -25
- package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
- package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryStyle.js +37 -44
- package/ol/styles/index.d.ts +3 -0
- package/ol/styles/index.d.ts.map +1 -0
- package/package.json +98 -177
- 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 +11 -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/routing/RoutingAPI.js +0 -44
- package/api/routing/RoutingAPI.test.js +0 -41
- package/api/stops/StopsAPI.js +0 -41
- package/api/stops/StopsAPI.test.js +0 -34
- package/api/tralis/TralisAPI.js +0 -731
- package/api/tralis/TralisAPI.test.js +0 -75
- package/api/tralis/TralisAPIUtils.js +0 -73
- package/api/tralis/WebSocketConnector.js +0 -338
- package/api/tralis/WebSocketConnector.test.js +0 -356
- package/common/Tracker.js +0 -197
- package/common/api/api.js +0 -64
- package/common/api/api.test.js +0 -68
- package/common/controls/Control.js +0 -146
- package/common/controls/Control.test.js +0 -98
- package/common/layers/Layer.js +0 -404
- package/common/layers/Layer.test.js +0 -585
- package/common/mixins/CopyrightMixin.js +0 -48
- package/common/mixins/SearchMixin.js +0 -176
- package/common/mixins/TralisLayerMixin.js +0 -930
- package/common/styles/trackerDefaultStyle.js +0 -333
- package/common/styles/trackerSimpleStyle.js +0 -22
- package/common/trackerConfig.js +0 -190
- package/common/trackerConfig.test.js +0 -25
- package/common/utils/createTrackerFilters.js +0 -87
- package/common/utils/createTrackerFilters.test.js +0 -95
- package/common/utils/getMapboxMapCopyrights.test.js +0 -47
- package/common/utils/getMapboxStyleUrl.js +0 -32
- package/common/utils/removeDuplicate.test.js +0 -22
- package/common/utils/timeUtils.test.js +0 -16
- package/index.js.map +0 -1
- package/mapbox/layers/Layer.test.js +0 -202
- package/mapbox/layers/TralisLayer.js +0 -329
- package/mapbox/layers/TralisLayer.test.js +0 -40
- package/mapbox/utils.js +0 -46
- package/module.js +0 -23
- package/ol/README.md +0 -0
- package/ol/controls/CopyrightControl.test.js +0 -211
- package/ol/controls/RoutingControl.test.js +0 -216
- package/ol/controls/StopFinderControl.test.js +0 -59
- package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
- package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
- package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
- package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
- package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
- package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
- package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
- package/ol/layers/Layer.test.js +0 -197
- package/ol/layers/MapboxLayer.test.js +0 -186
- package/ol/layers/MapboxStyleLayer.test.js +0 -262
- package/ol/layers/RoutingLayer.test.js +0 -49
- package/ol/layers/TralisLayer.js +0 -359
- package/ol/layers/TralisLayer.test.js +0 -97
- package/ol/layers/VectorLayer.test.js +0 -98
- package/ol/layers/WMSLayer.test.js +0 -84
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
import WS from 'jest-websocket-mock';
|
|
2
|
-
import Connector from './WebSocketConnector';
|
|
3
|
-
|
|
4
|
-
describe('WebSocketConnector', () => {
|
|
5
|
-
describe('#constructor', () => {
|
|
6
|
-
let server;
|
|
7
|
-
let server2;
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
server = new WS(`ws://foo:1234`);
|
|
11
|
-
server2 = new WS(`ws://foo:12345`);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
afterEach(() => {
|
|
15
|
-
server.close();
|
|
16
|
-
server2.close();
|
|
17
|
-
WS.clean();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('#constructor', () => {
|
|
21
|
-
test("doesn't connect.", async () => {
|
|
22
|
-
const client = new Connector();
|
|
23
|
-
expect(client.websocket).toBe();
|
|
24
|
-
expect(client.closed).toBe(false);
|
|
25
|
-
expect(client.closing).toBe(false);
|
|
26
|
-
expect(client.connecting).toBe(false);
|
|
27
|
-
expect(client.open).toBe(false);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe('#close', () => {
|
|
32
|
-
test('should close the websocket and clear some property', async () => {
|
|
33
|
-
// eslint-disable-next-line no-unused-vars
|
|
34
|
-
const client = new Connector();
|
|
35
|
-
const subsc2 = {
|
|
36
|
-
params: 'foo',
|
|
37
|
-
cb: () => {},
|
|
38
|
-
errorCb: () => {},
|
|
39
|
-
quiet: false,
|
|
40
|
-
};
|
|
41
|
-
client.subscriptions = [subsc2];
|
|
42
|
-
client.messagesOnOpen = ['GET foo'];
|
|
43
|
-
client.connect(`ws://foo:1234`);
|
|
44
|
-
client.websocket.addEventListener = jest.fn();
|
|
45
|
-
client.websocket.removeEventListener = jest.fn();
|
|
46
|
-
client.websocket.close = jest.fn();
|
|
47
|
-
await server.connected;
|
|
48
|
-
expect(client.websocket).toBeDefined();
|
|
49
|
-
expect(client.messagesOnOpen).toEqual(['GET foo']);
|
|
50
|
-
client.close();
|
|
51
|
-
expect(client.messagesOnOpen).toEqual([]);
|
|
52
|
-
expect(client.websocket).toBeNull();
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe('#connect', () => {
|
|
57
|
-
test('create a new WebSocket.', async () => {
|
|
58
|
-
const client = new Connector();
|
|
59
|
-
client.connect(`ws://foo:1234`);
|
|
60
|
-
await server.connected;
|
|
61
|
-
client.send('hello');
|
|
62
|
-
await expect(server).toReceiveMessage('hello');
|
|
63
|
-
expect(server).toHaveReceivedMessages(['hello']);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('close previous connection.', async () => {
|
|
67
|
-
const client = new Connector();
|
|
68
|
-
client.connect(`ws://foo:1234`);
|
|
69
|
-
await server.connected;
|
|
70
|
-
expect(client.websocket).toBeDefined();
|
|
71
|
-
const old = client.websocket;
|
|
72
|
-
expect(old.readyState).toBe(WebSocket.OPEN);
|
|
73
|
-
client.connect(`ws://foo:12345`);
|
|
74
|
-
expect(old.readyState).toBe(WebSocket.CLOSING);
|
|
75
|
-
expect(client.websocket.readyState).toBe(WebSocket.CONNECTING);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('call onOpen function', async () => {
|
|
79
|
-
const onOpen = jest.fn();
|
|
80
|
-
const client = new Connector();
|
|
81
|
-
client.subscribe = jest.fn();
|
|
82
|
-
client.connect(`ws://foo:1234`, onOpen);
|
|
83
|
-
await server.connected;
|
|
84
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
85
|
-
expect(onOpen).toHaveBeenCalledTimes(1);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test('subscribe previous subscriptions on open (quiet or not)', async () => {
|
|
89
|
-
const client = new Connector();
|
|
90
|
-
client.subscribe = jest.fn();
|
|
91
|
-
client.send = jest.fn();
|
|
92
|
-
const subsc = {
|
|
93
|
-
params: 'foo',
|
|
94
|
-
cb: () => {},
|
|
95
|
-
errorCb: () => {},
|
|
96
|
-
quiet: true,
|
|
97
|
-
};
|
|
98
|
-
const subsc2 = {
|
|
99
|
-
params: 'foo',
|
|
100
|
-
cb: () => {},
|
|
101
|
-
errorCb: () => {},
|
|
102
|
-
quiet: false,
|
|
103
|
-
};
|
|
104
|
-
client.subscriptions = [subsc, subsc2];
|
|
105
|
-
|
|
106
|
-
client.connect(`ws://foo:1234`);
|
|
107
|
-
await server.connected;
|
|
108
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
109
|
-
expect(client.subscribe).toHaveBeenCalledTimes(2);
|
|
110
|
-
client.subscribe.mockReset();
|
|
111
|
-
|
|
112
|
-
client.connect(`ws://foo:12345`);
|
|
113
|
-
await server2.connected;
|
|
114
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
115
|
-
expect(client.subscribe).toHaveBeenCalledTimes(2);
|
|
116
|
-
expect(client.subscribe).toHaveBeenCalledWith(
|
|
117
|
-
subsc.params,
|
|
118
|
-
subsc.cb,
|
|
119
|
-
subsc.errorCb,
|
|
120
|
-
subsc.quiet,
|
|
121
|
-
);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('send GET and SUB for not quiet previous subscriptions', async () => {
|
|
125
|
-
const client = new Connector();
|
|
126
|
-
client.send = jest.fn();
|
|
127
|
-
const subsc = {
|
|
128
|
-
params: { channel: 'foo' },
|
|
129
|
-
cb: () => {},
|
|
130
|
-
errorCb: () => {},
|
|
131
|
-
quiet: false,
|
|
132
|
-
};
|
|
133
|
-
client.subscriptions = [subsc];
|
|
134
|
-
|
|
135
|
-
client.connect(`ws://foo:1234`);
|
|
136
|
-
client.websocket.addEventListener = jest.fn();
|
|
137
|
-
client.websocket.removeEventListener = jest.fn();
|
|
138
|
-
await server.connected;
|
|
139
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
140
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
141
|
-
expect(client.send.mock.calls[0]).toEqual(['GET foo']);
|
|
142
|
-
expect(client.send.mock.calls[1]).toEqual(['SUB foo']);
|
|
143
|
-
client.send.mockReset();
|
|
144
|
-
|
|
145
|
-
client.connect(`ws://foo:12345`);
|
|
146
|
-
client.websocket.addEventListener = jest.fn();
|
|
147
|
-
client.websocket.removeEventListener = jest.fn();
|
|
148
|
-
await server2.connected;
|
|
149
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
150
|
-
// not quiet subscriptions will send GET and SUB requests.
|
|
151
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
152
|
-
expect(client.send.mock.calls[0]).toEqual(['GET foo']);
|
|
153
|
-
expect(client.send.mock.calls[1]).toEqual(['SUB foo']);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test('doesn\t send GET and SUB for quiet previous subscriptions', async () => {
|
|
157
|
-
const client = new Connector();
|
|
158
|
-
client.send = jest.fn();
|
|
159
|
-
const subsc = {
|
|
160
|
-
params: { channel: 'foo' },
|
|
161
|
-
cb: () => {},
|
|
162
|
-
errorCb: () => {},
|
|
163
|
-
quiet: true,
|
|
164
|
-
};
|
|
165
|
-
client.subscriptions = [subsc];
|
|
166
|
-
|
|
167
|
-
client.connect(`ws://foo:1234`);
|
|
168
|
-
client.websocket.addEventListener = jest.fn();
|
|
169
|
-
client.websocket.removeEventListener = jest.fn();
|
|
170
|
-
await server.connected;
|
|
171
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
172
|
-
expect(client.send).toHaveBeenCalledTimes(0);
|
|
173
|
-
client.send.mockReset();
|
|
174
|
-
|
|
175
|
-
client.connect(`ws://foo:12345`);
|
|
176
|
-
client.websocket.addEventListener = jest.fn();
|
|
177
|
-
client.websocket.removeEventListener = jest.fn();
|
|
178
|
-
await server2.connected;
|
|
179
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
180
|
-
// not quiet subscriptions will send GET and SUB requests.
|
|
181
|
-
expect(client.send).toHaveBeenCalledTimes(0);
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
describe('#subscribe', () => {
|
|
186
|
-
test('adds subscription to subscriptions array', async () => {
|
|
187
|
-
// eslint-disable-next-line no-unused-vars
|
|
188
|
-
const client = new Connector();
|
|
189
|
-
client.connect(`ws://foo:1234`);
|
|
190
|
-
await server.connected;
|
|
191
|
-
const params = { channel: 'bar', args: ['baz'], id: 'id' };
|
|
192
|
-
const cb = jest.fn();
|
|
193
|
-
const errorCb = jest.fn();
|
|
194
|
-
client.subscribe(params, cb, errorCb);
|
|
195
|
-
expect(client.subscriptions.length).toBe(1);
|
|
196
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
197
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
198
|
-
expect(client.subscriptions[0].errorCb).toBe(errorCb);
|
|
199
|
-
expect(client.subscriptions[0].quiet).toBe(false);
|
|
200
|
-
|
|
201
|
-
const obj = { source: 'bar baz', client_reference: 'id' };
|
|
202
|
-
server.send(JSON.stringify(obj));
|
|
203
|
-
|
|
204
|
-
expect(cb).toHaveBeenCalledTimes(1);
|
|
205
|
-
expect(cb).toHaveBeenCalledWith(obj);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
test("doesn't duplicate subscriptions", async () => {
|
|
209
|
-
// eslint-disable-next-line no-unused-vars
|
|
210
|
-
const client = new Connector();
|
|
211
|
-
client.connect(`ws://foo:1234`);
|
|
212
|
-
await server.connected;
|
|
213
|
-
const params = { channel: 'bar', args: ['baz'], id: 'id' };
|
|
214
|
-
const cb = jest.fn();
|
|
215
|
-
const errorCb = jest.fn();
|
|
216
|
-
client.subscribe(params, cb, errorCb, true);
|
|
217
|
-
client.subscribe(params, cb, errorCb, true);
|
|
218
|
-
expect(client.subscriptions.length).toBe(1);
|
|
219
|
-
|
|
220
|
-
const obj = { source: 'bar baz', client_reference: 'id' };
|
|
221
|
-
server.send(JSON.stringify(obj));
|
|
222
|
-
|
|
223
|
-
expect(cb).toHaveBeenCalledTimes(1);
|
|
224
|
-
expect(cb).toHaveBeenCalledWith(obj);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test('send GET and SUB requests.', async () => {
|
|
228
|
-
// eslint-disable-next-line no-unused-vars
|
|
229
|
-
const client = new Connector();
|
|
230
|
-
client.connect(`ws://foo:1234`);
|
|
231
|
-
client.send = jest.fn();
|
|
232
|
-
const params = { channel: 'bar', args: ['baz'], id: 'id' };
|
|
233
|
-
const cb = jest.fn();
|
|
234
|
-
const errorCb = jest.fn();
|
|
235
|
-
client.subscribe(params, cb, errorCb);
|
|
236
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
237
|
-
expect(client.send).toHaveBeenCalledWith('GET bar baz id');
|
|
238
|
-
expect(client.send).toHaveBeenCalledWith('SUB bar baz id');
|
|
239
|
-
client.send.mockRestore();
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
test('should register callback without sending GET and SUB requests (quiet=true).', async () => {
|
|
243
|
-
// eslint-disable-next-line no-unused-vars
|
|
244
|
-
const client = new Connector();
|
|
245
|
-
client.connect(`ws://foo:1234`);
|
|
246
|
-
await server.connected;
|
|
247
|
-
const params = { channel: 'bar', args: ['baz'], id: 'id' };
|
|
248
|
-
const cb = jest.fn();
|
|
249
|
-
const errorCb = jest.fn();
|
|
250
|
-
client.send = jest.fn();
|
|
251
|
-
client.subscribe(params, cb, errorCb, true);
|
|
252
|
-
expect(client.subscriptions.length).toBe(1);
|
|
253
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
254
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
255
|
-
expect(client.subscriptions[0].errorCb).toBe(errorCb);
|
|
256
|
-
expect(client.subscriptions[0].quiet).toBe(true);
|
|
257
|
-
expect(client.send).toBeCalledTimes(0);
|
|
258
|
-
client.send.mockRestore();
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
describe('#unsubscribe', () => {
|
|
263
|
-
test('should only unsubscribe the subscription using the good cb', async () => {
|
|
264
|
-
// eslint-disable-next-line no-unused-vars
|
|
265
|
-
const client = new Connector();
|
|
266
|
-
client.connect(`ws://foo:1234`);
|
|
267
|
-
await server.connected;
|
|
268
|
-
const params = { channel: 'foo', id: 'id' };
|
|
269
|
-
const cb = jest.fn();
|
|
270
|
-
const cb2 = jest.fn();
|
|
271
|
-
client.subscribe(params, cb);
|
|
272
|
-
client.subscribe(params, cb2);
|
|
273
|
-
expect(client.subscriptions.length).toBe(2);
|
|
274
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
275
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
276
|
-
expect(client.subscriptions[1].params).toBe(params);
|
|
277
|
-
expect(client.subscriptions[1].cb).toBe(cb2);
|
|
278
|
-
|
|
279
|
-
client.unsubscribe('foo', cb);
|
|
280
|
-
expect(client.subscriptions.length).toBe(1);
|
|
281
|
-
|
|
282
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
283
|
-
expect(cb2).toHaveBeenCalledTimes(0);
|
|
284
|
-
const obj = { source: 'foo', client_reference: 'id' };
|
|
285
|
-
server.send(JSON.stringify(obj));
|
|
286
|
-
|
|
287
|
-
expect(cb2).toHaveBeenCalledTimes(1);
|
|
288
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
test('should unsubscribe all subscriptions related to a channel', () => {
|
|
292
|
-
// eslint-disable-next-line no-unused-vars
|
|
293
|
-
const client = new Connector();
|
|
294
|
-
client.connect(`ws://foo:1234`);
|
|
295
|
-
client.websocket.removeEventListener = jest.fn();
|
|
296
|
-
client.websocket.addEventListener = jest.fn();
|
|
297
|
-
const params = { channel: 'foo' };
|
|
298
|
-
const params2 = { channel: 'bar' };
|
|
299
|
-
const cb = jest.fn();
|
|
300
|
-
const cb2 = jest.fn();
|
|
301
|
-
client.subscribe(params, cb);
|
|
302
|
-
client.subscribe(params, cb);
|
|
303
|
-
client.subscribe(params, cb);
|
|
304
|
-
client.subscribe(params, cb2);
|
|
305
|
-
client.subscribe(params2, cb2);
|
|
306
|
-
expect(client.subscriptions.length).toBe(3);
|
|
307
|
-
expect(client.websocket.removeEventListener).toBeCalledTimes(2);
|
|
308
|
-
expect(
|
|
309
|
-
client.websocket.addEventListener.mock.calls.filter(
|
|
310
|
-
(c) => c[0] === 'message',
|
|
311
|
-
).length,
|
|
312
|
-
).toBe(5);
|
|
313
|
-
|
|
314
|
-
client.unsubscribe('foo');
|
|
315
|
-
expect(client.subscriptions.length).toBe(1);
|
|
316
|
-
expect(client.subscriptions[0].params).toBe(params2);
|
|
317
|
-
expect(client.subscriptions[0].cb).toBe(cb2);
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
test('send DEL when there is no more unquiet subscriptions on the channel', async () => {
|
|
321
|
-
// eslint-disable-next-line no-unused-vars
|
|
322
|
-
const client = new Connector();
|
|
323
|
-
client.connect(`ws://foo:1234`);
|
|
324
|
-
await server.connected;
|
|
325
|
-
client.send = jest.fn();
|
|
326
|
-
client.websocket.removeEventListener = jest.fn();
|
|
327
|
-
client.websocket.addEventListener = jest.fn();
|
|
328
|
-
const params = { channel: 'foo' };
|
|
329
|
-
const cb = jest.fn();
|
|
330
|
-
client.subscribe(params, cb);
|
|
331
|
-
expect(client.send).toHaveBeenCalledWith('GET foo');
|
|
332
|
-
expect(client.send).toHaveBeenCalledWith('SUB foo');
|
|
333
|
-
|
|
334
|
-
client.unsubscribe('foo');
|
|
335
|
-
expect(client.send).toHaveBeenCalledWith('DEL foo');
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
test("doesn't send DEL when we unsubscribe a quiet channel", () => {
|
|
339
|
-
// eslint-disable-next-line no-unused-vars
|
|
340
|
-
const client = new Connector();
|
|
341
|
-
client.connect(`ws://foo:1234`);
|
|
342
|
-
client.send = jest.fn();
|
|
343
|
-
client.websocket.removeEventListener = jest.fn();
|
|
344
|
-
client.websocket.addEventListener = jest.fn();
|
|
345
|
-
const params = { channel: 'foo' };
|
|
346
|
-
const cb = jest.fn();
|
|
347
|
-
client.subscribe(params, cb, null, true);
|
|
348
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
349
|
-
|
|
350
|
-
client.unsubscribe('foo');
|
|
351
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
352
|
-
client.send.mockRestore();
|
|
353
|
-
});
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
});
|
package/common/Tracker.js
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
import { compose, apply, create } from 'ol/transform';
|
|
3
|
-
import getVehiclePosition from './utils/getVehiclePosition';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Tracker. This class stores and allows to draw trajectories on a canvas.
|
|
7
|
-
* @class
|
|
8
|
-
* @param {Object} options
|
|
9
|
-
* @private
|
|
10
|
-
*/
|
|
11
|
-
export default class Tracker {
|
|
12
|
-
/**
|
|
13
|
-
* @private
|
|
14
|
-
*/
|
|
15
|
-
constructor(options) {
|
|
16
|
-
/**
|
|
17
|
-
* Function use to style the features displayed.
|
|
18
|
-
* @type {function}
|
|
19
|
-
*/
|
|
20
|
-
this.style = options.style;
|
|
21
|
-
|
|
22
|
-
// we draw directly on the canvas since openlayers is too slow.
|
|
23
|
-
/**
|
|
24
|
-
* HTML <canvas> element.
|
|
25
|
-
* @type {Canvas}
|
|
26
|
-
*/
|
|
27
|
-
this.canvas = options.canvas || document.createElement('canvas');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Draw all the trajectories available to the canvas.
|
|
32
|
-
* @param {ViewState} trajectories An array of trajectories.
|
|
33
|
-
* @param {ViewState} viewState The view state of the map.
|
|
34
|
-
* @param {boolean} options.hoverVehicleId The id of the vehicle to highlight.
|
|
35
|
-
* @param {boolean} options.selectedVehicleId The id of the vehicle to select.
|
|
36
|
-
* @param {boolean} options.noInterpolate If true trajectories are not interpolated but
|
|
37
|
-
* drawn at the last known coordinate. Use this for performance optimization
|
|
38
|
-
* during map navigation.
|
|
39
|
-
* @private
|
|
40
|
-
*/
|
|
41
|
-
renderTrajectories(trajectories, viewState, options) {
|
|
42
|
-
const {
|
|
43
|
-
time = Date.now(),
|
|
44
|
-
size = [],
|
|
45
|
-
center,
|
|
46
|
-
resolution,
|
|
47
|
-
rotation = 0,
|
|
48
|
-
pixelRatio,
|
|
49
|
-
} = viewState;
|
|
50
|
-
const {
|
|
51
|
-
noInterpolate = false,
|
|
52
|
-
hoverVehicleId,
|
|
53
|
-
selectedVehicleId,
|
|
54
|
-
} = options;
|
|
55
|
-
|
|
56
|
-
const { canvas } = this;
|
|
57
|
-
const context = canvas.getContext('2d');
|
|
58
|
-
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
59
|
-
|
|
60
|
-
const [width, height] = size;
|
|
61
|
-
if (
|
|
62
|
-
width &&
|
|
63
|
-
height &&
|
|
64
|
-
(canvas.width !== width || canvas.height !== height)
|
|
65
|
-
) {
|
|
66
|
-
[canvas.width, canvas.height] = [width * pixelRatio, height * pixelRatio];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const coordinateToPixelTransform = compose(
|
|
70
|
-
create(),
|
|
71
|
-
size[0] / 2,
|
|
72
|
-
size[1] / 2,
|
|
73
|
-
1 / resolution,
|
|
74
|
-
-1 / resolution,
|
|
75
|
-
-rotation,
|
|
76
|
-
-center[0],
|
|
77
|
-
-center[1],
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
// Offscreen canvas has not style attribute
|
|
81
|
-
if (canvas.style) {
|
|
82
|
-
canvas.style.width = `${canvas.width / pixelRatio}px`;
|
|
83
|
-
canvas.style.height = `${canvas.height / pixelRatio}px`;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
let hoverVehicleImg;
|
|
87
|
-
let hoverVehiclePx;
|
|
88
|
-
let hoverVehicleWidth;
|
|
89
|
-
let hoverVehicleHeight;
|
|
90
|
-
let selectedVehicleImg;
|
|
91
|
-
let selectedVehiclePx;
|
|
92
|
-
let selectedVehicleWidth;
|
|
93
|
-
let selectedVehicleHeight;
|
|
94
|
-
let nbRendered = 0;
|
|
95
|
-
|
|
96
|
-
for (let i = trajectories.length - 1; i >= 0; i -= 1) {
|
|
97
|
-
const trajectory = trajectories[i];
|
|
98
|
-
|
|
99
|
-
// We simplify the trajectory object
|
|
100
|
-
const { train_id: id, timeOffset } = trajectory.properties;
|
|
101
|
-
// We set the rotation and the timeFraction of the trajectory (used by tralis).
|
|
102
|
-
// if rotation === null that seems there is no rotation available.
|
|
103
|
-
const { coord, rotation: rotationIcon } = getVehiclePosition(
|
|
104
|
-
time - (timeOffset || 0),
|
|
105
|
-
trajectory,
|
|
106
|
-
noInterpolate,
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
// We store the current vehicle position to the trajectory.
|
|
110
|
-
trajectories[i].properties.coordinate = coord;
|
|
111
|
-
trajectories[i].properties.rotation = rotationIcon;
|
|
112
|
-
|
|
113
|
-
if (!coord) {
|
|
114
|
-
// eslint-disable-next-line no-continue
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
let px = apply(coordinateToPixelTransform, [...coord]);
|
|
119
|
-
if (!px) {
|
|
120
|
-
// eslint-disable-next-line no-continue
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
px = px.map((p) => p * pixelRatio);
|
|
125
|
-
|
|
126
|
-
if (
|
|
127
|
-
px[0] < 0 ||
|
|
128
|
-
px[0] > canvas.width ||
|
|
129
|
-
px[1] < 0 ||
|
|
130
|
-
px[1] > canvas.height
|
|
131
|
-
) {
|
|
132
|
-
// eslint-disable-next-line no-continue
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const vehicleImg = this.style(trajectory, viewState, options);
|
|
137
|
-
if (!vehicleImg) {
|
|
138
|
-
// eslint-disable-next-line no-continue
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
nbRendered += 1;
|
|
143
|
-
|
|
144
|
-
const imgWidth = vehicleImg.width;
|
|
145
|
-
const imgHeight = vehicleImg.height;
|
|
146
|
-
|
|
147
|
-
if (hoverVehicleId !== id && selectedVehicleId !== id) {
|
|
148
|
-
context.drawImage(
|
|
149
|
-
vehicleImg,
|
|
150
|
-
px[0] - imgWidth / 2,
|
|
151
|
-
px[1] - imgHeight / 2,
|
|
152
|
-
imgWidth,
|
|
153
|
-
imgHeight,
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (hoverVehicleId && hoverVehicleId === id) {
|
|
158
|
-
// Store the canvas to draw it at the end
|
|
159
|
-
hoverVehicleImg = vehicleImg;
|
|
160
|
-
hoverVehiclePx = px;
|
|
161
|
-
hoverVehicleWidth = imgWidth;
|
|
162
|
-
hoverVehicleHeight = imgHeight;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (selectedVehicleId && selectedVehicleId === id) {
|
|
166
|
-
// Store the canvas to draw it at the end
|
|
167
|
-
selectedVehicleImg = vehicleImg;
|
|
168
|
-
selectedVehiclePx = px;
|
|
169
|
-
selectedVehicleWidth = imgWidth;
|
|
170
|
-
selectedVehicleHeight = imgHeight;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (selectedVehicleImg) {
|
|
175
|
-
context.drawImage(
|
|
176
|
-
selectedVehicleImg,
|
|
177
|
-
selectedVehiclePx[0] - selectedVehicleWidth / 2,
|
|
178
|
-
selectedVehiclePx[1] - selectedVehicleHeight / 2,
|
|
179
|
-
selectedVehicleWidth,
|
|
180
|
-
selectedVehicleHeight,
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (hoverVehicleImg) {
|
|
185
|
-
context.drawImage(
|
|
186
|
-
hoverVehicleImg,
|
|
187
|
-
hoverVehiclePx[0] - hoverVehicleWidth / 2,
|
|
188
|
-
hoverVehiclePx[1] - hoverVehicleHeight / 2,
|
|
189
|
-
hoverVehicleWidth,
|
|
190
|
-
hoverVehicleHeight,
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
return {
|
|
194
|
-
nbTrajectoriesRendered: nbRendered,
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
}
|
package/common/api/api.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import qs from 'query-string';
|
|
2
|
-
import BaseObject from 'ol/Object';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Common class to access to a geOps api.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* import { API } from 'mobility-toolbox-js/api';
|
|
9
|
-
*
|
|
10
|
-
* const api = new API({
|
|
11
|
-
* url: [yourUrl],
|
|
12
|
-
* apiKey: [yourApiKey]
|
|
13
|
-
* });
|
|
14
|
-
*
|
|
15
|
-
* @classproperty {string} url Url of the service.
|
|
16
|
-
* @classproperty {string} apiKey Api key to access the service.
|
|
17
|
-
*/
|
|
18
|
-
class API extends BaseObject {
|
|
19
|
-
constructor(options = {}) {
|
|
20
|
-
super();
|
|
21
|
-
/** @ignore */
|
|
22
|
-
this.url = options.url;
|
|
23
|
-
|
|
24
|
-
/** @ignore */
|
|
25
|
-
this.apiKey = options.apiKey;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Append the apiKey before sending the request.
|
|
30
|
-
* @ignore
|
|
31
|
-
*/
|
|
32
|
-
fetch(path, params, config) {
|
|
33
|
-
// Clean requets parameters, removing undefined and null values.
|
|
34
|
-
const urlParams = { ...(params || {}), key: this.apiKey };
|
|
35
|
-
const clone = { ...urlParams };
|
|
36
|
-
Object.keys(urlParams).forEach(
|
|
37
|
-
(key) =>
|
|
38
|
-
(clone[key] === undefined || clone[key] === null) && delete clone[key],
|
|
39
|
-
);
|
|
40
|
-
if (!this.apiKey) {
|
|
41
|
-
// eslint-disable-next-line no-console
|
|
42
|
-
return Promise.reject(
|
|
43
|
-
new Error(`No apiKey defined for request to ${this.url}`),
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
return fetch(
|
|
47
|
-
`${this.url}${path || ''}?${qs.stringify(clone)}`,
|
|
48
|
-
config,
|
|
49
|
-
).then((response) => {
|
|
50
|
-
try {
|
|
51
|
-
return response.json().then((data) => {
|
|
52
|
-
if (data.error) {
|
|
53
|
-
throw new Error(data.error);
|
|
54
|
-
}
|
|
55
|
-
return data;
|
|
56
|
-
});
|
|
57
|
-
} catch (err) {
|
|
58
|
-
return Promise.reject(new Error(err));
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export default API;
|
package/common/api/api.test.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import API from './api';
|
|
3
|
-
|
|
4
|
-
let api;
|
|
5
|
-
|
|
6
|
-
describe('API', () => {
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
global.fetch = fetch;
|
|
9
|
-
fetch.resetMocks();
|
|
10
|
-
|
|
11
|
-
api = new API({ url: 'https://foo.ch', apiKey: 'apiKey' });
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
describe('#fetch', () => {
|
|
15
|
-
test('should success', () => {
|
|
16
|
-
fetch.mockResponseOnce(JSON.stringify({ foo: 'bar' }));
|
|
17
|
-
|
|
18
|
-
return api
|
|
19
|
-
.fetch('/path', {
|
|
20
|
-
q: 'Bern',
|
|
21
|
-
fooUndefined: undefined,
|
|
22
|
-
fooNull: null,
|
|
23
|
-
fooEmpty: '',
|
|
24
|
-
})
|
|
25
|
-
.then((response) => {
|
|
26
|
-
// Correct url
|
|
27
|
-
expect(fetch.mock.calls[0][0]).toEqual(
|
|
28
|
-
'https://foo.ch/path?fooEmpty=&key=apiKey&q=Bern',
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
// Correct search result
|
|
32
|
-
expect(response).toEqual({ foo: 'bar' });
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe('should display error message', () => {
|
|
37
|
-
test('reject error', (done) => {
|
|
38
|
-
fetch.mockRejectOnce(new Error('Fake error message'));
|
|
39
|
-
return api.fetch().catch((err) => {
|
|
40
|
-
expect(err.name).toEqual('Error');
|
|
41
|
-
expect(err.message).toEqual('Fake error message');
|
|
42
|
-
done();
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test('if the response is invalid json', (done) => {
|
|
47
|
-
fetch.mockResponseOnce('invalid json');
|
|
48
|
-
|
|
49
|
-
api.fetch().catch((err) => {
|
|
50
|
-
expect(err.name).toEqual('FetchError');
|
|
51
|
-
expect(err.message).toEqual(
|
|
52
|
-
'invalid json response body at reason: Unexpected token i in JSON at position 0',
|
|
53
|
-
);
|
|
54
|
-
done();
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('if the response contains an error message', (done) => {
|
|
59
|
-
fetch.mockResponseOnce('{"error":"foo2"}');
|
|
60
|
-
api.fetch().catch((err) => {
|
|
61
|
-
expect(err.name).toEqual('Error');
|
|
62
|
-
expect(err.message).toEqual('foo2');
|
|
63
|
-
done();
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
});
|