test/unit/services.user.v3.flow-data.js
import test from 'ava';
import nock from 'nock';
import { client, User, userService } from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.getResponsePerPage = {
data: [
{
id: 6017,
type: 'data_collection__captured_value_groups',
attributes: {
submitted_at: '2018-05-13T15:57:20-07:00',
created_at: '2018-05-13T15:57:20-07:00',
updated_at: '2018-05-13T15:57:20-07:00',
owner_type: 'MobileUser',
final_submission: false
},
relationships: {
owner: {
data: {
id: 11081,
type: 'mobile_users'
}
},
creator: {
data: {
id: 11081,
type: 'mobile_users'
}
},
flow_process: {
data: {
id: 590,
type: 'data_collection__flow_processes'
}
},
captured_values: {
data: []
},
status: {
data: {
id: 196766,
type: 'statuses'
}
},
ediary_entry: {
data: null
}
}
},
{
id: 6018,
type: 'data_collection__captured_value_groups',
attributes: {
submitted_at: '2018-05-13T15:57:51-07:00',
created_at: '2018-05-13T15:57:51-07:00',
updated_at: '2018-05-13T15:57:55-07:00',
owner_type: 'MobileUser',
final_submission: false
},
relationships: {
owner: {
data: {
id: 11081,
type: 'mobile_users'
}
},
creator: {
data: {
id: 11081,
type: 'mobile_users'
}
},
flow_process: {
data: {
id: 1501,
type: 'data_collection__flow_processes'
}
},
captured_values: {
data: [
{
id: 100043,
type: 'data_collection__captured_values'
},
{
id: 100044,
type: 'data_collection__captured_values'
},
{
id: 100045,
type: 'data_collection__captured_values'
}
]
},
status: {
data: {
id: 196769,
type: 'statuses'
}
},
ediary_entry: {
data: null
}
}
}
],
included: [
{
id: 590,
type: 'data_collection__flow_processes',
attributes: {
name: 'Privacy Policy',
permanent_link: 'privacy_policy_published',
consent_credentials: null,
created_at: '2017-05-05T09:43:20-07:00',
updated_at: '2017-05-17T15:11:05-07:00',
owner_type: 'MobileUser',
published_at: '2017-05-05T09:43:20-07:00',
conditional_paths: null
},
relationships: {
linked_steps: {
data: [
{
id: 24779,
type: 'data_collection__linked_steps'
},
{
id: 24780,
type: 'data_collection__linked_steps'
}
]
},
published: {
data: null
},
draft: {
data: {
id: 589,
type: 'data_collection__flow_processes'
}
},
initial_step: {
data: {
id: 24780,
type: 'data_collection__linked_steps'
}
}
}
},
{
id: 196766,
type: 'statuses',
attributes: {
value: 'completed'
},
relationships: {
owner: {
data: null
},
statusable: {
data: {
id: 6017,
type: 'data_collection__captured_value_groups'
}
}
}
},
{
id: 1501,
type: 'data_collection__flow_processes',
attributes: {
name: 'Patient Upload Documents',
permanent_link: 'patient_upload_documents_published',
consent_credentials: null,
created_at: '2018-03-26T11:29:42-07:00',
updated_at: '2018-03-26T11:29:43-07:00',
owner_type: 'MobileUser',
published_at: '2018-03-26T11:29:43-07:00',
conditional_paths: null
},
relationships: {
linked_steps: {
data: [
{
id: 47666,
type: 'data_collection__linked_steps'
},
{
id: 47667,
type: 'data_collection__linked_steps'
},
{
id: 47668,
type: 'data_collection__linked_steps'
},
{
id: 47669,
type: 'data_collection__linked_steps'
},
{
id: 47670,
type: 'data_collection__linked_steps'
}
]
},
published: {
data: null
},
draft: {
data: {
id: 1500,
type: 'data_collection__flow_processes'
}
},
initial_step: {
data: {
id: 47666,
type: 'data_collection__linked_steps'
}
}
}
},
{
id: 196769,
type: 'statuses',
attributes: {
value: 'completed'
},
relationships: {
owner: {
data: null
},
statusable: {
data: {
id: 6018,
type: 'data_collection__captured_value_groups'
}
}
}
}
],
links: {
self: 'https://strimvelis-uat.clinical6.com/v3/mobile_users/11081/data_collection/captured_value_groups?page%5Bnumber%5D=1&page%5Bsize%5D=2&per_page=2',
next: 'https://strimvelis-uat.clinical6.com/v3/mobile_users/11081/data_collection/captured_value_groups?page%5Bnumber%5D=2&page%5Bsize%5D=2&per_page=2',
last: 'https://strimvelis-uat.clinical6.com/v3/mobile_users/11081/data_collection/captured_value_groups?page%5Bnumber%5D=6&page%5Bsize%5D=2&per_page=2'
},
meta: {
page: 1,
per_page: 2,
total_pages: 6
}
};
t.context.getResponseId = {
data: {
id: '28',
type: 'data_collection__captured_value_groups',
attributes: {
submitted_at: '2017-03-15T00:00:00Z',
created_at: '2017-08-22T01:29:43Z',
updated_at: '2017-08-22T01:29:43Z',
owner_type: 'MobileUser'
},
relationships: {
owner: {
data: {
id: 315,
type: 'mobile_users'
}
},
creator: {
data: {
id: 315,
type: 'mobile_users'
}
},
flow_process: {
data: {
id: 158,
type: 'data_collection__flow_processes'
}
},
captured_values: {
data: []
}
}
},
meta: {
required_for_update: [
'reason_for_change'
],
reason_for_change: [
{
reference: 'transcription_error',
display: 'Transcription Error'
},
{
reference: 'new_information',
display: 'New Information'
},
{
reference: 'changed_information',
display: 'Changed Information'
},
{
reference: 'other',
display: 'Other'
}
]
}
};
});
test.after('server shut down', () => {});
test.beforeEach(() => {
client.cache = 'never';
client.authToken = 'valid_token';
});
// UserService.getDataGroup method
/**
* @test {UserService.getDataGroup}
*/
test('[unit] UserService.getDataGroup should have a getDataGroup method', (t) => {
t.truthy(userService.getDataGroup);
});
/**
* @test {UserService.getDataGroup}
*/
test('[unit] UserService.getDataGroup should throw an error when id is not defined', async (t) => {
const title = 'UserService.getDataGroup error';
await t.throwsAsync(userService.getDataGroup(), `${title}: user does not have id`);
await t.throwsAsync(userService.getDataGroup({}), `${title}: user does not have id and user does not have type`);
await t.throwsAsync(userService.getDataGroup({ id: 27 }), `${title}: user does not have type`);
await t.throwsAsync(userService.getDataGroup(new User({ id: 5 }), { id: `permanent_link` }), `${title}: paramsId is not a number`);
});
// UserService.getDataGroup method
/**
* @test {UserService.getDataGroup}
*/
test('[unit] UserService.getDataGroup should make a properly formatted get request', async (t) => {
const { getResponsePerPage } = t.context;
let request = {};
nock(client.apiBaseUrl).get(`/v3/mobile_users/11081/data_collection/captured_value_groups?page[size]=2`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponsePerPage];
});
await userService.getDataGroup(new User({ id: 11081 }), { page: { size: 2 } });
t.is(request.path, `/v3/mobile_users/11081/data_collection/captured_value_groups?page[size]=2`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
});
/**
* @test {UserService.getDataGroup}
*/
test('[unit] UserService.getDataGroup should receive a valid response for a get request with an id and without filtering parameters', async (t) => {
const { getResponseId } = t.context;
nock(client.apiBaseUrl).get('/v3/mobile_users/315/data_collection/captured_value_groups/28').reply(200, getResponseId);
const response = await userService.getDataGroup(new User({ id: 315 }), { id: 28 });
t.truthy(response);
t.is(response.constructor.name, 'FlowDataGroup');
t.is(response.id, 28);
t.is(response.createdAt, '2017-08-22T01:29:43Z');
t.is(response.ownerType, 'MobileUser');
t.is(response.submittedAt, '2017-03-15T00:00:00Z');
t.is(response.updatedAt, '2017-08-22T01:29:43Z');
t.is(response.owner.id, 315);
t.is(response.creator.id, 315);
t.is(response.flow.id, 158);
// Check to see if meta data is set
t.is(response.meta.requiredForUpdate.length, 1);
// client.storageUtility.get('meta').then(data => data);
});
// UserService.getDataGroup method
/**
* @test {UserService.getDataGroup}
*/
test('[unit] UserService.getDataGroup should receive a valid response for a get request without an id or filtering parameters', async (t) => {
const { getResponsePerPage } = t.context;
nock(client.apiBaseUrl).get(`/v3/mobile_users/11081/data_collection/captured_value_groups`).reply(200, getResponsePerPage);
const response = await userService.getDataGroup(new User({ id: 11081 }));
t.truthy(response);
t.is(Object.keys(response).length, 2);
t.is(response[0].id, 6017);
t.is(response[0].type, 'data_collection__captured_value_groups');
t.is(response[0].submittedAt, '2018-05-13T15:57:20-07:00');
t.is(response[0].createdAt, '2018-05-13T15:57:20-07:00');
t.is(response[0].updatedAt, '2018-05-13T15:57:20-07:00');
t.is(response[0].ownerType, 'MobileUser');
t.is(response[0].owner.id, 11081);
t.is(response[0].owner.type, 'mobile_users');
t.is(response[0].creator.id, 11081);
t.is(response[0].creator.type, 'mobile_users');
t.is(response[0].flow.id, 590);
t.is(response[0].flow.type, 'data_collection__flow_processes');
t.is(response[0].flowData.length, 0);
t.is(response[0].status.id, 196766);
t.is(response[0].status.type, 'statuses');
t.is(response[0].entry, null);
t.is(response[1].id, 6018);
t.is(response[1].type, 'data_collection__captured_value_groups');
t.is(response[1].submittedAt, '2018-05-13T15:57:51-07:00');
t.is(response[1].createdAt, '2018-05-13T15:57:51-07:00');
t.is(response[1].updatedAt, '2018-05-13T15:57:55-07:00');
t.is(response[1].ownerType, 'MobileUser');
t.is(response[1].owner.id, 11081);
t.is(response[1].owner.type, 'mobile_users');
t.is(response[1].creator.id, 11081);
t.is(response[1].creator.type, 'mobile_users');
t.is(response[1].flow.id, 1501);
t.is(response[1].flow.type, 'data_collection__flow_processes');
t.is(response[1].flowData.length, 3);
t.is(response[1].flowData[0].id, 100043);
t.is(response[1].flowData[0].type, 'data_collection__captured_values');
t.is(response[1].flowData[1].id, 100044);
t.is(response[1].flowData[1].type, 'data_collection__captured_values');
t.is(response[1].flowData[2].id, 100045);
t.is(response[1].flowData[2].type, 'data_collection__captured_values');
t.is(response[1].status.id, 196769);
t.is(response[1].status.type, 'statuses');
t.is(response[1].entry, null);
});
// UserService.getDataGroup method
/**
* @test {UserService.getDataGroup}
*/
test('[unit] UserService.getDataGroup should receive a valid response for a get request without an id and with the filtering parameters', async (t) => {
const { getResponsePerPage } = t.context;
nock(client.apiBaseUrl).get(`/v3/mobile_users/11081/data_collection/captured_value_groups?page[size]=2`).reply(200, getResponsePerPage);
const response = await userService.getDataGroup(new User({ id: 11081 }), { page: { size: 2 } });
t.truthy(response);
t.is(Object.keys(response).length, 2);
t.is(response[0].id, 6017);
t.is(response[0].type, 'data_collection__captured_value_groups');
t.is(response[0].submittedAt, '2018-05-13T15:57:20-07:00');
t.is(response[0].createdAt, '2018-05-13T15:57:20-07:00');
t.is(response[0].updatedAt, '2018-05-13T15:57:20-07:00');
t.is(response[0].ownerType, 'MobileUser');
t.is(response[0].owner.id, 11081);
t.is(response[0].owner.type, 'mobile_users');
t.is(response[0].creator.id, 11081);
t.is(response[0].creator.type, 'mobile_users');
t.is(response[0].flow.id, 590);
t.is(response[0].flow.type, 'data_collection__flow_processes');
t.is(response[0].flowData.length, 0);
t.is(response[0].status.id, 196766);
t.is(response[0].status.type, 'statuses');
t.is(response[0].entry, null);
t.is(response[1].id, 6018);
t.is(response[1].type, 'data_collection__captured_value_groups');
t.is(response[1].submittedAt, '2018-05-13T15:57:51-07:00');
t.is(response[1].createdAt, '2018-05-13T15:57:51-07:00');
t.is(response[1].updatedAt, '2018-05-13T15:57:55-07:00');
t.is(response[1].ownerType, 'MobileUser');
t.is(response[1].owner.id, 11081);
t.is(response[1].owner.type, 'mobile_users');
t.is(response[1].creator.id, 11081);
t.is(response[1].creator.type, 'mobile_users');
t.is(response[1].flow.id, 1501);
t.is(response[1].flow.type, 'data_collection__flow_processes');
t.is(response[1].flowData.length, 3);
t.is(response[1].flowData[0].id, 100043);
t.is(response[1].flowData[0].type, 'data_collection__captured_values');
t.is(response[1].flowData[1].id, 100044);
t.is(response[1].flowData[1].type, 'data_collection__captured_values');
t.is(response[1].flowData[2].id, 100045);
t.is(response[1].flowData[2].type, 'data_collection__captured_values');
t.is(response[1].status.id, 196769);
t.is(response[1].status.type, 'statuses');
t.is(response[1].entry, null);
});