-
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(messageChannels): EN-6366: Add MessageChannels call to customer …
…to retrieve available channels to a customer. Feature/en 6366 add message channels support (#80) * feat(messageChannels): EN-6366: Add MessageChannels call to customer to retrieve available channels to a customer. * simplify to a single call, to get message channels
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 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
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,23 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import fetchMock from 'fetch-mock'; | ||
import Client from '../Client'; | ||
|
||
const messageChannels = { | ||
setUpSuccessfulMock: (client) => { | ||
const listResponse = () => new Response( | ||
Client.toBlob(messageChannels.list), { | ||
headers: { | ||
Link: '</1/SYNC/messages_channels>', | ||
}, | ||
}); | ||
fetchMock | ||
.get(client.resolve('/1/SYNC/message_channels'), listResponse); | ||
}, | ||
getByName: name => messageChannels.list.find(v => v.name === name), | ||
list: [{ | ||
href: '/1/SYNC/message_channels/Signs', | ||
name: 'Signs', | ||
}], | ||
}; | ||
|
||
export default messageChannels; |
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,54 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* MessageChannels resource | ||
*/ | ||
class MessageChannels extends Resource { | ||
/** | ||
* Creates a new MessageChannels. | ||
* | ||
* Will populate itself with the values given to it after the client parameter | ||
* @example <caption>Assigning partial message channels data to a new instance</caption> | ||
* const client = new Client(); | ||
* const partialMessageChannelsData = [{ | ||
* href: '/1/SYNC/message_channel/GTFS-RT', | ||
* name: 'GTFS-RT', | ||
* }]; | ||
* const messageChannel = new MessageChannel(client, partialMessageChannelsData); | ||
* messageChannel.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' || k === 'code'); | ||
|
||
Object.assign(this, newProperties, { hydrated }); | ||
} | ||
|
||
/** | ||
* Makes a href for a given customer code | ||
* @param {string} customerCode Customer code | ||
* @returns {{href: string}} URI to instance of messageChannels | ||
*/ | ||
static makeHref(customerCode) { | ||
return { | ||
href: `/1/${customerCode}/message_channels`, | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches the data for this message channel via the client | ||
* @returns {Promise} If successful, a hydrated instance of this messageChannels | ||
*/ | ||
fetch() { | ||
return this.client.get(this.href) | ||
.then(response => response.json()) | ||
.then(messageChannels => new MessageChannels(this.client, this, messageChannels)); | ||
} | ||
} | ||
|
||
export default MessageChannels; |
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,34 @@ | ||
import chai from 'chai'; | ||
import fetchMock from 'fetch-mock'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import Client from '../Client'; | ||
import MessageChannels from './MessageChannels'; | ||
import { messageChannels as mockMessageChannels } from '../mocks'; | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('When instantiating a message channel based on customer and name', () => { | ||
const client = new Client(); | ||
const messageChannels = new MessageChannels(client, MessageChannels.makeHref('SYNC')); | ||
|
||
it('should set the href', () => messageChannels.href.should.equal('/1/SYNC/message_channels')); | ||
it('should not be hydrated', () => messageChannels.hydrated.should.equal(false)); | ||
}); | ||
|
||
describe('When fetching message channels based on customer', () => { | ||
const client = new Client(); | ||
|
||
beforeEach(() => mockMessageChannels.setUpSuccessfulMock(client)); | ||
beforeEach(() => fetchMock.catch(503)); | ||
afterEach(fetchMock.restore); | ||
|
||
let promise; | ||
beforeEach(() => { | ||
promise = new MessageChannels(client, MessageChannels.makeHref('SYNC')).fetch(); | ||
}); | ||
|
||
it('should resolve the promise', () => promise.should.be.fulfilled); | ||
it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/message_channels')); | ||
it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true)); | ||
}); |