Skip to content

Commit

Permalink
fix(age): make age optional (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregChan authored Jul 27, 2018
1 parent e0b80c3 commit 8728190
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const _ = require('lodash');

/** @exports Vehicle */

/**
* @param {Object} response
*
* @return {Date|null} A parsed age or null if no age exists
*/
const parseAge = function(response) {
const age = _.get(response, 'headers.sc-data-age', null);
return age ? new Date(age) : null;
};

/**
* Initializes a new Vehicle to use for making requests to the Smartcar API.
*
Expand Down Expand Up @@ -138,7 +148,7 @@ Vehicle.prototype.location = function() {
.then(function(response) {
return {
data: response.body,
age: new Date(response.headers['sc-data-age']),
age: parseAge(response),
};
});
};
Expand Down Expand Up @@ -173,7 +183,7 @@ Vehicle.prototype.odometer = function() {
.then(function(response) {
return {
data: response.body,
age: new Date(response.headers['sc-data-age']),
age: parseAge(response),
unitSystem: response.headers['sc-unit-system'],
};
});
Expand Down
36 changes: 36 additions & 0 deletions test/lib/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ test('location', async function(t) {

});

test('location - no age', async function(t) {

const body = {
latitude: 1234,
longitude: 1234,
};
const headers = {};
t.context.n = nocks.base()
.get('/location')
.reply(200, body, headers);

const response = await vehicle.location();
t.deepEqual(response.data, body);
t.is(response.age, null);

});

test('odometer', async function(t) {

const body = {
Expand All @@ -192,6 +209,25 @@ test('odometer', async function(t) {

});

test('odometer - no age', async function(t) {

const body = {
distance: 1234,
};
const headers = {
'sc-unit-system': 'metric',
};
t.context.n = nocks.base()
.get('/odometer')
.reply(200, body, headers);

const response = await vehicle.odometer();
t.deepEqual(response.data, body);
t.is(response.age, null);
t.is(response.unitSystem, headers['sc-unit-system']);

});

test('vin', async function(t) {

const body = {
Expand Down

0 comments on commit 8728190

Please sign in to comment.