UNPKG

@iwpnd/valhalla-ts

Version:

A nodejs client and helper utilities for valhalla routing engine

240 lines 8.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const rip_ts_1 = require("@iwpnd/rip-ts"); const __fixtures__1 = require("./__fixtures__"); const types_1 = require("./types"); const valhalla_1 = require("./valhalla"); const mockAgent = new rip_ts_1.MockAgent({ connections: 1 }); (0, rip_ts_1.setGlobalDispatcher)(mockAgent); mockAgent.disableNetConnect(); describe('valhalla', () => { const url = 'http://localhost:3000'; const valhalla = new valhalla_1.Valhalla(url); const mockPool = mockAgent.get(url); describe('make jest coverage happy', () => { const maneuvertype = types_1.ManeuverType.kLeft; expect(maneuvertype).toEqual(15); }); describe('status', () => { it('should request status with verbose=true', async () => { const status = (0, __fixtures__1.randomStatus)(); mockPool .intercept({ path: '/status?verbose=true', method: 'GET', headers: { Accept: 'application/json' }, }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(200, status); await expect(valhalla.status(true)).resolves.toEqual(status); }); it('should throw on error', async () => { mockPool .intercept({ path: '/status', method: 'GET', headers: { Accept: 'application/json' }, }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(404, {}); await expect(valhalla.status()).rejects.toThrow(rip_ts_1.RequestError); }); }); describe('route', () => { const trip = (0, __fixtures__1.randomTrip)(); const { locations: [start, end], } = trip; const req = { costing: 'bicycle', directions_type: 'none', directions_options: { units: 'kilometers', }, locations: [start, end], }; it('should request a route', async () => { const resp = { trip, }; mockPool .intercept({ path: '/route', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(200, resp); await expect(valhalla.route(req)).resolves.toEqual(trip); }); it('should throw on error', async () => { mockPool .intercept({ path: '/route', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(404, {}); await expect(valhalla.route(req)).rejects.toThrow(rip_ts_1.RequestError); }); }); describe('optimizedRoute', () => { const trip = (0, __fixtures__1.randomTrip)(); const { locations: [start, end], } = trip; const req = { costing: 'bicycle', directions_type: 'none', directions_options: { units: 'kilometers', }, locations: [start, end], }; it('should request optimized route', async () => { const resp = { trip, }; mockPool .intercept({ path: '/optimized_route', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(200, resp); await expect(valhalla.optimizedRoute(req)).resolves.toEqual(resp); }); it('should throw on error', async () => { mockPool .intercept({ path: '/optimized_route', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(404, {}); await expect(valhalla.optimizedRoute(req)).rejects.toThrow(rip_ts_1.RequestError); }); }); describe('isochrone', () => { const isochrone = (0, __fixtures__1.randomIsochrone)(); const req = { locations: [{ lat: 1, lon: 1 }], costing: 'bicycle', contours: [{ time: 5, color: '000000' }], polygons: true, }; it('should an isochrone', async () => { const resp = { type: 'FeatureCollection', features: [isochrone], }; mockPool .intercept({ path: '/isochrone', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(200, resp); await expect(valhalla.isochrone(req)).resolves.toEqual(resp); }); it('should throw on error', async () => { mockPool .intercept({ path: '/isochrone', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(404, {}); await expect(valhalla.isochrone(req)).rejects.toThrow(rip_ts_1.RequestError); }); }); describe('traceRoute', () => { const trip = (0, __fixtures__1.randomTrip)(); const { locations: [start, end], } = trip; const req = { shape: [start, end], shape_match: 'map_snap', costing: 'bicycle', directions_type: 'none', directions_options: { units: 'kilometers', }, }; it('should request trace route', async () => { mockPool .intercept({ path: '/trace_route', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(200, { trip }); await expect(valhalla.traceRoute(req)).resolves.toEqual({ trip }); }); it('should throw on error', async () => { mockPool .intercept({ path: '/trace_route', method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(req), }) .defaultReplyHeaders({ 'Content-Type': 'application/json', }) .reply(404, {}); await expect(valhalla.traceRoute(req)).rejects.toThrow(rip_ts_1.RequestError); }); }); }); //# sourceMappingURL=valhalla.test.js.map