Skip to content

Commit

Permalink
feat(signs): EN-5907: Update approach announcements for signs. (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeff Cuevas-Koch <[email protected]>
  • Loading branch information
cuevaskoch and Jeff Cuevas-Koch authored Apr 24, 2020
1 parent 5fb0d1b commit b74b14e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/examples/get_signs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,26 @@ describe('When retrieving a sign by ID', () => {
return signPromise;
});
});

describe('When updating a sign', () => {
const api = new Track({ autoRenew: false });

beforeEach(() => charlie.setUpSuccessfulMock(api.client));
beforeEach(() => mockSigns.setUpSuccessfulMock(api.client));
beforeEach(() => fetchMock.catch(503));
afterEach(fetchMock.restore);

it('should update approach announcements', () => {
api.logIn({ username: '[email protected]', password: 'securepassword' });

const signPromise = api.customer('SYNC').sign(1)
.fetch()
.then(sign => {
sign.approach_announcements_enabled = true;
sign.approach_announcements_seconds = 120;
sign.update();
});

return signPromise;
});
})
6 changes: 5 additions & 1 deletion src/mocks/signs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ const signs = {
},
});
const singleResponse = () => new Response(Client.toBlob(signs.getById(1)));
const patchResponse = () => new Response(undefined, {
headers: {},
});

fetchMock
.get(client.resolve('/1/SYNC/signs?page=1&per_page=10&q=first&sort='), listResponse)
.get(client.resolve('/1/SYNC/signs/1'), singleResponse);
.get(client.resolve('/1/SYNC/signs/1'), singleResponse)
.patch(client.resolve('/1/SYNC/signs/1'), patchResponse);
},
getById: id => signs.list.find(v => v.id === id),
list: [{
Expand Down
22 changes: 22 additions & 0 deletions src/resources/Sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ class Sign extends Resource {
.then(response => response.json())
.then(sign => new Sign(this.client, this, sign));
}

/**
* Updates data for a sign via the client.
* Note: only updates approach_announcements_enabled and approach_announcements_seconds.
* @returns {Promise} if successful returns instance of this sign
*/
update() {
return this.client.patch(this.href, {
body: [
{
op: 'replace',
path: 'approach_announcements_enabled',
value: this.approach_announcements_enabled,
},
{
op: 'replace',
path: 'approach_announcements_seconds',
value: this.approach_announcements_seconds,
},
],
});
}
}

export default Sign;
23 changes: 23 additions & 0 deletions src/resources/Sign.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,26 @@ describe('When fetching a sign based on customer and ID', () => {
it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/signs/1'));
it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true));
});

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

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

let promise;
beforeEach(() => {
promise = new Sign(client, Sign.makeHref('SYNC', 1)).fetch()
.then(sign => {
sign.approach_announcements_enabled = true;
sign.approach_announcements_seconds = 120;
return sign;
})
.then(sign => sign.update()
.then(() => sign));
});

it('should resolve the promise', () => promise.should.be.fulfilled);
it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/signs/1'));
})

0 comments on commit b74b14e

Please sign in to comment.