test/unit/services/ediary/services.ediary.getTemplates.js
import test from 'ava';
import nock from 'nock';
import { client, eDiaryService } from '../../../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.getResponseTemplatesAll = {
data: [
{
id: '9',
type: 'ediary__entry_templates',
attributes: {
category: 'automatic',
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z'
},
relationships: {
entry_group: {
data: {
id: '12',
type: 'ediary__entry_groups'
}
},
flow_process: {
data: {
id: '142',
type: 'data_collection__flow_processes'
}
}
}
},
{
id: '10',
type: 'ediary__entry_templates',
attributes: {
category: 'automatic',
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z'
},
relationships: {
entry_group: {
data: {
id: '12',
type: 'ediary__entry_groups'
}
},
flow_process: {
data: {
id: '143',
type: 'data_collection__flow_processes'
}
}
}
},
{
id: '11',
type: 'ediary__entry_templates',
attributes: {
category: 'automatic',
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z'
},
relationships: {
entry_group: {
data: {
id: '12',
type: 'ediary__entry_groups'
}
},
flow_process: {
data: {
id: '144',
type: 'data_collection__flow_processes'
}
}
}
}
],
included: [
{
id: '12',
type: 'ediary__entry_groups',
attributes: {
name: 'dummy_1534',
permanent_link: 'dummy_1535',
position: 1,
category: null,
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z'
},
relationships: {
ediary_entries: {
data: []
},
entry_templates: {
data: [
{
id: '9',
type: 'ediary__entry_templates'
},
{
id: '10',
type: 'ediary__entry_templates'
},
{
id: '11',
type: 'ediary__entry_templates'
}
]
},
child_entry_groups: {
data: []
},
parent_entry_group: {
data: null
}
}
},
{
id: '142',
type: 'data_collection__flow_processes',
attributes: {
name: 'veritatis',
permanent_link: 'dummy_1536',
consent_credentials: null,
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z',
owner_type: 'MobileUser',
published_at: null,
conditional_paths: null
},
relationships: {
linked_steps: {
data: []
},
published: {
data: null
},
draft: {
data: null
},
initial_step: {
data: null
}
}
},
{
id: '143',
type: 'data_collection__flow_processes',
attributes: {
name: 'veritatis',
permanent_link: 'dummy_1537',
consent_credentials: null,
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z',
owner_type: 'MobileUser',
published_at: null,
conditional_paths: null
},
relationships: {
linked_steps: {
data: []
},
published: {
data: null
},
draft: {
data: null
},
initial_step: {
data: null
}
}
},
{
id: '144',
type: 'data_collection__flow_processes',
attributes: {
name: 'veritatis',
permanent_link: 'dummy_1538',
consent_credentials: null,
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z',
owner_type: 'MobileUser',
published_at: null,
conditional_paths: null
},
relationships: {
linked_steps: {
data: []
},
published: {
data: null
},
draft: {
data: null
},
initial_step: {
data: null
}
}
}
]
};
t.context.getResponseTemplates13 = {
data: [
{
id: '12',
type: 'ediary__entry_templates',
attributes: {
category: 'manual',
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z'
},
relationships: {
entry_group: {
data: {
id: '13',
type: 'ediary__entry_groups'
}
},
flow_process: {
data: {
id: '145',
type: 'data_collection__flow_processes'
}
}
}
}
],
included: [
{
id: '13',
type: 'ediary__entry_groups',
attributes: {
name: 'dummy_1539',
permanent_link: 'dummy_1540',
position: 1,
category: null,
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z'
},
relationships: {
ediary_entries: {
data: []
},
entry_templates: {
data: [
{
id: '12',
type: 'ediary__entry_templates'
},
{
id: '13',
type: 'ediary__entry_templates'
},
{
id: '14',
type: 'ediary__entry_templates'
}
]
},
child_entry_groups: {
data: []
},
parent_entry_group: {
data: null
}
}
},
{
id: '145',
type: 'data_collection__flow_processes',
attributes: {
name: 'veritatis',
permanent_link: 'dummy_1541',
consent_credentials: null,
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z',
owner_type: 'MobileUser',
published_at: null,
conditional_paths: null
},
relationships: {
linked_steps: {
data: []
},
published: {
data: null
},
draft: {
data: null
},
initial_step: {
data: null
}
}
}
]
};
});
test.after('server shut down', () => {});
test.beforeEach(() => {
client.authToken = 'valid_token';
});
// EDiaryService.getTemplates
/**
* @test {EDiaryService.getTemplates}
*/
test.serial('[unit] EDiaryService.getTemplates should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'EDiaryService.getChildren error: requires authToken';
await t.throwsAsync(eDiaryService.getTemplates({ id: 1 }), expectedError);
});
/**
* @test {EDiaryService.getTemplates}
*/
test('[unit] EDiaryService.getTemplates should receive a valid response for getTemplates (api example 1)', async (t) => {
const { getResponseTemplatesAll } = t.context;
let request = {};
nock(client.apiBaseUrl).get(`/v3/ediary/entry_groups/12/ediary/entry_templates`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseTemplatesAll];
});
const response = await eDiaryService.getTemplates({ id: 12 });
t.is(request.path, `/v3/ediary/entry_groups/12/ediary/entry_templates`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.truthy(response);
t.is(response.length, 3);
t.is(response[0].id, 9);
t.is(response[0].type, 'ediary__entry_templates');
t.is(response[0].category, 'automatic');
t.is(response[0].createdAt, '2017-06-27T20:32:59Z');
t.is(response[0].updatedAt, '2017-06-27T20:32:59Z');
t.is(response[0].entryGroup.id, 12);
t.is(response[0].entryGroup.name, 'dummy_1534');
t.is(response[0].entryGroup.permanentLink, 'dummy_1535');
t.is(response[0].entryGroup.position, 1);
t.is(response[0].entryGroup.createdAt, '2017-06-27T20:32:59Z');
t.is(response[0].entryGroup.updatedAt, '2017-06-27T20:32:59Z');
t.is(response[0].flow.id, 142);
t.is(response[0].flow.permanentLink, 'dummy_1536');
t.is(response[0].flow.name, 'veritatis');
t.is(response[0].flow.owner_type, 'MobileUser');
t.is(response[0].flow.ownerType, 'MobileUser');
t.is(response[0].flow.createdAt, '2017-06-27T20:32:59Z');
t.is(response[0].flow.updatedAt, '2017-06-27T20:32:59Z');
});
/**
* @test {EDiaryService.getTemplates}
*/
test('[unit] EDiaryService.getTemplates should receive a valid response for getTemplates (api example 2)', async (t) => {
const { getResponseTemplates13 } = t.context;
let request = {};
nock(client.apiBaseUrl).get(`/v3/ediary/entry_groups/13/ediary/entry_templates?filters[category]=manual`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseTemplates13];
});
const response = await eDiaryService.getTemplates({ id: 13 }, { category: 'manual' });
t.is(request.path, `/v3/ediary/entry_groups/13/ediary/entry_templates?filters[category]=manual`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.truthy(response);
t.is(response.length, 1);
t.is(response[0].id, 12);
t.is(response[0].type, 'ediary__entry_templates');
t.is(response[0].category, 'manual');
t.is(response[0].createdAt, '2017-06-27T20:32:59Z');
t.is(response[0].updatedAt, '2017-06-27T20:32:59Z');
t.is(response[0].entryGroup.id, 13);
t.is(response[0].entryGroup.name, 'dummy_1539');
t.is(response[0].entryGroup.permanentLink, 'dummy_1540');
t.is(response[0].entryGroup.position, 1);
t.is(response[0].entryGroup.createdAt, '2017-06-27T20:32:59Z');
t.is(response[0].entryGroup.updatedAt, '2017-06-27T20:32:59Z');
t.is(response[0].flow.id, 145);
t.is(response[0].flow.permanentLink, 'dummy_1541');
t.is(response[0].flow.name, 'veritatis');
t.is(response[0].flow.owner_type, 'MobileUser');
t.is(response[0].flow.ownerType, 'MobileUser');
t.is(response[0].flow.createdAt, '2017-06-27T20:32:59Z');
t.is(response[0].flow.updatedAt, '2017-06-27T20:32:59Z');
});