test/unit/services.message.js
import test from 'ava';
import sinon from 'sinon';
import { client, messageService } from '../../src';
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.server = sinon.fakeServer.create();
t.context.server.autoRespond = true;
t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/conversations`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
conversations: [{
subject: 'commodi',
created_at: '2016-07-13T20:26:17Z',
updated_at: '2016-07-21T21:48:34Z'
}, {
subject: 'commodi',
created_at: '2016-07-13T05:08:27Z',
updated_at: '2016-07-13T05:11:52Z'
}, {
subject: 'commodi',
created_at: '2016-07-08T18:51:13Z',
updated_at: '2016-07-13T04:31:51Z'
}
]
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/conversations/1/messages`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'ok',
content: {
messages: [{
id: 151,
title: 'First message',
body: 'This is the first sample message.',
created_at: '2016-01-20T00:30:30Z',
updated_at: '2016-01-20T00:30:30Z'
}, {
id: 152,
title: 'A reply',
body: 'This is a sample response to the first sample message.',
created_at: '2016-01-20T00:31:30Z',
updated_at: '2016-01-20T00:31:30Z'
}]
}
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/messages`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'ok',
conversation_id: 1
})]);
t.context.message = {
subject: 'commodi',
body: 'This is the body of the message',
recipients: [
1533,
],
};
});
test.afterEach(t => t.context.server.restore());
// MessageService.getConversations method
/**
* @test {MessageService.getConversations}
*/
test('[unit] MessageService.getConversations should exist', (t) => {
t.truthy(messageService.getConversations);
});
/**
* @test {MessageService.getConversations}
*/
test.serial('[unit] MessageService.getConversations should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
t.throws(() => messageService.getConversations(), `Clinical6 getConversations error: requires authToken`);
});
/**
* @test {MessageService.getConversations}
*/
test('[unit] MessageService.getConversations should return a promise', async (t) => {
t.truthy(await messageService.getConversations().then);
});
// /**
// * @test {MessageService.getConversations}
// */
// test('[unit] MessageService.getConversations should make a properly formatted get request', async (t) => {
// await messageService.getConversations();
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/api/conversations`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// });
/**
* @test {MessageService.getConversations}
*/
test('[unit] MessageService.getConversations should receive a valid response for a get request', async (t) => {
const response = await messageService.getConversations();
t.truthy(response);
t.is(response.length, 3);
t.is(response[0].subject, 'commodi');
t.is(response[0].created_at, '2016-07-13T20:26:17Z');
t.is(response[0].updated_at, '2016-07-21T21:48:34Z');
t.is(response[1].subject, 'commodi');
t.is(response[1].created_at, '2016-07-13T05:08:27Z');
t.is(response[1].updated_at, '2016-07-13T05:11:52Z');
t.is(response[2].subject, 'commodi');
t.is(response[2].created_at, '2016-07-08T18:51:13Z');
t.is(response[2].updated_at, '2016-07-13T04:31:51Z');
});
// MessageService.getMessages method
/**
* @test {MessageService.getMessages}
*/
test('[unit] MessageService.getMessages should exist', (t) => {
t.truthy(messageService.getMessages);
});
/**
* @test {MessageService.getMessages}
*/
test.serial('[unit] MessageService.getMessages should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'Clinical6 getMessages error: requires authToken';
t.throws(() => messageService.getMessages(1), expectedError);
});
/**
* @test {MessageService.getMessages}
*/
test('[unit] MessageService.getMessages should throw an error when there is no conversation ID', (t) => {
const expectedError = 'Clinical6 getMessages error: id is not defined';
t.throws(() => messageService.getMessages(), expectedError);
});
/**
* @test {MessageService.getMessages}
*/
test('[unit] MessageService.getMessages should return a promise', async (t) => {
t.truthy(await messageService.getMessages(1).then);
});
// /**
// * @test {MessageService.getMessages}
// */
// test('[unit] MessageService.getMessages should make a properly formatted get request', async (t) => {
// await messageService.getMessages(1);
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// const expectedUrl = `${client.apiBaseUrl}/api/conversations/1/messages`;
// t.is(request.url, expectedUrl);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// });
/**
* @test {MessageService.getMessages}
*/
test('[unit] MessageService.getMessages should receive a valid response for a get request', async (t) => {
const response = await messageService.getMessages(1);
t.truthy(response);
t.is(response.length, 2);
t.is(response[0].title, 'First message');
t.is(response[0].body, 'This is the first sample message.');
t.is(response[1].title, 'A reply');
t.is(response[1].body, 'This is a sample response to the first sample message.');
});
// MessageService.sendMessage method
/**
* @test {MessageService.sendMessage}
*/
test('[unit] MessageService.sendMessage should exist', (t) => {
t.truthy(messageService.sendMessage);
});
/**
* @test {MessageService.sendMessage}
*/
test.serial('[unit] MessageService.sendMessage should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'Clinical6 sendMessage error: requires authToken and message is not defined';
t.throws(() => messageService.sendMessage(), expectedError);
});
/**
* @test {MessageService.sendMessage}
*/
test('[unit] MessageService.sendMessage should throw an error when there is no message', (t) => {
const expectedError = 'Clinical6 sendMessage error: message is not defined';
t.throws(() => messageService.sendMessage(), expectedError);
});
/**
* @test {MessageService.sendMessage}
*/
test('[unit] MessageService.sendMessage should throw an error when there is no subject attribute in message', (t) => {
let message = {
body: 'This is the body of the message',
recipients: [
1533,
],
};
t.throws(() => messageService.sendMessage(message), `Clinical6 sendMessage error: message does not have subject`);
message = {
subject: 'commodi',
recipients: [
1533,
],
};
t.throws(() => messageService.sendMessage(message), `Clinical6 sendMessage error: message does not have body`);
message = {
subject: 'commodi',
body: 'This is the body of the message',
};
t.throws(() => messageService.sendMessage(message), `Clinical6 sendMessage error: message does not have recipients`);
});
/**
* @test {MessageService.sendMessage}
*/
test('[unit] MessageService.sendMessage should return a promise', async (t) => {
const message = {
subject: 'commodi',
body: 'This is the body of the message',
recipients: [
1533,
],
};
t.truthy(await messageService.sendMessage(message).then);
});
// /**
// * @test {MessageService.sendMessage}
// */
// test('[unit] MessageService.sendMessage should make a properly formatted POST request', async (t) => {
// const aMessage = {
// body: 'This is the body of the message',
// subject: 'commodi',
// recipients: [
// 1533,
// ],
// };
// await messageService.sendMessage(aMessage);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `https://somesite.Clinical6.com/api/messages`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestBody, JSON.stringify({
// message: aMessage,
// }));
// });
/**
* @test {MessageService.sendMessage}
*/
test('[unit] MessageService.sendMessage should successfully create a message with a message object', async (t) => {
const aMessage = {
body: 'This is the body of the message',
subject: 'commodi',
recipients: [
1533,
],
};
const response = await messageService.sendMessage(aMessage);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `https://somesite.Clinical6.com/api/messages`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({
// message: aMessage,
// }));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.status, 'ok');
t.is(response.conversation_id, 1);
});