test/unit/services.flow.v2.js
import test from 'ava';
import sinon from 'sinon';
import { client, Flow, flowService } 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/data_collection/captured_value_groups/24`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '24',
type: 'data_collection__captured_value_groups',
attributes: {
submitted_at: '2017-03-15T00:00:00Z',
created_at: '2017-07-25T00:06:56Z',
updated_at: '2017-07-25T00:06:56Z',
owner_type: 'MobileUser'
},
relationships: {
owner: {
data: {
id: '131',
type: 'mobile_users'
}
},
creator: {
data: {
id: '131',
type: 'mobile_users'
}
},
flow_process: {
data: {
id: '27',
type: 'data_collection__flow_processes'
}
},
captured_values: {
data: []
}
}
}
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/v2/data_collection/flow_processes/permanent_link`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
steps: [
{
id: 11,
title: 'dummy_634',
description: 'dummy_635',
content_type: 'dummy_637',
image: { },
inputs: [
{
title: 'Age',
question_type: 'text',
style: 'some_style',
choice_list: [],
id: '1'
}
],
paths: []
},
{
id: 12,
title: 'dummy_639',
description: 'dummy_640',
content_type: 'dummy_642',
image: { },
inputs: [],
paths: [
{
capture: true,
button_name: 'Next',
steps: [
{
step: 13,
conditions: [
{
criteria: 'something',
operator: '=',
value: 'Yes'
}
]
}
]
}
]
},
{
id: 13,
title: 'dummy_644',
description: 'dummy_645',
content_type: 'dummy_647',
image: { },
inputs: [],
paths: []
}
],
name: 'Dummy Flow',
first_step: 11,
total: 3
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/v2/data_collection/flow_processes/permanent_link/collect`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'ok',
data: {
captured_value_group_id: 39,
destinations: {
profile: 702
},
process_status: 'completed'
}
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/data_collection/flow_process_values`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: 'dummy_126_published_3',
type: 'data_collection__flow_process_values',
attributes: {
10: 'male',
11: 15
},
relationships: {
flow_process: {
data: {
id: '3',
type: 'data_collection__flow_processes'
}
},
captured_value_group: {
data: {
id: '3',
type: 'data_collection__captured_value_groups'
}
},
owner: {
data: {
id: '9',
type: 'users'
}
}
}
}
})]);
t.context.flow = new Flow({
id: 'permanent_link',
steps: [],
first_step: 0,
total: 0,
});
t.context.flow._fields = {
'user_t.first_name': {
id: 'user_t',
value: 'quaerat',
},
};
});
test.afterEach(t => t.context.server.restore());
// FlowService.collect method
/**
* @test {FlowService.collect}
*/
test('[unit] FlowService.collect should throw an error when id is not defined', (t) => {
t.throws(() => flowService.collect(), 'FlowService.collect error: flow is not defined');
});
// /**
// * @test {FlowService.collect}
// */
// test('[unit] FlowService.collect should make a properly formatted POST request', async (t) => {
// const { flow } = t.context;
// await flowService.collect(flow);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/v2/data_collection/flow_processes/permanent_link/collect`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// });
/**
* @test {FlowService.collect}
*/
test('[unit] FlowService.collect should receive a valid response', async (t) => {
const { flow } = t.context;
flow.owner = 9;
flow.id = 3;
const response = await flowService.collect(flow);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/v2/data_collection/flow_processes/permanent_link/collect`);
// 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');
// t.is(request.requestBody, JSON.stringify({
// fields: [{
// id: 'user_t',
// value: 'quaerat',
// }],
// owner_type: 'mobile_user',
// }));
// t.is(response.status, 'ok');
t.is(response.flowDataGroup.id, 3);
t.is(flow.flowDataGroupId, 3);
t.is(response.owner.id, '9');
t.is(response.flow.id, flow.id.toString());
});
// FlowService.getDataGroup method
/**
* @test {FlowService.getDataGroup}
*/
test('[unit] FlowService.getDataGroup should throw an error when id is not defined', (t) => {
t.throws(() => flowService.getDataGroup(), 'FlowService.getDataGroup error: params is not defined');
t.throws(() => flowService.getDataGroup({}), 'FlowService.getDataGroup error: params does not have id');
t.throws(() => flowService.getDataGroup({ id: 'permanent_link' }), 'FlowService.getDataGroup error: id is not a number');
});
// /**
// * @test {FlowService.getDataGroup}
// */
// test('[unit] FlowService.getDataGroup should make a properly formatted GET request', async (t) => {
// await flowService.getDataGroup({ id: 24 });
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/data_collection/captured_value_groups/24`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// });
/**
* @test {FlowService.getDataGroup}
*/
test('[unit] FlowService.getDataGroup should receive a valid response for a get request', async (t) => {
const response = await flowService.getDataGroup({ id: 24 });
t.truthy(response);
t.is(response.constructor.name, 'FlowDataGroup');
t.is(response.id, 24);
t.is(response.createdAt, '2017-07-25T00:06:56Z');
t.is(response.ownerType, 'MobileUser');
t.is(response.submittedAt, '2017-03-15T00:00:00Z');
t.is(response.updatedAt, '2017-07-25T00:06:56Z');
});
// FlowService.getFlow method
/**
* @test {FlowService.getFlow}
*/
test('[unit] FlowService.getFlow should throw an error when permanent link is not defined', (t) => {
t.throws(() => flowService.getFlow(), 'FlowService.getFlow error: permanentLink is not defined');
t.throws(() => flowService.getFlow(23), 'FlowService.getFlow error: permanentLink is not a string');
});
// /**
// * @test {FlowService.getFlow}
// */
// test('[unit] FlowService.getFlow should make a properly formatted GET request', async (t) => {
// await flowService.getFlow('permanent_link', 1, 10);
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/api/v2/data_collection/flow_processes/permanent_link`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// });
/**
* @test {FlowService.getFlow}
*/
test('[unit] FlowService.getFlow should receive a valid response for a get request', async (t) => {
const response = await flowService.getFlow('permanent_link', 1, 10);
t.truthy(response);
t.is(response.constructor.name, 'Flow');
t.is(response.total, 3);
t.is(response.name, 'Dummy Flow');
t.is(response.first_step, 11);
t.is(response.steps.length, 3);
t.is(response.steps[0].constructor.name, 'FlowStep');
t.is(response.steps[0].inputs[0].constructor.name, 'FlowInput');
t.is(response.steps[0].id, 11);
t.is(response.steps[0].title, 'dummy_634');
t.is(response.steps[0].description, 'dummy_635');
t.is(response.steps[0].content_type, 'dummy_637');
});