Skip to content

Commit

Permalink
feat(drivers): EN-7633 Update drivers (#101)
Browse files Browse the repository at this point in the history
* feat(drivers): EN-7633 Update drivers

* (+semver:fix) feat: added endpoint for creating a new driver

* Revert "(+semver:fix) feat: added endpoint for creating a new driver"

This reverts commit 70ec9e0.

Co-authored-by: francisminu <[email protected]>
  • Loading branch information
dkaminski and francisminu authored Aug 30, 2021
1 parent 9698d2c commit 233c15a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/mocks/drivers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ const drivers = {
},
});
const singleResponse = () => new Response(Client.toBlob(drivers.getById(1)));
const putResponse = () => new Response(undefined, {
headers: {
Location: '/1/SYNC/drivers/1',
},
});

fetchMock
.get(client.resolve('/1/SYNC/drivers?page=1&per_page=10&sort='), listResponse)
.get(client.resolve('/1/SYNC/drivers?page=1&per_page=10&q=charlie&sort='), listResponse)
.get(client.resolve('/1/SYNC/drivers/1'), singleResponse);
.get(client.resolve('/1/SYNC/drivers/1'), singleResponse)
.put(client.resolve('/1/SYNC/drivers/1'), putResponse);
},
getById: id => drivers.list.find(v => v.id === id),
list: [{
Expand Down
23 changes: 18 additions & 5 deletions src/resources/Driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Driver extends Resource {
* @param {Client} client Instance of pre-configured client
* @param {Array} rest Remaining arguments to use in assigning values to this instance
*/
constructor(client, ...rest) {
constructor(client, rest) {
super(client);

const newProperties = Object.assign({}, ...rest);
const hydrated = !Object.keys(newProperties).every(k => k === 'href');
const { code, ...newProperties } = rest;
this.customerCode = code;
const hydrated = !Object.keys(newProperties).every(k => k === 'href' || k === 'customerCode');
Object.assign(this, newProperties, { hydrated });
}

Expand All @@ -40,6 +40,7 @@ class Driver extends Resource {
static makeHref(customerCode, id) {
return {
href: `/1/${customerCode}/drivers/${id}`,
code: customerCode,
};
}

Expand All @@ -50,7 +51,19 @@ class Driver extends Resource {
fetch() {
return this.client.get(this.href)
.then(response => response.json())
.then(driver => new Driver(this.client, this, driver));
.then(driver => new Driver(this.client, { ...this, ...driver }));
}

/**
* Updates data for a driver via the client
* @returns {Promise} If successful, returns instance of this driver
*/
update() {
// eslint-disable-next-line no-unused-vars
const { client, hydrated, customerCode, ...body } = this;
const { href } = Driver.makeHref(this.customerCode, this.id);
return this.client.put(href, { body })
.then(() => new Driver(this.client, { ...this }));
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/resources/Driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,35 @@ describe('When fetching a driver based on customer and ID', () => {
it('should have the expected first name', () => promise.then(p => p.first_name).should.eventually.equal('Charlie'));
it('should have the expected last name', () => promise.then(p => p.last_name).should.eventually.equal('Singh'));
});

describe('When updating a driver for a customer', () => {
const client = new Client();

beforeEach(() => mockDrivers.setUpSuccessfulMock(client));
beforeEach(() => fetchMock.catch(503));
afterEach(fetchMock.restore);

let promise;
beforeEach(() => {
promise = new Driver(client, Driver.makeHref('SYNC', 1))
.fetch()
.then((driver) => {
/* eslint-disable no-param-reassign */
driver.customer_driver_id = '0002';
driver.first_name = 'Charlotte';
driver.last_name = 'Song';
/* eslint-enable no-param-reassign */
return driver.update();
})
.then(driver => driver);
});

it('should resolve the promise', () => promise.should.be.fulfilled);
it('should set the href', () => promise.then(p => p.href).should.eventually.equal('/1/SYNC/drivers/1'));
it('should have the expected customer driver id', () => promise.then(p => p.customer_driver_id)
.should.eventually.equal('0002'));
it('should have the expected first name', () => promise.then(p => p.first_name)
.should.eventually.equal('Charlotte'));
it('should have the expected last name', () => promise.then(p => p.last_name)
.should.eventually.equal('Song'));
});

0 comments on commit 233c15a

Please sign in to comment.