From f089a0be6c16d620cea19b8cb8fdd475e18decb1 Mon Sep 17 00:00:00 2001 From: Jeff Koch <4649003+jrkoch@users.noreply.github.com> Date: Tue, 18 Dec 2018 13:23:26 -0800 Subject: [PATCH] feat(messages): Use updated message schema (#51) (#53) --- src/examples/get_message_templates.test.js | 130 ------------------ src/examples/get_messages.test.js | 115 ++++++++++++++++ src/mocks/index.js | 2 +- src/mocks/messageTemplates.js | 59 -------- src/mocks/messages.js | 54 ++++++++ src/resources/Customer.js | 24 ++-- src/resources/Customer.test.js | 8 +- .../{MessageTemplate.js => Message.js} | 57 ++++---- ...essageTemplate.test.js => Message.test.js} | 58 ++++---- ...TemplatesContext.js => MessagesContext.js} | 24 ++-- ...ontext.test.js => MessagesContext.test.js} | 12 +- src/resources/SignMessage.js | 5 - src/resources/SignMessage.test.js | 14 +- src/resources/SignMessageSchedule.js | 25 ---- src/resources/SignMessageSchedule.test.js | 16 --- 15 files changed, 268 insertions(+), 335 deletions(-) delete mode 100644 src/examples/get_message_templates.test.js create mode 100644 src/examples/get_messages.test.js delete mode 100644 src/mocks/messageTemplates.js create mode 100644 src/mocks/messages.js rename src/resources/{MessageTemplate.js => Message.js} (55%) rename src/resources/{MessageTemplate.test.js => Message.test.js} (66%) rename src/resources/{MessageTemplatesContext.js => MessagesContext.js} (54%) rename src/resources/{MessageTemplatesContext.test.js => MessagesContext.test.js} (56%) delete mode 100644 src/resources/SignMessageSchedule.js delete mode 100644 src/resources/SignMessageSchedule.test.js diff --git a/src/examples/get_message_templates.test.js b/src/examples/get_message_templates.test.js deleted file mode 100644 index 8e3e5f5..0000000 --- a/src/examples/get_message_templates.test.js +++ /dev/null @@ -1,130 +0,0 @@ -import chai from 'chai'; -import chaiAsPromised from 'chai-as-promised'; -import fetchMock from 'fetch-mock'; -import Track from '../index'; -import { charlie, messageTemplates as mockMessageTemplates } from '../mocks'; - -chai.should(); -chai.use(chaiAsPromised); - -describe('When searching for message templates by name', () => { - const api = new Track({ autoRenew: false }); - - beforeEach(() => charlie.setUpSuccessfulMock(api.client)); - beforeEach(() => mockMessageTemplates.setUpSuccessfulMock(api.client)); - beforeEach(() => fetchMock.catch(503)); - afterEach(fetchMock.restore); - - it('should get a list of message templates', () => { - api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); - - const messageTemplatesPromise = api.customer('SYNC').messageTemplates() - .withQuery('5k') // MessageTemplates containing "5k" in their name - .getPage() - .then(page => page.list) - .then(messageTemplates => messageTemplates); // Do things with list of message templates - - return messageTemplatesPromise; - }); -}); - -describe('When retrieving a message template by ID', () => { - const api = new Track({ autoRenew: false }); - - beforeEach(() => charlie.setUpSuccessfulMock(api.client)); - beforeEach(() => mockMessageTemplates.setUpSuccessfulMock(api.client)); - beforeEach(() => fetchMock.catch(503)); - afterEach(fetchMock.restore); - - it('should get a message template', () => { - api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); - - const messageTemplatesPromise = api.customer('SYNC').messageTemplate(1) - .fetch() - .then(messageTemplate => messageTemplate); // Do things with messageTemplate - - return messageTemplatesPromise; - }); -}); - -describe('When creating a message template', () => { - const api = new Track({ autoRenew: false }); - - beforeEach(() => charlie.setUpSuccessfulMock(api.client)); - beforeEach(() => mockMessageTemplates.setUpSuccessfulMock(api.client)); - beforeEach(() => fetchMock.catch(503)); - afterEach(fetchMock.restore); - - it('should save a message template', () => { - api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); - - const messageTemplatesPromise = api.customer('SYNC').messageTemplate({ - href: '/1/SYNC/message_templates/1', - id: 1, - name: '5k Detour', - text: 'Due to the 5k Race, buses will detour off Figueroa from 6pm to 11am on 2/15/17. Find northbound buses on Hope, southbound buses on Flower.', - start: '2017-02-12T08:00:00-08:00', - end: '2017-02-15T11:00:00-08:00', - sign_messages: [ - { - id: 1, - override_text: 'Due to the 5k, buses will be on detour.', - schedules: [ - { - day_of_week: 1, - start: '06:00:00', - end: '18:00:00', - }, - ], - tags: [ - { - href: '/1/SYNC/tags/1', - }, - ], - routes: [ - { - href: '/1/SYNC/routes/1', - }, - ], - stops: [ - { - href: '/1/SYNC/stops/1', - }, - ], - signs: [ - { - href: '/1/SYNC/signs/1', - }, - ], - }, - ], - }) - .create() - .then(messageTemplate => messageTemplate); // Do things with messageTemplate - - return messageTemplatesPromise; - }); -}); - -describe('When updating a message template', () => { - const api = new Track({ autoRenew: false }); - - beforeEach(() => charlie.setUpSuccessfulMock(api.client)); - beforeEach(() => mockMessageTemplates.setUpSuccessfulMock(api.client)); - beforeEach(() => fetchMock.catch(503)); - afterEach(fetchMock.restore); - - it('should update a message template', () => { - api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); - - const messageTemplatesPromise = api.customer('SYNC').messageTemplate(1) - .fetch() - .then((template) => { - // eslint-disable-next-line no-param-reassign - template.name = 'Updated Template Name'; - return template.update(); - }); - - return messageTemplatesPromise; - }); -}); diff --git a/src/examples/get_messages.test.js b/src/examples/get_messages.test.js new file mode 100644 index 0000000..004d0c4 --- /dev/null +++ b/src/examples/get_messages.test.js @@ -0,0 +1,115 @@ +import chai from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import fetchMock from 'fetch-mock'; +import Track from '../index'; +import { charlie, messages as mockMessages } from '../mocks'; + +chai.should(); +chai.use(chaiAsPromised); + +describe('When searching for messages by name', () => { + const api = new Track({ autoRenew: false }); + + beforeEach(() => charlie.setUpSuccessfulMock(api.client)); + beforeEach(() => mockMessages.setUpSuccessfulMock(api.client)); + beforeEach(() => fetchMock.catch(503)); + afterEach(fetchMock.restore); + + it('should get a list of messages', () => { + api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); + + const messagesPromise = api.customer('SYNC').messages() + .withQuery('5k') // Messages containing "5k" in their name + .getPage() + .then(page => page.list) + .then(messages => messages); // Do things with list of messages + + return messagesPromise; + }); +}); + +describe('When retrieving a message by ID', () => { + const api = new Track({ autoRenew: false }); + + beforeEach(() => charlie.setUpSuccessfulMock(api.client)); + beforeEach(() => mockMessages.setUpSuccessfulMock(api.client)); + beforeEach(() => fetchMock.catch(503)); + afterEach(fetchMock.restore); + + it('should get a message', () => { + api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); + + const messagePromise = api.customer('SYNC').message(1) + .fetch() + .then(message => message); // Do things with message + + return messagePromise; + }); +}); + +describe('When creating a message', () => { + const api = new Track({ autoRenew: false }); + + beforeEach(() => charlie.setUpSuccessfulMock(api.client)); + beforeEach(() => mockMessages.setUpSuccessfulMock(api.client)); + beforeEach(() => fetchMock.catch(503)); + afterEach(fetchMock.restore); + + it('should save a message', () => { + api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); + + const messagePromise = api.customer('SYNC') + .message({ + href: '/1/SYNC/messages/1', + id: 1, + name: '5k Detour', + text: 'Due to the 5k Race, buses will detour off Figueroa from 6pm to 11am on 2/15/17. Find northbound buses on Hope, southbound buses on Flower.', + start_date: '2017-02-12T08:00:00-08:00', + end_date: '2017-02-15T11:00:00-08:00', + start_time: '08:00:00', + duration: '02:00:00', + days_of_week: 'Monday', + manual_archive_date: null, + tags: [{ + href: '/1/SYNC/tags/1', + }], + routes: [{ + href: '/1/SYNC/routes/1', + }], + stops: [{ + href: '/1/SYNC/stops/1', + }], + sign_messages: [{ + id: 1, + override_text: 'Bus Detour Due to 5k Race', + }], + }) + .create() + .then(message => message); // Do things with message + + return messagePromise; + }); +}); + +describe('When updating a message', () => { + const api = new Track({ autoRenew: false }); + + beforeEach(() => charlie.setUpSuccessfulMock(api.client)); + beforeEach(() => mockMessages.setUpSuccessfulMock(api.client)); + beforeEach(() => fetchMock.catch(503)); + afterEach(fetchMock.restore); + + it('should update a message', () => { + api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); + + const messagePromise = api.customer('SYNC').message(1) + .fetch() + .then((message) => { + // eslint-disable-next-line no-param-reassign + message.name = 'Updated Message Name'; + return message.update(); + }); + + return messagePromise; + }); +}); diff --git a/src/mocks/index.js b/src/mocks/index.js index f8268e3..600ed7e 100644 --- a/src/mocks/index.js +++ b/src/mocks/index.js @@ -8,7 +8,7 @@ export { default as dispatchMessages } from './dispatchMessages'; export { default as dispatchMessageBatches } from './dispatchMessageBatches'; export { default as drivers } from './drivers'; export { default as externalApis } from './externalApis'; -export { default as messageTemplates } from './messageTemplates'; +export { default as messages } from './messages'; export { default as patterns } from './patterns'; export { default as realTime } from './realTime'; export { default as routes } from './routes'; diff --git a/src/mocks/messageTemplates.js b/src/mocks/messageTemplates.js deleted file mode 100644 index 39ab471..0000000 --- a/src/mocks/messageTemplates.js +++ /dev/null @@ -1,59 +0,0 @@ -// eslint-disable-next-line import/no-extraneous-dependencies -import fetchMock from 'fetch-mock'; -import Client from '../Client'; - -const messageTemplates = { - setUpSuccessfulMock: (client) => { - const listResponse = () => new Response( - Client.toBlob(messageTemplates.list), { - headers: { - Link: '; rel="next", ; rel="last"', - }, - }); - const singleResponse = () => new Response(Client.toBlob(messageTemplates.getById(1))); - const createResponse = () => new Response(undefined, { - headers: { - Location: '/1/SYNC/message_templates/1', - }, - }); - - fetchMock - .get(client.resolve('/1/SYNC/message_templates?page=1&per_page=10&q=5k&sort='), listResponse) - .get(client.resolve('/1/SYNC/message_templates/1'), singleResponse) - .post(client.resolve('/1/SYNC/message_templates'), createResponse) - .put(client.resolve('/1/SYNC/message_templates/1'), createResponse); - }, - getById: id => messageTemplates.list.find(v => v.id === id), - list: [{ - href: '/1/SYNC/message_templates/1', - id: 1, - name: '5k Detour', - text: 'Due to the 5k Race, buses will detour off Figueroa from 6pm to 11am on 2/15/17. Find northbound buses on Hope, southbound buses on Flower.', - start: '2017-02-12T08:00:00-08:00', - end: '2017-02-15T11:00:00-08:00', - manual_archive_date: null, - sign_messages: [{ - id: 1, - override_text: 'Due to the 5k, buses will be on detour.', - schedules: [{ - day_of_week: 1, - start: '06:00:00', - end: '18:00:00', - }], - tags: [{ - href: '/1/SYNC/tags/1', - }], - routes: [{ - href: '/1/SYNC/routes/1', - }], - stops: [{ - href: '/1/SYNC/stops/1', - }], - signs: [{ - href: '/1/SYNC/signs/1', - }], - }], - }], -}; - -export default messageTemplates; diff --git a/src/mocks/messages.js b/src/mocks/messages.js new file mode 100644 index 0000000..8405b36 --- /dev/null +++ b/src/mocks/messages.js @@ -0,0 +1,54 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import fetchMock from 'fetch-mock'; +import Client from '../Client'; + +const messages = { + setUpSuccessfulMock: (client) => { + const listResponse = () => new Response( + Client.toBlob(messages.list), { + headers: { + Link: '; rel="next", ; rel="last"', + }, + }); + const singleResponse = () => new Response(Client.toBlob(messages.getById(1))); + const createResponse = () => new Response(undefined, { + headers: { + Location: '/1/SYNC/messages/1', + }, + }); + + fetchMock + .get(client.resolve('/1/SYNC/messages?page=1&per_page=10&q=5k&sort='), listResponse) + .get(client.resolve('/1/SYNC/messages/1'), singleResponse) + .post(client.resolve('/1/SYNC/messages'), createResponse) + .put(client.resolve('/1/SYNC/messages/1'), createResponse); + }, + getById: id => messages.list.find(v => v.id === id), + list: [{ + href: '/1/SYNC/messages/1', + id: 1, + name: '5k Detour', + text: 'Due to the 5k Race, buses will detour off Figueroa from 6pm to 11am on 2/15/17. Find northbound buses on Hope, southbound buses on Flower.', + start_date: '2017-02-12T08:00:00-08:00', + end_date: '2017-02-15T11:00:00-08:00', + start_time: '08:00:00', + duration: '02:00:00', + days_of_week: 'Monday', + manual_archive_date: null, + tags: [{ + href: '/1/SYNC/tags/1', + }], + routes: [{ + href: '/1/SYNC/routes/1', + }], + stops: [{ + href: '/1/SYNC/stops/1', + }], + sign_messages: [{ + id: 1, + override_text: 'Bus Detour Due to 5k Race', + }], + }], +}; + +export default messages; diff --git a/src/resources/Customer.js b/src/resources/Customer.js index 2d48151..b6c9cb8 100644 --- a/src/resources/Customer.js +++ b/src/resources/Customer.js @@ -10,8 +10,8 @@ import Driver from './Driver'; import DriversContext from './DriversContext'; import ExternalApi from './ExternalApi'; import ExternalApisContext from './ExternalApisContext'; -import MessageTemplate from './MessageTemplate'; -import MessageTemplatesContext from './MessageTemplatesContext'; +import Message from './Message'; +import MessagesContext from './MessagesContext'; import Pattern from './Pattern'; import PatternsContext from './PatternsContext'; import Route from './Route'; @@ -151,23 +151,23 @@ class Customer extends Resource { } /** - * Gets a context for querying this customer's message templates - * @returns {MessageTemplatesContext} Context for querying this customer's message templates + * Gets a context for querying this customer's messages + * @returns {MessagesContext} Context for querying this customer's messages */ - messageTemplates() { - return this.resource(MessageTemplatesContext, this.code); + messages() { + return this.resource(MessagesContext, this.code); } /** - * Gets a message template resource by id - * @param {Object} payload Identity of the message_template or object representing a new template - * @returns {MessageTemplate} MessageTemplate resource + * Gets a message resource by id + * @param {Object} payload Identity of the message or object representing a new message + * @returns {Message} Message resource */ - messageTemplate(payload) { + message(payload) { if (!isNaN(parseFloat(payload)) && isFinite(payload)) { - return this.resource(MessageTemplate, MessageTemplate.makeHref(this.code, payload)); + return this.resource(Message, Message.makeHref(this.code, payload)); } - return this.resource(MessageTemplate, { code: this.code, ...payload }); + return this.resource(Message, { code: this.code, ...payload }); } /** diff --git a/src/resources/Customer.test.js b/src/resources/Customer.test.js index 881038a..c9b0163 100644 --- a/src/resources/Customer.test.js +++ b/src/resources/Customer.test.js @@ -12,8 +12,8 @@ import Driver from './Driver'; import DriversContext from './DriversContext'; import ExternalApi from './ExternalApi'; import ExternalApisContext from './ExternalApisContext'; -import MessageTemplate from './MessageTemplate'; -import MessageTemplatesContext from './MessageTemplatesContext'; +import Message from './Message'; +import MessagesContext from './MessagesContext'; import Pattern from './Pattern'; import PatternsContext from './PatternsContext'; import Route from './Route'; @@ -52,8 +52,8 @@ describe('When getting resources related to a customer', () => { it('should allow a driver to be retrieved', () => customer.driver().should.be.instanceOf(Driver)); it('should allow external apis to be searched', () => customer.externalApis().should.be.instanceOf(ExternalApisContext)); it('should allow an external api to be retrieved', () => customer.externalApi().should.be.instanceOf(ExternalApi)); - it('should allow message templates to be searched', () => customer.messageTemplates().should.be.instanceof(MessageTemplatesContext)); - it('should allow a message template to be retrieved', () => customer.messageTemplate().should.be.instanceof(MessageTemplate)); + it('should allow messages to be searched', () => customer.messages().should.be.instanceof(MessagesContext)); + it('should allow a message to be retrieved', () => customer.message().should.be.instanceof(Message)); it('should allow patterns to be searched', () => customer.patterns().should.be.instanceof(PatternsContext)); it('should allow a pattern to be retrieved', () => customer.pattern().should.be.instanceof(Pattern)); it('should allow routes to be searched', () => customer.routes().should.be.instanceof(RoutesContext)); diff --git a/src/resources/MessageTemplate.js b/src/resources/Message.js similarity index 55% rename from src/resources/MessageTemplate.js rename to src/resources/Message.js index aa4ca90..8ceb590 100644 --- a/src/resources/MessageTemplate.js +++ b/src/resources/Message.js @@ -1,23 +1,26 @@ import Resource from './Resource'; import SignMessage from './SignMessage'; +import Route from './Route'; +import Stop from './Stop'; +import Tag from './Tag'; /** - * MessageTemplate resource + * Message resource */ -class MessageTemplate extends Resource { +class Message extends Resource { /** - * Creates a new message template + * Creates a new message * * Will populate itself with the values given to it after the client parameter - * @example Assigning partial messageTemplate data to a new instance + * @example Assigning partial message data to a new instance * const client = new Client(); - * const partialMessageTemplateData = { - * href: '/1/SYNC/message_templates/1', + * const partialMessageData = { + * href: '/1/SYNC/messages/1', * name: '5k Detour', * text: 'Due to the 5k Race...', * }; - * const messageTemplate = new MessageTemplate(client, partialMessageTemplateData); + * const message = new Message(client, partialMessageData); * - * messageTemplate.hydrated == true; + * message.hydrated == true; * @param {Client} client Instance of pre-configured client * @param {Array} rest Remaining arguments to use in assigning values to this instance */ @@ -27,8 +30,14 @@ class MessageTemplate extends Resource { this.customerCode = code; const hydrated = !Object.keys(newProperties).every(k => k === 'href' || k === 'customerCode'); const references = { + routes: newProperties.routes + && newProperties.routes.map(r => new Route(this.client, r)), sign_messages: newProperties.sign_messages && newProperties.sign_messages.map(sm => new SignMessage(this.client, sm)), + stops: newProperties.stops + && newProperties.stops.map(s => new Stop(this.client, s)), + tags: newProperties.tags + && newProperties.tags.map(t => new Tag(this.client, t)), }; Object.assign(this, newProperties, { @@ -40,52 +49,52 @@ class MessageTemplate extends Resource { /** * Makes a href for a given customer code and ID * @param {string} customerCode Customer code - * @param {Number} id MessageTemplate ID - * @returns {string} URI to instance of messageTemplate + * @param {Number} id Message ID + * @returns {string} URI to instance of message */ static makeHref(customerCode, id) { return { - href: `/1/${customerCode}/message_templates/${id}`, + href: `/1/${customerCode}/messages/${id}`, code: customerCode, }; } /** - * Fetches the data for this messageTemplate via the client - * @returns {Promise} If successful, a hydrated instance of this messageTemplate + * Fetches the data for this message via the client + * @returns {Promise} If successful, a hydrated instance of this message */ fetch() { return this.client.get(this.href) .then(response => response.json()) - .then(messageTemplate => new MessageTemplate(this.client, { ...this, ...messageTemplate })); + .then(message => new Message(this.client, { ...this, ...message })); } /** - * Creates a new message template via the client - * @returns {Promise} If successful, a hydrated instance of message template with id + * Creates a new message via the client + * @returns {Promise} If successful, a hydrated instance of message with id */ create() { // eslint-disable-next-line no-unused-vars const { client, hydrated, customerCode, ...body } = this; - return this.client.post(`/1/${customerCode}/message_templates`, { body }) + return this.client.post(`/1/${customerCode}/messages`, { body }) .then(response => response.headers.get('location')) .then((href) => { - const match = /\/\d+\/\S+\/message_templates\/(\d+)/.exec(href); - return new MessageTemplate(this.client, { ...this, href, id: parseFloat(match[1]) }); + const match = /\/\d+\/\S+\/messages\/(\d+)/.exec(href); + return new Message(this.client, { ...this, href, id: parseFloat(match[1]) }); }); } /** - * Updates data for a message template via the client - * @returns {Promise} if successful returns instance of this message template + * Updates data for a message via the client + * @returns {Promise} if successful returns instance of this message */ update() { // eslint-disable-next-line no-unused-vars const { client, hydrated, customerCode, ...body } = this; - const { href } = MessageTemplate.makeHref(this.customerCode, this.id); + const { href } = Message.makeHref(this.customerCode, this.id); return this.client.put(href, { body }) - .then(() => new MessageTemplate(this.client, { ...this })); + .then(() => new Message(this.client, { ...this })); } } -export default MessageTemplate; +export default Message; diff --git a/src/resources/MessageTemplate.test.js b/src/resources/Message.test.js similarity index 66% rename from src/resources/MessageTemplate.test.js rename to src/resources/Message.test.js index 2abe5c1..40042fe 100644 --- a/src/resources/MessageTemplate.test.js +++ b/src/resources/Message.test.js @@ -2,57 +2,57 @@ import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import fetchMock from 'fetch-mock'; import Client from '../Client'; -import MessageTemplate from './MessageTemplate'; -import { messageTemplates as mockMessageTemplates } from '../mocks'; +import Message from './Message'; +import { messages as mockMessages } from '../mocks'; chai.should(); chai.use(chaiAsPromised); -describe('When instantiating a message template based on customer and ID', () => { +describe('When instantiating a message based on customer and ID', () => { const client = new Client(); - const messageTemplate = new MessageTemplate(client, MessageTemplate.makeHref('SYNC', 1)); + const message = new Message(client, Message.makeHref('SYNC', 1)); - it('should set the href', () => messageTemplate.href.should.equal('/1/SYNC/message_templates/1')); - it('should not be hydrated', () => messageTemplate.hydrated.should.equal(false)); + it('should set the href', () => message.href.should.equal('/1/SYNC/messages/1')); + it('should not be hydrated', () => message.hydrated.should.equal(false)); }); -describe('When instantiating a message template based on an object', () => { +describe('When instantiating a message based on an object', () => { const client = new Client(); - const messageTemplate = new MessageTemplate(client, mockMessageTemplates.getById(1)); + const message = new Message(client, mockMessages.getById(1)); - it('should set the ID', () => messageTemplate.id.should.equal(1)); - it('should set the href', () => messageTemplate.href.should.equal('/1/SYNC/message_templates/1')); - it('should be hydrated', () => messageTemplate.hydrated.should.equal(true)); + it('should set the ID', () => message.id.should.equal(1)); + it('should set the href', () => message.href.should.equal('/1/SYNC/messages/1')); + it('should be hydrated', () => message.hydrated.should.equal(true)); }); -describe('When fetching a message template based on customer and ID', () => { +describe('When fetching a message based on customer and ID', () => { const client = new Client(); - beforeEach(() => mockMessageTemplates.setUpSuccessfulMock(client)); + beforeEach(() => mockMessages.setUpSuccessfulMock(client)); beforeEach(() => fetchMock.catch(503)); afterEach(fetchMock.restore); let promise; beforeEach(() => { - promise = new MessageTemplate(client, MessageTemplate.makeHref('SYNC', 1)).fetch(); + promise = new Message(client, Message.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/message_templates/1')); + it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/messages/1')); it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true)); }); -describe('When creating a message template', () => { +describe('When creating a message', () => { const client = new Client(); - beforeEach(() => mockMessageTemplates.setUpSuccessfulMock(client)); + beforeEach(() => mockMessages.setUpSuccessfulMock(client)); beforeEach(() => fetchMock.catch(503)); afterEach(fetchMock.restore); let promise; beforeEach(() => { - promise = new MessageTemplate(client, { code: 'SYNC', + promise = new Message(client, { code: 'SYNC', ...{ name: '5k Detour', @@ -98,21 +98,21 @@ describe('When creating a message template', () => { 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/message_templates/1')); + it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/messages/1')); it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true)); }); -describe('When updating a message template', () => { +describe('When updating a message', () => { const client = new Client(); - const updateValue = 'newTemplateName'; + const updateValue = 'newMessageName'; - beforeEach(() => mockMessageTemplates.setUpSuccessfulMock(client)); + beforeEach(() => mockMessages.setUpSuccessfulMock(client)); beforeEach(() => fetchMock.catch(503)); afterEach(fetchMock.restore); let promise; beforeEach(() => { - promise = new MessageTemplate(client, { code: 'SYNC', + promise = new Message(client, { code: 'SYNC', ...{ name: '5k Detour', @@ -154,16 +154,16 @@ describe('When updating a message template', () => { ], }, }).create() - .then((messageTemplate) => { - // eslint-disable-next-line no-param-reassign - messageTemplate.name = updateValue; - return messageTemplate.update(); + .then((message) => { + // eslint-disable-next-line no-param-reassign + message.name = updateValue; + return message.update(); }) - .then(messageTemplate => messageTemplate); + .then(message => message); }); 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/message_templates/1')); + it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/messages/1')); it('should be hydrated', () => promise.then(v => v.hydrated).should.eventually.equal(true)); }); diff --git a/src/resources/MessageTemplatesContext.js b/src/resources/MessagesContext.js similarity index 54% rename from src/resources/MessageTemplatesContext.js rename to src/resources/MessagesContext.js index eb27930..56e6b27 100644 --- a/src/resources/MessageTemplatesContext.js +++ b/src/resources/MessagesContext.js @@ -1,15 +1,15 @@ import 'isomorphic-fetch'; import PagedContext from './PagedContext'; -import MessageTemplate from './MessageTemplate'; +import Message from './Message'; /** - * Message template querying context + * Message querying context * - * This is used to query the list of message templates for a customer + * This is used to query the list of messages for a customer */ -class MessageTemplatesContext extends PagedContext { +class MessagesContext extends PagedContext { /** - * Creates a new message template context + * Creates a new message 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 @@ -22,13 +22,13 @@ class MessageTemplatesContext extends PagedContext { /** * Sets the query term for the context * @example - * const messageTemplates = new MessageTemplatesContext(...); - * messageTemplates + * const messages = new MessagesContext(...); + * messages * .withQuery('12') * .getPage() * .then(page => ...); * @param {string} term Query term to search for - * @returns {MessageTemplatesContext} Returns itself + * @returns {MessagesContext} Returns itself */ withQuery(term) { this.params.q = term; @@ -37,12 +37,12 @@ class MessageTemplatesContext extends PagedContext { /** * Gets the first page of results for this context - * @returns {Promise} If successful, a page of MessageTemplate objects - * @see MessageTemplate + * @returns {Promise} If successful, a page of Message objects + * @see Message */ getPage() { - return this.page(MessageTemplate, `/1/${this.code}/message_templates`); + return this.page(Message, `/1/${this.code}/messages`); } } -export default MessageTemplatesContext; +export default MessagesContext; diff --git a/src/resources/MessageTemplatesContext.test.js b/src/resources/MessagesContext.test.js similarity index 56% rename from src/resources/MessageTemplatesContext.test.js rename to src/resources/MessagesContext.test.js index 42a4ee0..0aa1d67 100644 --- a/src/resources/MessageTemplatesContext.test.js +++ b/src/resources/MessagesContext.test.js @@ -2,25 +2,25 @@ import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import fetchMock from 'fetch-mock'; import Client from '../Client'; -import MessageTemplatesContext from './MessageTemplatesContext'; -import { messageTemplates as mockMessageTemplates } from '../mocks'; +import MessagesContext from './MessagesContext'; +import { messages as mockMessages } from '../mocks'; chai.should(); chai.use(chaiAsPromised); -describe('When building a query for message templates', () => { +describe('When building a query for messages', () => { const client = new Client(); client.setAuthenticated(); beforeEach(() => fetchMock - .get(client.resolve('/1/SYNC/message_templates?page=9&per_page=27&q=valid&sort='), mockMessageTemplates.list) + .get(client.resolve('/1/SYNC/messages?page=9&per_page=27&q=valid&sort='), mockMessages.list) .catch(503)); afterEach(fetchMock.restore); let promise; beforeEach(() => { - const messageTemplates = new MessageTemplatesContext(client, 'SYNC'); - promise = messageTemplates + const messages = new MessagesContext(client, 'SYNC'); + promise = messages .withPage(9) .withPerPage(27) .withQuery('valid') diff --git a/src/resources/SignMessage.js b/src/resources/SignMessage.js index d0ded39..b337b7d 100644 --- a/src/resources/SignMessage.js +++ b/src/resources/SignMessage.js @@ -1,7 +1,5 @@ import Resource from './Resource'; import Route from './Route'; -import Sign from './Sign'; -import SignMessageSchedule from './SignMessageSchedule'; import Stop from './Stop'; import Tag from './Tag'; @@ -22,9 +20,6 @@ class SignMessage extends Resource { const hydrated = !Object.keys(newProperties).every(k => k === 'href'); const references = { routes: newProperties.routes && newProperties.routes.map(x => new Route(this.client, x)), - schedules: newProperties.schedules && - newProperties.schedules.map(x => new SignMessageSchedule(this.client, x)), - signs: newProperties.signs && newProperties.signs.map(x => new Sign(this.client, x)), stops: newProperties.stops && newProperties.stops.map(x => new Stop(this.client, x)), tags: newProperties.tags && newProperties.tags.map(x => new Tag(this.client, x)), }; diff --git a/src/resources/SignMessage.test.js b/src/resources/SignMessage.test.js index 12b72c2..b2aed0b 100644 --- a/src/resources/SignMessage.test.js +++ b/src/resources/SignMessage.test.js @@ -2,26 +2,16 @@ import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import Client from '../Client'; import SignMessage from './SignMessage'; -import Route from './Route'; -import Sign from './Sign'; -import SignMessageSchedule from './SignMessageSchedule'; -import Stop from './Stop'; -import Tag from './Tag'; -import { messageTemplates as mockMessageTemplates } from '../mocks'; +import { messages as mockMessages } from '../mocks'; chai.should(); chai.use(chaiAsPromised); describe('When instantiating a sign message based on an object', () => { const client = new Client(); - const mockSignMessage = mockMessageTemplates.getById(1).sign_messages[0]; + const mockSignMessage = mockMessages.getById(1).sign_messages[0]; const signMessage = new SignMessage(client, mockSignMessage); it('should be hydrated', () => signMessage.hydrated.should.equal(true)); it('should have override text', () => signMessage.override_text.should.equal(mockSignMessage.override_text)); - it('should have routes', () => signMessage.routes[0].should.be.an.instanceof(Route)); - it('should have schedules', () => signMessage.schedules[0].should.be.an.instanceof(SignMessageSchedule)); - it('should have signs', () => signMessage.signs[0].should.be.an.instanceof(Sign)); - it('should have stops', () => signMessage.stops[0].should.be.an.instanceof(Stop)); - it('should have tags', () => signMessage.tags[0].should.be.an.instanceof(Tag)); }); diff --git a/src/resources/SignMessageSchedule.js b/src/resources/SignMessageSchedule.js deleted file mode 100644 index 5f507a2..0000000 --- a/src/resources/SignMessageSchedule.js +++ /dev/null @@ -1,25 +0,0 @@ -import Resource from './Resource'; - -/** - * SignMessageSchedule resource - */ -class SignMessageSchedule extends Resource { - /** - * Creates a new sign message schedule - * - * Will populate itself with the values given to it after the client parameter - * @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, - }); - } -} - -export default SignMessageSchedule; diff --git a/src/resources/SignMessageSchedule.test.js b/src/resources/SignMessageSchedule.test.js deleted file mode 100644 index d797fcc..0000000 --- a/src/resources/SignMessageSchedule.test.js +++ /dev/null @@ -1,16 +0,0 @@ -import chai from 'chai'; -import chaiAsPromised from 'chai-as-promised'; -import Client from '../Client'; -import SignMessageSchedule from './SignMessageSchedule'; -import { messageTemplates as mockMessageTemplates } from '../mocks'; - -chai.should(); -chai.use(chaiAsPromised); - -describe('When instantiating a sign message schedule based on an object', () => { - const client = new Client(); - const mockSignMessageSchedule = mockMessageTemplates.getById(1).sign_messages[0].schedules[0]; - const signMessageSchedule = new SignMessageSchedule(client, mockSignMessageSchedule); - - it('should be hydrated', () => signMessageSchedule.hydrated.should.equal(true)); -});