Skip to content

Commit

Permalink
feat(messages): Use updated message schema (#51) (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuevaskoch authored Dec 18, 2018
1 parent 30ad86d commit f089a0b
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 335 deletions.
130 changes: 0 additions & 130 deletions src/examples/get_message_templates.test.js

This file was deleted.

115 changes: 115 additions & 0 deletions src/examples/get_messages.test.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]', 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: '[email protected]', 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: '[email protected]', 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: '[email protected]', 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;
});
});
2 changes: 1 addition & 1 deletion src/mocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
59 changes: 0 additions & 59 deletions src/mocks/messageTemplates.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/mocks/messages.js
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit f089a0b

Please sign in to comment.