test/unit/services.rule.v3.js
import test from 'ava';
import sinon from 'sinon';
import { client, Rule, ruleService } 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('DELETE', `${client.apiBaseUrl}/v3/reminder/rules/*`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/reminder/rules`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: [
{
id: '11',
type: 'reminder__rules',
attributes: {
permanent_link: 'dummy_2254',
label: null,
created_at: '2017-09-10T19:21:12Z',
updated_at: '2017-09-10T19:21:12Z'
}
},
{
id: '12',
type: 'reminder__rules',
attributes: {
permanent_link: 'dummy_2255',
label: null,
created_at: '2017-09-10T19:21:12Z',
updated_at: '2017-09-10T19:21:12Z'
}
},
{
id: '13',
type: 'reminder__rules',
attributes: {
permanent_link: 'dummy_2256',
label: null,
created_at: '2017-09-10T19:21:12Z',
updated_at: '2017-09-10T19:21:12Z'
}
}
]
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/reminder/rules/*`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '93',
type: 'reminder__rules',
attributes: {
permanent_link: 'dummy_909',
label: null,
created_at: '2017-09-10T19:18:32Z',
updated_at: '2017-09-10T19:18:32Z'
}
}
})]);
t.context.storage = client.storageUtility;
client.rule = new Rule({});
t.context.ruleJsonApi = {
data: {
type: 'rules',
attributes: {
title: 'dummy_1065',
description: 'Demo Content info...',
redemption_points: 250
}
}
};
t.context.rule = new Rule(t.context.ruleJsonApi);
});
test.afterEach(t => t.context.server.restore());
// RuleService.delete method
/**
* @test {RuleService.delete}
*/
test('[unit] RuleService.delete should throw errors for invalid parameters', async (t) => {
const undefinedError = 'RuleService.delete error: rule is not defined';
await t.throwsAsync(ruleService.delete(), undefinedError);
});
// /**
// * @test {RuleService.delete}
// */
// test('[unit] RuleService.delete should make a properly formatted delete request', async (t) => {
// const { rule } = t.context;
// rule.id = 5;
// await ruleService.delete(rule);
// const request = t.context.server.requests[0];
// t.is(request.method, 'DELETE');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/rules/${rule.id}`);
// 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 {RuleService.delete}
*/
test('[unit] RuleService.delete should receive a valid response for a delete request', async (t) => {
const { rule } = t.context;
rule.id = 5;
const response = await ruleService.delete(rule);
// const request = t.context.server.requests[0];
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.truthy(response);
});
/**
* @test {RuleService.delete}
*/
test('[unit] RuleService.delete should remove the element from local storage', async (t) => {
const { rule, storage } = t.context;
rule.id = 5;
await storage.set('rules', rule.toJSON(), { id: rule.id });
await ruleService.delete(rule);
t.is(storage.has('rules', { id: rule.id }), false);
});
// RuleService.get method
/**
* @test {RuleService.get}
*/
test.serial('[unit] RuleService.get should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'RuleService.get error: requires authToken';
await t.throwsAsync(ruleService.get(), expectedError);
});
// /**
// * @test {RuleService.get}
// */
// test('[unit] RuleService.get should make a properly formatted get request', async (t) => {
// await ruleService.get();
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/rules`);
// 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 {RuleService.get}
*/
test('[unit] RuleService.get should receive a valid response for a get request without an id', async (t) => {
const response = await ruleService.get();
t.truthy(response);
t.is(Object.keys(response).length, 3);
});
/**
* @test {RuleService.get}
*/
test('[unit] RuleService.get should receive a valid response for a get request with an id', async (t) => {
const response = await ruleService.get({ id: 4 });
t.truthy(response);
t.is(response.id, 93);
t.is(response.type, 'reminder__rules');
t.is(response.createdAt, '2017-09-10T19:18:32Z');
t.is(response.updatedAt, '2017-09-10T19:18:32Z');
t.is(response.permanentLink, 'dummy_909');
});
// RuleService.insert method
/**
* @test {RuleService.insert}
*/
test.serial('[unit] RuleService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'RuleService.insert error: requires authToken';
await t.throwsAsync(ruleService.insert(new Rule()), expectedError);
});
/**
* @test {RuleService.insert}
*/
test('[unit] RuleService.insert should successfully insert a rule with a rule object', async (t) => {
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/reminder/rules`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '10',
type: 'reminder__rules',
attributes: {
permanent_link: 'dummy_2247',
label: null,
created_at: '2017-09-10T19:21:12Z',
updated_at: '2017-09-10T19:21:12Z'
}
}
})]);
const requestJsonApi = {
data: {
type: 'reminder__rules',
attributes: {
permanent_link: 'dummy_2247'
}
}
};
const rule = new Rule(requestJsonApi);
const response = await ruleService.insert(rule);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/rules`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify(requestJsonApi));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.id, 10);
t.is(response.type, 'reminder__rules');
t.is(response.permanentLink, 'dummy_2247');
t.is(response.createdAt, '2017-09-10T19:21:12Z');
t.is(response.updatedAt, '2017-09-10T19:21:12Z');
});
// RuleService.update method
// /**
// * @test {RuleService.update}
// */
// test('[unit] RuleService.update should throw errors for invalid parameters', async (t) => {
// const undefinedError = 'RuleService.update error: rule is not defined';
// await t.throwsAsync(ruleService.update(), undefinedError);
// });
// /**
// * @test {RuleService.update}
// */
// test('[unit] RuleService.update should successfully update a rule with a rule object', async (t) => {
// const { rule, ruleJsonApi } = t.context;
// const response = await ruleService.update(rule);
// const request = t.context.server.requests[0];
// t.is(request.method, 'PATCH');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/rules/${rule.id}`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify(ruleJsonApi));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// t.is(response.id, 4);
// t.is(response.type, 'reminder__rules');
// });