diff --git a/src/mocks/drivers.js b/src/mocks/drivers.js index c500fac..4ec8003 100644 --- a/src/mocks/drivers.js +++ b/src/mocks/drivers.js @@ -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: [{ diff --git a/src/resources/Driver.js b/src/resources/Driver.js index fd1c0ff..0c40c2c 100644 --- a/src/resources/Driver.js +++ b/src/resources/Driver.js @@ -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 }); } @@ -40,6 +40,7 @@ class Driver extends Resource { static makeHref(customerCode, id) { return { href: `/1/${customerCode}/drivers/${id}`, + code: customerCode, }; } @@ -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 })); } } diff --git a/src/resources/Driver.test.js b/src/resources/Driver.test.js index db79a8b..85b5aa8 100644 --- a/src/resources/Driver.test.js +++ b/src/resources/Driver.test.js @@ -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')); +});