test/unit/services.cohort-assignment.js
import test from 'ava';
import nock from 'nock';
import {
client,
clinical6,
Cohort,
CohortAssignment,
User,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.insertResponse = {
data: {
id: '1',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-17T19:59:53Z',
updated_at: '2018-04-17T19:59:53Z'
},
relationships: {
user: {
data: {
id: '278',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '2',
type: 'cohorts'
}
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.cohortAssignmentJsonApi = {
data: {
id: '1',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-17T19:59:53Z',
updated_at: '2018-04-17T19:59:53Z'
},
relationships: {
user: {
data: {
id: '278',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '2',
type: 'cohorts'
}
}
}
}
};
t.context.cohortAssignment = new CohortAssignment(t.context.cohortAssignmentJsonApi);
});
// CohortAssignmentService.delete method
/**
* @test {Clinical6.delete}
*/
test('[unit] CohortAssignmentService.delete should throw errors for invalid parameters', async (t) => {
const title = `CohortAssignmentService.delete error`;
await t.throwsAsync(clinical6.delete(new CohortAssignment()), `${title}: cohort_assignment does not have id`);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] CohortAssignmentService.delete should receive a valid response for a delete request', async (t) => {
const { cohortAssignment } = t.context;
cohortAssignment.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/cohort_assignments/${cohortAssignment.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await clinical6.delete(cohortAssignment);
t.is(request.path, `/v3/cohort_assignments/${cohortAssignment.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] CohortAssignmentService.delete should remove the element from local storage', async (t) => {
const { cohortAssignment, storage } = t.context;
cohortAssignment.id = 5;
nock(client.apiBaseUrl).delete(`/v3/cohort_assignments/${cohortAssignment.id}`).reply(200, '');
await storage.set(cohortAssignment.type, cohortAssignment.toJSON(), { id: cohortAssignment.id });
await clinical6.delete(cohortAssignment);
t.is(storage.has(cohortAssignment.type, { id: cohortAssignment.id }), false);
});
// // CohortAssignmentService.get method
// /**
// * @test {Clinical6.get}
// */
// test.serial('[unit] CohortAssignmentService.get should throw an error when there is no authToken', async (t) => {
// client.authToken = undefined;
// const title = `CohortAssignmentService.get error`;
// await t.throwsAsync(clinical6.get(CohortAssignment), `${title}: requires authToken`);
// });
// // /**
// // * @test {Clinical6.get}
// // */
// // test('[unit] CohortAssignmentService.get should make a properly formatted get request', async (t) => {
// // await clinical6.get(CohortAssignment);
// // t.is(request.path, `/v3/cohort_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] CohortAssignmentService.get should receive a valid response for a get request without an id', async (t) => {
// const response = await clinical6.get(CohortAssignment);
// t.truthy(response);
// t.is(Object.keys(response).length, 3);
// t.is(response[0].id, 24);
// t.is(response[0].type, 'cohort_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].mobileUser.id, 76);
// t.is(response[0].mobileUser.profile.firstName, 'Jillian');
// t.is(response[0].mobileUser.profile.lastName, 'Reilly');
// t.is(response[1].id, 25);
// t.is(response[1].type, 'cohort_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].mobileUser.id, 77);
// t.is(response[1].mobileUser.profile.firstName, 'Beaulah');
// t.is(response[1].mobileUser.profile.lastName, 'Rogahn');
// t.is(response[2].id, 26);
// t.is(response[2].type, 'cohort_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].mobileUser.id, 78);
// t.is(response[2].mobileUser.profile.firstName, 'Stanton');
// t.is(response[2].mobileUser.profile.lastName, 'Rice');
// });
// /**
// * @test {Clinical6.get}
// */
// test('[unit] CohortAssignmentService.get should receive a valid response for a get request with an id', async (t) => {
// const response = await clinical6.get(new CohortAssignment({ id: 27 }));
// t.truthy(response);
// t.is(response.id, 27);
// t.is(response.type, 'cohort_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.mobileUser.id, 80);
// t.is(response.mobileUser.profile.firstName, 'Ana');
// t.is(response.mobileUser.profile.lastName, 'Macejkovic');
// });
// CohortAssignmentService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] CohortAssignmentService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const title = `CohortAssignmentService.insert error`;
await t.throwsAsync(clinical6.insert(new CohortAssignment()), `${title}: requires authToken`);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] CohortAssignmentService.insert should successfully insert a cohort with a cohort object', async (t) => {
const { insertResponse } = t.context;
let request = {};
nock(client.apiBaseUrl).post(`/v3/cohort_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const requestJsonApi = {
data: {
type: 'cohort_assignments',
attributes: {},
relationships: {
cohort: {
data: {
id: 2,
type: 'cohorts'
}
},
user: {
data: {
id: 278,
type: 'mobile_users'
}
}
}
}
};
const cohortAssignment = new CohortAssignment();
cohortAssignment.user = new User({ id: 278 });
cohortAssignment.user.store();
cohortAssignment.cohort = new Cohort({ id: 2 });
cohortAssignment.cohort.store();
const response = await clinical6.insert(cohortAssignment);
t.is(request.path, `/v3/cohort_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, 'cohort_assignments');
});
// // CohortAssignmentService.update method
// /**
// * @test {Clinical6.update}
// */
// test.serial('[unit] CohortAssignmentService.update should throw an error when there is no authToken', async (t) => {
// client.authToken = undefined;
// const title = `CohortAssignmentService.update error`;
// await t.throwsAsync(clinical6.update(new CohortAssignment({ id: 5 })), `${title}: requires authToken`);
// });
// /**
// * @test {Clinical6.update}
// */
// test('[unit] CohortAssignmentService.update should successfully update a cohort member with a cohort object', async (t) => {
// const { cohortAssignment } = t.context; // cohortAssignmentJsonApi
// cohortAssignment.mobileUser = new User({ id: 73 });
// cohortAssignment.mobileUser.store();
// cohortAssignment.cohort = new Cohort({ id: 35 });
// cohortAssignment.cohort.store();
// cohortAssignment.store();
// const response = await clinical6.update(cohortAssignment);
// // t.is(request.path, `/v3/cohort_assignments/${cohortAssignment.id}`);
// // t.is(request.headers.accept, 'application/json');
// // t.deepEqual(request.requestBody, cohortAssignmentJsonApi);
// // 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, 'cohort_assignments');
// });