test/unit/services.user.v3.notification.js
import test from 'ava';
import sinon from 'sinon';
import {
client,
mobileUserService,
Notification,
User,
} 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}/v3/:resource/:resource_id/notification/deliveries`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: [
{
id: '7',
type: 'notification__deliveries',
attributes: {
action: null,
action_object: null,
channels: [
'push notification'
],
message: 'Some message',
status: 'pending',
title: 'Some title',
opts: null,
created_at: '2017-11-14T19:50:20Z',
updated_at: '2017-11-14T19:50:20Z'
}
},
{
id: '9',
type: 'notification__deliveries',
attributes: {
action: null,
action_object: null,
channels: [
'push notification'
],
message: 'Some message',
status: 'pending',
title: 'Some title',
opts: null,
created_at: '2017-11-14T19:50:20Z',
updated_at: '2017-11-14T19:50:20Z'
}
},
{
id: '10',
type: 'notification__deliveries',
attributes: {
action: null,
action_object: null,
channels: [
'push notification'
],
message: 'Some message',
status: 'pending',
title: 'Some title',
opts: null,
created_at: '2017-11-14T19:50:20Z',
updated_at: '2017-11-14T19:50:20Z'
}
}
]
})]
);
t.context.storage = client.storageUtility;
client.notification = new Notification({});
t.context.notificationJsonApi = {
data: {
type: 'notification__deliveries',
action: null,
action_object: null,
channels: [
'push notification'
],
message: 'Some message',
status: 'read',
title: 'Some title',
opts: null
}
};
t.context.notification = new Notification(t.context.notificationJsonApi);
});
test.afterEach(t => t.context.server.restore());
// UserService.getNotifications method
/**
* @test {UserService.getNotifications}
*/
test.serial('[unit] UserService.getNotifications should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const user = new User({ id: 200 });
const expectedError = 'UserService.getNotifications error: requires authToken';
await t.throwsAsync(mobileUserService.getNotifications(user), expectedError);
});
/**
* @test {UserService.getNotifications}
*/
test('[unit] UserService.getNotifications should throw an error when there is no user', async (t) => {
// const notification = new Notification({ id: 5 });
const expectedError = 'UserService.getNotifications error: user does not have id';
await t.throwsAsync(mobileUserService.getNotifications(), expectedError);
});
/**
* @test {UserService.getNotifications}
*/
test('[unit] UserService.getNotifications should throw an error when there is no user id', async (t) => {
// const notification = new Notification({ id: 5 });
const expectedError = 'UserService.getNotifications error: user does not have id';
await t.throwsAsync(mobileUserService.getNotifications({ type: 'mobile_users' }), expectedError);
});
/**
* @test {UserService.getNotifications}
*/
test('[unit] UserService.getNotifications should throw an error when there is no user type', async (t) => {
// const notification = new Notification({ id: 5 });
const expectedError = 'UserService.getNotifications error: user does not have type';
await t.throwsAsync(mobileUserService.getNotifications({ id: 200 }), expectedError);
});
// /**
// * @test {UserService.getNotifications}
// */
// test('[unit] UserService.getNotifications should make a properly formatted get request', (t) => {
// const user = new User({ id: 200 });
// mobileUserService.getNotifications(undefined, user);
// const { method, url, requestHeaders } = t.context.server.requests[0];
// t.is(method, 'GET');
// t.is(url, `${client.apiBaseUrl}/v3/mobile_users/200/notification/deliveries`);
// t.is(requestHeaders.Accept, 'application/json');
// t.is(requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(requestHeaders.Authorization, 'Token token=valid_token');
// });
/**
* @test {UserService.getNotifications}
*/
test('[unit] UserService.getNotifications should receive a valid response for a get request', async (t) => {
const user = new User({ id: 200 });
const response = await mobileUserService.getNotifications(user);
await t.truthy(response);
await t.is(response[0].id, 7);
await t.is(response[0].type, 'notification__deliveries');
await t.is(response[1].id, 9);
await t.is(response[1].type, 'notification__deliveries');
await t.is(response[2].id, 10);
await t.is(response[2].type, 'notification__deliveries');
});