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: '1/SYNC/message_templates?page=1&per_page=10&q=5k&sort=>; rel="next", 1/SYNC/message_templates?page=1&per_page=10&q=5k&sort=>; 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: '1/SYNC/messages?page=1&per_page=10&q=5k&sort=>; rel="next", 1/SYNC/messages?page=1&per_page=10&q=5k&sort=>; 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