-
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(signs): Added Signs API client (EN-1573) (#1)
- Loading branch information
Showing
8 changed files
with
278 additions
and
1 deletion.
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, signs as mockSigns } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When searching for signs by name', () => { | ||
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 get a list of signs', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const signsPromise = api.customer('SYNC').signs() | ||
.withQuery('first') // Signs containing "first" in their name | ||
.getPage() | ||
.then(page => page.list) | ||
.then(signs => signs); // Do things with list of signs | ||
|
||
return signsPromise; | ||
}); | ||
}); | ||
|
||
describe('When retrieving a sign by ID', () => { | ||
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 get a sign', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const signPromise = api.customer('SYNC').sign(1) | ||
.fetch() | ||
.then(sign => sign); // Do things with sign | ||
|
||
return signPromise; | ||
}); | ||
}); |
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'; | ||
|
||
/** | ||
* Sign resource | ||
*/ | ||
class Sign extends Resource { | ||
/** | ||
* Creates a new sign | ||
* | ||
* Will populate itself with the values given to it after the client parameter | ||
* @example <caption>Assigning partial sign data to a new instance</caption> | ||
* const client = new Client(); | ||
* const partialSignData = { | ||
* href: '/1/SYNC/sign/2', | ||
* name: 'The second sign', | ||
* }; | ||
* const sign = new Sign(client, partialSignData); | ||
* | ||
* sign.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 Sign ID | ||
* @returns {string} URI to instance of sign | ||
*/ | ||
static makeHref(customerCode, id) { | ||
return { | ||
href: `/1/${customerCode}/signs/${id}`, | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches the data for this sign via the client | ||
* @returns {Promise} If successful, a hydrated instance of this sign | ||
*/ | ||
fetch() { | ||
return this.client.get(this.href) | ||
.then(response => response.json()) | ||
.then(sign => new Sign(this.client, this, sign)); | ||
} | ||
} | ||
|
||
export default Sign; |
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 Sign from './Sign'; | ||
import { signs as mockSigns } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When instantiating a sign based on customer and ID', () => { | ||
const client = new Client(); | ||
const sign = new Sign(client, Sign.makeHref('SYNC', 1)); | ||
|
||
it('should set the href', () => sign.href.should.equal('/1/SYNC/signs/1')); | ||
it('should not be hydrated', () => sign.hydrated.should.equal(false)); | ||
}); | ||
|
||
describe('When instantiating a sign based on an object', () => { | ||
const client = new Client(); | ||
const sign = new Sign(client, mockSigns.getById(1)); | ||
|
||
it('should set the ID', () => sign.id.should.equal(1)); | ||
it('should set the href', () => sign.href.should.equal('/1/SYNC/signs/1')); | ||
it('should be hydrated', () => sign.hydrated.should.equal(true)); | ||
}); | ||
|
||
describe('When fetching a sign based on customer and ID', () => { | ||
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(); | ||
}); | ||
|
||
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/signs/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 Sign from './Sign'; | ||
|
||
/** | ||
* Sign querying context | ||
* | ||
* This is used to query the list of signs for a customer | ||
*/ | ||
class SignsContext extends PagedContext { | ||
/** | ||
* Creates a new sign 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 signs = new SignsContext(...); | ||
* signs | ||
* .withQuery('12') | ||
* .getPage() | ||
* .then(page => ...); | ||
* @param {string} term Query term to search for | ||
* @returns {SignsContext} 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 Sign objects | ||
* @see Sign | ||
*/ | ||
getPage() { | ||
return this.page(Sign, `/1/${this.code}/signs`); | ||
} | ||
} | ||
|
||
export default SignsContext; |
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 SignsContext from './SignsContext'; | ||
import { signs as mockSigns } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When building a query for signs', () => { | ||
const client = new Client(); | ||
client.setAuthenticated(); | ||
|
||
beforeEach(() => fetchMock | ||
.get(client.resolve('/1/SYNC/signs?page=9&perPage=27&q=valid'), mockSigns.list) | ||
.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
const signs = new SignsContext(client, 'SYNC'); | ||
promise = signs | ||
.withPage(9) | ||
.withPerPage(27) | ||
.withQuery('valid') | ||
.getPage(); | ||
}); | ||
|
||
it('should make the expected request', () => promise.should.be.fulfilled); | ||
}); |