test/unit/services.approver-assignment.js
import test from 'ava';
import nock from 'nock';
import {
client,
clinical6,
ApproverGroup,
ApproverAssignment,
ConsentApprover,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.insertResponse = {
data: {
id: '1',
type: 'consent__approver_assignments',
relationships: {
approver_group: {
data: {
id: '2',
type: 'consent__approver_groups'
}
},
approver: {
data: {
id: '2',
type: 'consent__approvers'
}
}
}
},
included: [
{
id: '2',
type: 'consent__approver_groups',
attributes: {
name: 'Approver Group 2',
created_at: '2018-05-04T16:43:23Z',
updated_at: '2018-05-04T16:43:23Z'
},
relationships: {
approver_assignments: {
data: [
{
id: '1',
type: 'consent__approver_assignments'
}
]
},
group_assignments: {
data: []
},
approvers: {
data: [
{
id: '2',
type: 'consent__approvers'
}
]
}
}
},
{
id: '2',
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
approver_groups: {
data: [
{
id: '2',
type: 'consent__approver_groups'
}
]
},
consent_form_versions: {
data: []
}
}
}
]
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.approverAssignmentJsonApi = {
data: {
type: 'consent__approver_assignments',
attributes: {},
relationships: {
approver: {
data: {
id: 2,
type: 'consent__approvers'
}
},
approver_group: {
data: {
id: 2,
type: 'consent__approver_groups'
}
}
}
}
};
t.context.approverAssignment = new ApproverAssignment(t.context.approverAssignmentJsonApi);
});
// ApproverAssignmentService.delete method
/**
* @test {Clinical6.delete}
*/
test('[unit] ApproverAssignmentService.delete should throw errors for invalid parameters', async (t) => {
const title = `ApproverAssignmentService.delete error`;
await t.throwsAsync(clinical6.delete(new ApproverAssignment()), `${title}: approver_assignment does not have id`);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] ApproverAssignmentService.delete should receive a valid response for a delete request', async (t) => {
const { approverAssignment } = t.context;
approverAssignment.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/consent/approver_assignments/${approverAssignment.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await clinical6.delete(approverAssignment);
t.is(request.path, `/v3/consent/approver_assignments/${approverAssignment.id}`);
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.falsy(response);
});
/**
* @test {Clinical6.delete}
*/
test.serial('[unit] ApproverAssignmentService.delete should remove the element from local storage', async (t) => {
const { approverAssignment, storage } = t.context;
approverAssignment.id = 5;
nock(client.apiBaseUrl).delete(`/v3/consent/approver_assignments/${approverAssignment.id}`).reply(200, '');
await storage.set(approverAssignment.type, approverAssignment.toJSON(), { id: approverAssignment.id });
await clinical6.delete(approverAssignment);
t.is(storage.has(approverAssignment.type, { id: approverAssignment.id }), false);
});
// // ApproverAssignmentService.get method
// /**
// * @test {Clinical6.get}
// */
// test.serial('[unit] ApproverAssignmentService.get should throw an error when there is no authToken', async (t) => {
// client.authToken = undefined;
// const title = `ApproverAssignmentService.get error`;
// await t.throwsAsync(clinical6.get(ApproverAssignment), `${title}: requires authToken`);
// });
// // /**
// // * @test {Clinical6.get}
// // */
// // test('[unit] ApproverAssignmentService.get should make a properly formatted get request', async (t) => {
// // await clinical6.get(ApproverAssignment);
// // t.is(request.path, `/v3/consent/approver_assignments`);
// // 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 {Clinical6.get}
// */
// test('[unit] ApproverAssignmentService.get should receive a valid response for a get request without an id', async (t) => {
// const response = await clinical6.get(ApproverAssignment);
// t.truthy(response);
// t.is(Object.keys(response).length, 3);
// t.is(response[0].id, 24);
// t.is(response[0].type, 'approver_assignments');
// t.is(response[0].memberType, 'patient');
// t.is(response[0].createdAt, '2017-09-27T20:32:18Z');
// t.is(response[0].updatedAt, '2017-09-27T20:32:18Z');
// t.is(response[0].mobileApprover.id, 76);
// t.is(response[0].mobileApprover.profile.firstName, 'Jillian');
// t.is(response[0].mobileApprover.profile.lastName, 'Reilly');
// t.is(response[1].id, 25);
// t.is(response[1].type, 'approver_assignments');
// t.is(response[1].memberType, 'patient');
// t.is(response[1].createdAt, '2017-09-27T20:32:18Z');
// t.is(response[1].updatedAt, '2017-09-27T20:32:18Z');
// t.is(response[1].mobileApprover.id, 77);
// t.is(response[1].mobileApprover.profile.firstName, 'Beaulah');
// t.is(response[1].mobileApprover.profile.lastName, 'Rogahn');
// t.is(response[2].id, 26);
// t.is(response[2].type, 'approver_assignments');
// t.is(response[2].memberType, 'patient');
// t.is(response[2].createdAt, '2017-09-27T20:32:18Z');
// t.is(response[2].updatedAt, '2017-09-27T20:32:18Z');
// t.is(response[2].mobileApprover.id, 78);
// t.is(response[2].mobileApprover.profile.firstName, 'Stanton');
// t.is(response[2].mobileApprover.profile.lastName, 'Rice');
// });
// /**
// * @test {Clinical6.get}
// */
// test('[unit] ApproverAssignmentService.get should receive a valid response for a get request with an id', async (t) => {
// const response = await clinical6.get(new ApproverAssignment({ id: 27 }));
// t.truthy(response);
// t.is(response.id, 27);
// t.is(response.type, 'approver_assignments');
// t.is(response.memberType, 'patient');
// t.is(response.createdAt, '2017-09-27T20:32:18Z');
// t.is(response.updatedAt, '2017-09-27T20:32:18Z');
// t.is(response.mobileApprover.id, 80);
// t.is(response.mobileApprover.profile.firstName, 'Ana');
// t.is(response.mobileApprover.profile.lastName, 'Macejkovic');
// });
// ApproverAssignmentService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] ApproverAssignmentService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const title = `ApproverAssignmentService.insert error`;
await t.throwsAsync(clinical6.insert(new ApproverAssignment()), `${title}: requires authToken`);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] ApproverAssignmentService.insert should successfully insert a cohort with a cohort object', async (t) => {
const { insertResponse } = t.context;
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/approver_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const requestJsonApi = {
data: {
type: 'consent__approver_assignments',
attributes: {},
relationships: {
approver: {
data: {
id: 278,
type: 'consent__approvers'
}
},
approver_group: {
data: {
id: 2,
type: 'consent__approver_groups'
}
}
}
}
};
const approverAssignment = new ApproverAssignment();
approverAssignment.approver = new ConsentApprover({ id: 278 });
approverAssignment.approver.store();
approverAssignment.approverGroup = new ApproverGroup({ id: 2 });
approverAssignment.approverGroup.store();
const response = await clinical6.insert(approverAssignment);
t.is(request.path, `/v3/consent/approver_assignments`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, requestJsonApi);
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 1);
t.is(response.type, 'consent__approver_assignments');
t.truthy(response.approver instanceof ConsentApprover);
const { approver } = response;
t.is(approver.id, 2);
t.is(approver.firstName, 'John');
t.is(approver.lastName, 'Doe');
t.is(approver.email, 'john@example.com');
t.is(response.approverGroup.id, approver.approverGroups[0].id);
// console.log(approver.approverGroups);
});
// // ApproverAssignmentService.update method
// /**
// * @test {Clinical6.update}
// */
// test.serial('[unit] ApproverAssignmentService.update should throw an error when there is no authToken', async (t) => {
// client.authToken = undefined;
// const title = `ApproverAssignmentService.update error`;
// await t.throwsAsync(clinical6.update(new ApproverAssignment({ id: 5 })), `${title}: requires authToken`);
// });
// /**
// * @test {Clinical6.update}
// */
// test('[unit] ApproverAssignmentService.update should successfully update a cohort member with a cohort object', async (t) => {
// const { approverAssignment } = t.context; // approverAssignmentJsonApi
// approverAssignment.mobileApprover = new ConsentApprover({ id: 73 });
// approverAssignment.mobileApprover.store();
// approverAssignment.cohort = new ApproverGroup({ id: 35 });
// approverAssignment.cohort.store();
// approverAssignment.store();
// const response = await clinical6.update(approverAssignment);
// // t.is(request.path, `/v3/consent/approver_assignments/${approverAssignment.id}`);
// // t.is(request.headers.accept, 'application/json');
// // t.deepEqual(request.requestBody, approverAssignmentJsonApi);
// // t.is(request.headers['content-type'], 'application/json');
// // t.is(request.headers.authorization, 'Token token=valid_token');
// t.is(response.id, 23);
// t.is(response.type, 'approver_assignments');
// });