Skip to content

Commit 233c15a

Browse files
dkaminskifrancisminu
and
francisminu
authored
feat(drivers): EN-7633 Update drivers (#101)
* 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]>
1 parent 9698d2c commit 233c15a

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/mocks/drivers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ const drivers = {
1111
},
1212
});
1313
const singleResponse = () => new Response(Client.toBlob(drivers.getById(1)));
14+
const putResponse = () => new Response(undefined, {
15+
headers: {
16+
Location: '/1/SYNC/drivers/1',
17+
},
18+
});
1419

1520
fetchMock
1621
.get(client.resolve('/1/SYNC/drivers?page=1&per_page=10&sort='), listResponse)
1722
.get(client.resolve('/1/SYNC/drivers?page=1&per_page=10&q=charlie&sort='), listResponse)
18-
.get(client.resolve('/1/SYNC/drivers/1'), singleResponse);
23+
.get(client.resolve('/1/SYNC/drivers/1'), singleResponse)
24+
.put(client.resolve('/1/SYNC/drivers/1'), putResponse);
1925
},
2026
getById: id => drivers.list.find(v => v.id === id),
2127
list: [{

src/resources/Driver.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class Driver extends Resource {
2323
* @param {Client} client Instance of pre-configured client
2424
* @param {Array} rest Remaining arguments to use in assigning values to this instance
2525
*/
26-
constructor(client, ...rest) {
26+
constructor(client, rest) {
2727
super(client);
28-
29-
const newProperties = Object.assign({}, ...rest);
30-
const hydrated = !Object.keys(newProperties).every(k => k === 'href');
28+
const { code, ...newProperties } = rest;
29+
this.customerCode = code;
30+
const hydrated = !Object.keys(newProperties).every(k => k === 'href' || k === 'customerCode');
3131
Object.assign(this, newProperties, { hydrated });
3232
}
3333

@@ -40,6 +40,7 @@ class Driver extends Resource {
4040
static makeHref(customerCode, id) {
4141
return {
4242
href: `/1/${customerCode}/drivers/${id}`,
43+
code: customerCode,
4344
};
4445
}
4546

@@ -50,7 +51,19 @@ class Driver extends Resource {
5051
fetch() {
5152
return this.client.get(this.href)
5253
.then(response => response.json())
53-
.then(driver => new Driver(this.client, this, driver));
54+
.then(driver => new Driver(this.client, { ...this, ...driver }));
55+
}
56+
57+
/**
58+
* Updates data for a driver via the client
59+
* @returns {Promise} If successful, returns instance of this driver
60+
*/
61+
update() {
62+
// eslint-disable-next-line no-unused-vars
63+
const { client, hydrated, customerCode, ...body } = this;
64+
const { href } = Driver.makeHref(this.customerCode, this.id);
65+
return this.client.put(href, { body })
66+
.then(() => new Driver(this.client, { ...this }));
5467
}
5568
}
5669

src/resources/Driver.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,35 @@ describe('When fetching a driver based on customer and ID', () => {
5050
it('should have the expected first name', () => promise.then(p => p.first_name).should.eventually.equal('Charlie'));
5151
it('should have the expected last name', () => promise.then(p => p.last_name).should.eventually.equal('Singh'));
5252
});
53+
54+
describe('When updating a driver for a customer', () => {
55+
const client = new Client();
56+
57+
beforeEach(() => mockDrivers.setUpSuccessfulMock(client));
58+
beforeEach(() => fetchMock.catch(503));
59+
afterEach(fetchMock.restore);
60+
61+
let promise;
62+
beforeEach(() => {
63+
promise = new Driver(client, Driver.makeHref('SYNC', 1))
64+
.fetch()
65+
.then((driver) => {
66+
/* eslint-disable no-param-reassign */
67+
driver.customer_driver_id = '0002';
68+
driver.first_name = 'Charlotte';
69+
driver.last_name = 'Song';
70+
/* eslint-enable no-param-reassign */
71+
return driver.update();
72+
})
73+
.then(driver => driver);
74+
});
75+
76+
it('should resolve the promise', () => promise.should.be.fulfilled);
77+
it('should set the href', () => promise.then(p => p.href).should.eventually.equal('/1/SYNC/drivers/1'));
78+
it('should have the expected customer driver id', () => promise.then(p => p.customer_driver_id)
79+
.should.eventually.equal('0002'));
80+
it('should have the expected first name', () => promise.then(p => p.first_name)
81+
.should.eventually.equal('Charlotte'));
82+
it('should have the expected last name', () => promise.then(p => p.last_name)
83+
.should.eventually.equal('Song'));
84+
});

0 commit comments

Comments
 (0)