-
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.
Merge pull request #6 from Zuver/feature/EN-1575-tags-api-client
Tags API client
- Loading branch information
Showing
9 changed files
with
231 additions
and
7 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, tags as mockTags } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When searching for tags by name', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockTags.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a list of tags', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const tagsPromise = api.customer('SYNC').tags() | ||
.withQuery('LA') // Tags containing "LA" in their name | ||
.getPage() | ||
.then(page => page.list) | ||
.then(tags => tags); // Do things with list of tags | ||
|
||
return tagsPromise; | ||
}); | ||
}); | ||
|
||
describe('When retrieving a tag by ID', () => { | ||
const api = new Track({ autoRenew: false }); | ||
|
||
beforeEach(() => charlie.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => mockTags.setUpSuccessfulMock(api.client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
it('should get a tag', () => { | ||
api.logIn({ username: '[email protected]', password: 'securepassword' }); | ||
|
||
const tagPromise = api.customer('SYNC').tag(3) | ||
.fetch() | ||
.then(tag => tag); // Do things with tag | ||
|
||
return tagPromise; | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1,18 +1,44 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import fetchMock from 'fetch-mock'; | ||
import Client from '../Client'; | ||
import Tag from './Tag'; | ||
import { tags as mockTags } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When instantiating a tag based on customer and ID', () => { | ||
const client = new Client(); | ||
const tag = new Tag(client, Tag.makeHref('SYNC', 3)); | ||
|
||
it('should set the href', () => tag.href.should.equal('/1/SYNC/tags/3')); | ||
it('should not be hydrated', () => tag.hydrated.should.equal(false)); | ||
}); | ||
|
||
describe('When instantiating a tag based on an object', () => { | ||
const client = new Client(); | ||
const mockTag = { | ||
href: '/1/SYNC/tags/1', | ||
name: 'tag', | ||
}; | ||
const tag = new Tag(client, mockTag); | ||
const tag = new Tag(client, mockTags.getById(3)); | ||
|
||
it('should set the ID', () => tag.id.should.equal(3)); | ||
it('should set the href', () => tag.href.should.equal('/1/SYNC/tags/3')); | ||
it('should be hydrated', () => tag.hydrated.should.equal(true)); | ||
}); | ||
|
||
describe('When fetching a tag based on customer and ID', () => { | ||
const client = new Client(); | ||
|
||
beforeEach(() => mockTags.setUpSuccessfulMock(client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
promise = new Tag(client, Tag.makeHref('SYNC', 3)).fetch(); | ||
}); | ||
|
||
it('should resolve the promise', () => promise.should.be.fulfilled); | ||
it('should set the ID', () => promise.then(v => v.id).should.eventually.equal(3)); | ||
it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/tags/3')); | ||
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 Tag from './Tag'; | ||
|
||
/** | ||
* Tag querying context | ||
* | ||
* This is used to query the list of tags for a customer | ||
*/ | ||
class TagsContext extends PagedContext { | ||
/** | ||
* Creates a new tag 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 tags = new TagContext(...); | ||
* tags | ||
* .withQuery('LA') | ||
* .getPage() | ||
* .then(page => ...); | ||
* @param {string} term Query term to search for | ||
* @returns {TagsContext} 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 Tag objects | ||
* @see Tag | ||
*/ | ||
getPage() { | ||
return this.page(Tag, `/1/${this.code}/tags`); | ||
} | ||
} | ||
|
||
export default TagsContext; |
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 TagsContext from './TagsContext'; | ||
import { tags as mockTags } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When building a query for tags', () => { | ||
const client = new Client(); | ||
client.setAuthenticated(); | ||
|
||
beforeEach(() => fetchMock | ||
.get(client.resolve('/1/SYNC/tags?page=9&perPage=27&q=valid&sort='), mockTags.list) | ||
.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
const tags = new TagsContext(client, 'SYNC'); | ||
promise = tags | ||
.withPage(9) | ||
.withPerPage(27) | ||
.withQuery('valid') | ||
.getPage(); | ||
}); | ||
|
||
it('should make the expected request', () => promise.should.be.fulfilled); | ||
}); |