-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stops): Add support for getting stops for a customer (EN-1527) (#4)
feat(stops): Add support for getting stops for a customer (EN-1527) fix(stops): Added missing integration with Customer and example tests
- Loading branch information
Showing
10 changed files
with
280 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Track from '../index'; | ||
import { charlie, stops as mockStops } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When searching for stops by name', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockStops.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a list of stops', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const stopsPromise = api.customer('SYNC').stops() | ||
.withQuery('1st') // Stops containing "1st" in their name | ||
.getPage() | ||
.then(page => page.list) | ||
.then(stops => stops); // Do things with list of stops | ||
|
||
return stopsPromise; | ||
}); | ||
}); | ||
|
||
describe('When retrieving a stop by ID', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockStops.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a stop', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const stopsPromise = api.customer('SYNC').stop(1) | ||
.fetch() | ||
.then(stop => stop); // Do things with stop | ||
|
||
return stopsPromise; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* Stop resource | ||
*/ | ||
class Stop extends Resource { | ||
/** | ||
* Creates a new stop | ||
* | ||
* Will populate itself with the values given to it after the client parameter | ||
* @example <caption>Assigning partial stop data to a new instance</caption> | ||
* const client = new Client(); | ||
* const partialStopData = { | ||
* href: '/1/SYNC/stops/2', | ||
* name: '9876', | ||
* }; | ||
* const stop = new Stop(client, partialStopData); | ||
* | ||
* stop.hydrated == true; | ||
* @param {Client} client Instance of pre-configured client | ||
* @param {Array} rest Remaining arguments to use in assigning values to this instance | ||
*/ | ||
constructor(client, ...rest) { | ||
super(client); | ||
|
||
const newProperties = Object.assign({}, ...rest); | ||
const hydrated = !Object.keys(newProperties).every(k => k === 'href'); | ||
|
||
Object.assign(this, newProperties, { | ||
hydrated, | ||
}); | ||
} | ||
|
||
/** | ||
* Makes a href for a given customer code and ID | ||
* @param {string} customerCode Customer code | ||
* @param {Number} id Stop ID | ||
* @returns {string} URI to instance of stop | ||
*/ | ||
static makeHref(customerCode, id) { | ||
return { | ||
href: `/1/${customerCode}/stops/${id}`, | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches the data for this stop via the client | ||
* @returns {Promise} If successful, a hydrated instance of this stop | ||
*/ | ||
fetch() { | ||
return this.client.get(this.href) | ||
.then(response => response.json()) | ||
.then(stop => new Stop(this.client, this, stop)); | ||
} | ||
} | ||
|
||
export default Stop; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Client from '../Client'; | ||
import Stop from './Stop'; | ||
import { stops as mockStops } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When instantiating a stop based on customer and ID', () => { | ||
const client = new Client(); | ||
const stop = new Stop(client, Stop.makeHref('SYNC', 1)); | ||
|
||
it('should set the href', () => stop.href.should.equal('/1/SYNC/stops/1')); | ||
it('should not be hydrated', () => stop.hydrated.should.equal(false)); | ||
}); | ||
|
||
describe('When instantiating a stop based on an object', () => { | ||
const client = new Client(); | ||
const stop = new Stop(client, mockStops.getById(1)); | ||
|
||
it('should set the ID', () => stop.id.should.equal(1)); | ||
it('should set the href', () => stop.href.should.equal('/1/SYNC/stops/1')); | ||
it('should be hydrated', () => stop.hydrated.should.equal(true)); | ||
}); | ||
|
||
describe('When fetching a stop based on customer and ID', () => { | ||
const client = new Client(); | ||
|
||
beforeEach(() => mockStops.setUpSuccessfulMock(client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
promise = new Stop(client, Stop.makeHref('SYNC', 1)).fetch(); | ||
}); | ||
|
||
it('should resolve the promise', () => promise.should.be.fulfilled); | ||
it('should set the ID', () => promise.then(v => v.id).should.eventually.equal(1)); | ||
it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/stops/1')); | ||
it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'isomorphic-fetch'; | ||
import PagedContext from './PagedContext'; | ||
import Stop from './Stop'; | ||
|
||
/** | ||
* Stop querying context | ||
* | ||
* This is used to query the list of stops for a customer | ||
*/ | ||
class StopsContext extends PagedContext { | ||
/** | ||
* Creates a new stop context | ||
* @param {Client} client Instance of pre-configured client | ||
* @param {string} customerCode Customer code | ||
* @param {Object} params Object of querystring parameters to append to the URL | ||
*/ | ||
constructor(client, customerCode, params) { | ||
super(client, { ...params }); | ||
this.code = customerCode; | ||
} | ||
|
||
/** | ||
* Sets the query term for the context | ||
* @example | ||
* const stops = new StopContext(...); | ||
* stops | ||
* .withQuery('12') | ||
* .getPage() | ||
* .then(page => ...); | ||
* @param {string} term Query term to search for | ||
* @returns {StopsContext} Returns itself | ||
*/ | ||
withQuery(term) { | ||
this.params.q = term; | ||
return this; | ||
} | ||
|
||
/** | ||
* Gets the first page of results for this context | ||
* @returns {Promise} If successful, a page of Stop objects | ||
* @see Stop | ||
*/ | ||
getPage() { | ||
return this.page(Stop, `/1/${this.code}/stops`); | ||
} | ||
} | ||
|
||
export default StopsContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Client from '../Client'; | ||
import StopsContext from './StopsContext'; | ||
import { stops as mockStops } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When building a query for stops', () => { | ||
const client = new Client(); | ||
client.setAuthenticated(); | ||
|
||
beforeEach(() => fetchMock | ||
.get(client.resolve('/1/SYNC/stops?page=9&perPage=27&q=valid&sort='), mockStops.list) | ||
.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
const stops = new StopsContext(client, 'SYNC'); | ||
promise = stops | ||
.withPage(9) | ||
.withPerPage(27) | ||
.withQuery('valid') | ||
.getPage(); | ||
}); | ||
|
||
it('should make the expected request', () => promise.should.be.fulfilled); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters