UNPKG

solvice-js-client

Version:

A JavaScript client to access the Solvice api.

310 lines (257 loc) 11.1 kB
describe("VrpClient", function () { var problem = { "solverType": "VRP", "locations" : [ { "name" : "loc0", "latitude" : 51.130957575422364, "longitude" : 4.713613704439873 }, { "name" : "loc1", "latitude" : 50.83436927783171, "longitude" : 4.744984900808598 }], "options" : { "capacitySpread" : true, "minimizeDriverWaitTime" : false, "minimizeUseOfVehicles" : false, "vehicleSetupCost" : 60, "minimumMinutesWorked" : 0 }, "orders" : [ { "name" : "order0", "location" : "loc3", "demand" : 1, "windows" : [ { "starttime" : 480, "endtime" : 720, "hard" : true }, { "starttime" : 780, "endtime" : 1020, "hard" : true } ], "type" : "Technician", "duration" : 30 }, { "name" : "order1", "location" : "loc2", "demand" : 1, "windows" : [ { "starttime" : 480, "endtime" : 720, "hard" : true }, { "starttime" : 780, "endtime" : 1020, "hard" : true } ], "type" : "Technician", "duration" : 30 }], "fleet" : [ { "name" : "truck0", "capacity" : 27, "startlocation" : "loc3", "endlocation" : "loc1", "shiftstart" : 480, "shiftend" : 1020, "breakstart" : 720, "breakend" : 780, "breakduration" : 45, "maxminutes" : 0, "category" : "TRUCK", "type" : "Technician" }, { "name" : "truck1", "capacity" : 25, "startlocation" : "loc2", "endlocation" : "loc2", "shiftstart" : 480, "shiftend" : 1020, "breakstart" : 720, "breakend" : 780, "breakduration" : 45, "maxminutes" : 0, "category" : "TRUCK", "type" : "Technician" } ] }; it('should define Solvice in window', function () { expect(window).to.have.property('Solvice'); }); it('should define VrpClient in window.Solvice', function () { expect(window).to.have.deep.property('Solvice.VrpClient').which.is.a('function'); }); describe('#addLocation', function () { var client = new window.Solvice.VrpClient(); it('should not throw an exception if called with valid arguments', function () { var location = { latitude: 21, longitude: 23, name: 'Place 1' }; expect(client.addLocation.bind(client, location)).not.to.throw(TypeError); }); it('should throw an exception if called with an empty location', function () { expect(client.addLocation.bind(client, null)).to.throw(TypeError, /MISSING_FIELD: location/); }); it('should throw an exception if called with an empty location', function () { expect(client.addLocation.bind(client, {})).to.throw(TypeError, /MISSING_FIELD: latitude/); }); it('should throw an exception if called with an empty location', function () { expect(client.addLocation.bind(client, {latitude: 51})).to.throw(TypeError, /MISSING_FIELD: longitude/); }); it('should throw an exception if called with an empty location', function () { expect(client.addLocation.bind(client, {latitude: 51, longitude: 51})).to.throw(TypeError, /MISSING_FIELD: name/); }); it('should throw an exception if called with an empty location', function () { expect(client.addLocation.bind(client, {latitude: 51, longitude: 51, name: 'Loc1'})).not.to.throw(TypeError); }); }); describe('#addOrder', function () { var client = new window.Solvice.VrpClient(); it('should not throw an exception if called with valid arguments', function () { var order = { name: 'order1', location: 'loc3' }; expect(client.addOrder.bind(client, order)).not.to.throw(TypeError); }); it('should throw an exception if called with an empty order', function () { expect(client.addOrder.bind(client, null)).to.throw(TypeError, 'MISSING_FIELD: order'); }); it('should throw an exception if called with an empty name', function () { var order = { location: 'loc3' }; expect(client.addOrder.bind(client, order)).to.throw(TypeError, /MISSING_FIELD: name/); }); it('should throw an exception if called with an empty location', function () { var order = { name: 'loc3' }; expect(client.addOrder.bind(client, order)).to.throw(TypeError, /MISSING_FIELD: location/); }); it('should throw an exception if called with an invalid demand', function () { var order = { name: 'order1', location: 'loc3', demand: 'invalid' }; expect(client.addOrder.bind(client, order)).to.throw(TypeError, /demand/); }); it('should throw an exception if called with an invalid demand', function () { var order = { name: 'order1', location: 'loc3', duration: 'invalid' }; expect(client.addOrder.bind(client, order)).to.throw(TypeError, /duration/); }); it('should throw an exception if called with an invalid activity', function () { var order = { name: 'order1', location: 'loc3', activity: 'invalid' }; expect(client.addOrder.bind(client, order)).to.throw(TypeError, /activity/); }); it('should throw an exception if called with a missing window endtime', function () { var order = { name: 'order1', location: 'loc3', windows: [{ starttime: 21, hard: true }] }; expect(client.addOrder.bind(client, order)).to.throw(TypeError, /endtime/); }); it('should throw an exception if called with a missing window starttime', function () { var order = { name: 'order1', location: 'loc3', windows: [{ endttime: 21, hard: true }] }; expect(client.addOrder.bind(client, order)).to.throw(TypeError, /starttime/); }); it('should not throw an exception if called with valid windows', function () { var order = { name: 'order1', location: 'loc3', windows: [{ starttime: 23, endtime: 24, hard: true }] }; expect(client.addOrder.bind(client, order)).to.not.throw(TypeError); }); }); describe('#setOption', function () { var client = new window.Solvice.VrpClient(); it('should not throw an exception if called with valid arguments', function () { expect(client.setOption.bind(client, 'capacitySpread', true)).not.to.throw(TypeError); expect(client.setOption.bind(client, 'minimizeDriverWaitTime', true)).not.to.throw(TypeError); expect(client.setOption.bind(client, 'minimizeUseOfVehicles', true)).not.to.throw(TypeError); expect(client.setOption.bind(client, 'vehicleSetupCost', true)).not.to.throw(TypeError); expect(client.setOption.bind(client, 'minimumMinutesWorked', true)).not.to.throw(TypeError); }); it('should throw an exception if called with an invalid option', function () { expect(client.setOption.bind(client, 'invalid', true)).to.throw(TypeError); }); }); describe('#getRouteRequestParams', function () { var client = new window.Solvice.VrpClient(); it('should return full params list', function () { problem.locations.forEach(function (location) { client.addLocation(location) }); problem.orders.forEach(function (orders) { client.addOrder(orders) }); problem.fleet.forEach(function (fleetUnit) { client.addFleetUnit(fleetUnit) }); Object.keys(problem.options).forEach(function (key) { client.setOption(key, problem.options[key]) }); var submitted = client.getRouteRequestParams(); expect(submitted).to.deep.equal(problem); }); }); describe('#getRoute', function () { var client; var xhr; var requests = []; beforeEach(function () { requests = []; xhr = sinon.useFakeXMLHttpRequest(); xhr.onCreate = function (xhr) { requests.push(xhr); }; client = new window.Solvice.VrpClient() problem.locations.forEach(function (location) { client.addLocation(location) }); problem.orders.forEach(function (orders) { client.addOrder(orders) }); problem.fleet.forEach(function (fleetUnit) { client.addFleetUnit(fleetUnit) }); Object.keys(problem.options).forEach(function (key) { client.setOption(key, problem.options[key]) }); }); afterEach(function () { xhr.restore(); }); it('should return successful result if the api returns success', function () { var successCallback = sinon.spy(); var errorCallback = sinon.spy(); client.getRoute(successCallback, errorCallback); expect(requests).to.have.length(1); expect(JSON.parse(requests[0].requestBody)).to.deep.equal(problem); requests[0].respond(200, { "Content-Type": "application/json" }, '[{ "id": 12, "status": "QUEUED" }]'); expect(successCallback.calledOnce).to.equal(true); expect(errorCallback.calledOnce).to.equal(false); }); it('should return error if the api returns error', function () { var successCallback = sinon.spy(); var errorCallback = sinon.spy(); client.getRoute(successCallback, errorCallback); expect(requests).to.have.length(1); requests[0].respond(401, { "Content-Type": "application/json" }, '[{ "id": 12, "status": "QUEUED" }]'); expect(successCallback.calledOnce).to.equal(false); expect(errorCallback.calledOnce).to.equal(true); }); }); });