Skip to content

Commit

Permalink
Merge pull request #8 from sealink/RB-128-updated-vehiclelink-endpoints
Browse files Browse the repository at this point in the history
[RB-128] Update vehiclelink api endpoints
  • Loading branch information
Damon1024 committed Dec 13, 2021
2 parents 9d5aec5 + fb066de commit 9378163
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

- [RB-128] Update vehiclelink endpoints

## 0.1.1

- [RB-96] Fix npm badge
Expand Down
12 changes: 6 additions & 6 deletions lib/vehiclelinkApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ class VehiclelinkApi {
};
}

fetchMakes() {
const url = `${this.host}/makes`;
fetchMakes(segmentCode) {
const url = `${this.host}/segments/${segmentCode}/makes`;
return request(url, this.makeGetRequest());
}

fetchFamilies(makeCode) {
fetchFamilies(segmentCode, makeCode) {
const params = new URLSearchParams();
params.append('make_code', makeCode);
const url = `${this.host}/families?${params}`;
const url = `${this.host}/segments/${segmentCode}/families?${params}`;
return request(url, this.makeGetRequest());
}

fetchVehicles(makeCode, familyCode, bodyStyleDescription) {
fetchVehicles(segmentCode, makeCode, familyCode, bodyStyleDescription) {
const params = new URLSearchParams();
params.append('make_code', makeCode);
params.append('family_code', familyCode);
params.append('body_style_description', bodyStyleDescription);
const url = `${this.host}/vehicles?${params}`;
const url = `${this.host}/segments/${segmentCode}/vehicles?${params}`;
return request(url, this.makeGetRequest());
}
}
Expand Down
52 changes: 29 additions & 23 deletions test/vehiclelinkApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const configHeaders = {
describe('errorHandling', () => {
beforeEach(() => {
nock(host, { reqHeaders: configHeaders })
.get('/makes')
.get('/segments/vehicles/makes')
.reply(422, { error: 'Test error' });

nock(host, { reqHeaders: configHeaders })
.get('/families?make_code=TOYO')
.get('/segments/vehicles/families?make_code=TOYO')
.reply(500, []);

nock(host, { reqHeaders: configHeaders })
.get(
'/vehicles?make_code=TOYO&family_code=PRADO&body_style_description=Style+1'
'/segments/vehicles/vehicles?make_code=TOYO&family_code=PRADO&body_style_description=Style+1'
)
.reply(500, []);
});
Expand All @@ -30,22 +30,26 @@ describe('errorHandling', () => {
});

it('should handle errors when fetch makes', (done) => {
new VehiclelinkApi(host, bearerToken).fetchMakes().catch((err) => {
expect(err.response.status).toEqual(422);
done();
});
new VehiclelinkApi(host, bearerToken)
.fetchMakes('vehicles')
.catch((err) => {
expect(err.response.status).toEqual(422);
done();
});
});

it('should handle errors when fetch families', (done) => {
new VehiclelinkApi(host, bearerToken).fetchFamilies('TOYO').catch((err) => {
expect(err.response.status).toEqual(500);
done();
});
new VehiclelinkApi(host, bearerToken)
.fetchFamilies('vehicles', 'TOYO')
.catch((err) => {
expect(err.response.status).toEqual(500);
done();
});
});

it('should handle errors when fetch vehicles', (done) => {
new VehiclelinkApi(host, bearerToken)
.fetchVehicles('TOYO', 'PRADO', 'Style 1')
.fetchVehicles('vehicles', 'TOYO', 'PRADO', 'Style 1')
.catch((err) => {
expect(err.response.status).toEqual(500);
done();
Expand All @@ -61,17 +65,19 @@ describe('fetchMakes', () => {
];

nock(host, { reqHeaders: configHeaders })
.get('/makes')
.get('/segments/vehicles/makes')
.reply(200, makesResults);
});

it('should return a hash of makes', (done) => {
new VehiclelinkApi(host, bearerToken).fetchMakes().then((makes) => {
expect(makes).toHaveLength(2);
expect(makes[0].description).toEqual('Toyota');
expect(makes[1].description).toEqual('Mazda');
done();
});
new VehiclelinkApi(host, bearerToken)
.fetchMakes('vehicles')
.then((makes) => {
expect(makes).toHaveLength(2);
expect(makes[0].description).toEqual('Toyota');
expect(makes[1].description).toEqual('Mazda');
done();
});
});
});

Expand All @@ -98,13 +104,13 @@ describe('fetchFamilies', () => {
];

nock(host, { reqHeaders: configHeaders })
.get('/families?make_code=TOYO')
.get('/segments/vehicles/families?make_code=TOYO')
.reply(200, familiesResults);
});

it('should return a hash of families', (done) => {
new VehiclelinkApi(host, bearerToken)
.fetchFamilies('TOYO')
.fetchFamilies('vehicles', 'TOYO')
.then((families) => {
expect(families).toHaveLength(2);
expect(families[0].description).toEqual('PRADO');
Expand Down Expand Up @@ -140,13 +146,13 @@ describe('fetchVehicles', () => {
params.append('body_style_description', 'Style 1');

nock(host, { reqHeaders: configHeaders })
.get(`/vehicles?${params}`)
.get(`/segments/vehicles/vehicles?${params}`)
.reply(200, vehiclesResults);
});

it('should return a hash of families', (done) => {
new VehiclelinkApi(host, bearerToken)
.fetchVehicles('TOYO', 'PRADO', 'Style 1')
.fetchVehicles('vehicles', 'TOYO', 'PRADO', 'Style 1')
.then((vehicles) => {
expect(vehicles).toHaveLength(1);
expect(vehicles[0].length_value).toEqual('5100');
Expand Down

0 comments on commit 9378163

Please sign in to comment.