test/unit/services.consent-approver.js
import test from 'ava';
import nock from 'nock';
import {
client,
clinical6,
ConsentApprover,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.getResponseAll = {
data: [
{
id: '17',
type: 'consent__approvers',
attributes: {
first_name: 'name-17',
last_name: 'last-17',
email: 'email17@address.com',
title: null
}
},
{
id: '18',
type: 'consent__approvers',
attributes: {
first_name: 'name-18',
last_name: 'last-18',
email: 'email18@address.com',
title: null
}
},
{
id: '19',
type: 'consent__approvers',
attributes: {
first_name: 'name-19',
last_name: 'last-19',
email: 'email19@address.com',
title: null
}
}
],
included: [
{
id: '3',
type: 'filter_groups',
attributes: {
operator: 'and',
created_at: '2018-04-17T16:14:07Z',
updated_at: '2018-04-17T16:14:07Z'
},
relationships: {
cohort: {
data: {
id: '20',
type: 'cohorts'
}
}
}
},
{
id: '4',
type: 'filter_groups',
attributes: {
operator: 'and',
created_at: '2018-04-17T16:14:07Z',
updated_at: '2018-04-17T16:14:07Z'
},
relationships: {
cohort: {
data: {
id: '21',
type: 'cohorts'
}
}
}
}
]
};
t.context.getResponseId = {
data: {
id: 21,
type: 'consent__approvers',
attributes: {
first_name: 'Name',
last_name: 'Last',
email: 'email',
title: null
},
relationships: {
consent_form_version: {
data: null
}
}
}
};
t.context.patchResponse = {
data: {
id: 14,
type: 'consent__approvers',
attributes: {
first_name: 'Snoop',
last_name: 'Dogg',
email: 'email',
title: null,
password: null,
},
relationships: {
consent_form_version: {
data: null
}
}
}
};
t.context.consentApproverJsonApi = {
data: {
id: 14,
type: 'consent__approvers',
attributes: {
first_name: 'Snoop',
last_name: 'Dogg',
email: 'email'
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.consentApprover = new ConsentApprover(t.context.consentApproverJsonApi);
});
// ConsentApproverService.get method
/**
* @test {Clinical6.get}
*/
test('[unit] ConsentApproverService.get should make a properly formatted get request and response without an id', async (t) => {
const { getResponseAll } = t.context;
let request = {};
nock(client.apiBaseUrl).get('/v3/consent/approvers').reply(function () {
request = this.req;
return [200, getResponseAll];
});
const response = await clinical6.get(ConsentApprover);
t.is(request.path, `/v3/consent/approvers`);
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(Object.keys(response).length, 3);
t.truthy(response[0] instanceof ConsentApprover);
});
/**
* @test {Clinical6.get}
*/
test.serial('[unit ConsentApproverService.get should receive a valid response for a get request with an id', async (t) => {
const { getResponseId } = t.context;
nock(client.apiBaseUrl).get('/v3/consent/approvers/21').reply(200, getResponseId);
const response = await clinical6.get(new ConsentApprover({ id: 21 }));
t.truthy(response);
t.is(response.id, 21);
t.is(response.type, 'consent__approvers');
t.is(response.firstName, 'Name');
t.is(response.lastName, 'Last');
t.is(response.email, 'email');
t.is(response.title, null);
});
// ConsentApproverService.update method
/**
* @test {Clinical6.update}
*/
test.serial('[unit] ConsentApproverService.update should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'ConsentApproverService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new ConsentApprover()), expectedError);
});
// ConsentApproverService.update method
/**
* @test {Clinical6.update}
*/
test('[unit] ConsentApproverService.update should successfully update a consentApprover with a consentApprover object', async (t) => {
const { consentApprover, patchResponse, consentApproverJsonApi } = t.context;
let request = {};
nock(client.apiBaseUrl).patch(/\/v3\/consent\/approvers\/([0-9]*)$/).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, patchResponse];
});
const response = await clinical6.update(consentApprover);
t.is(request.path, `/v3/consent/approvers/${consentApprover.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.deepEqual(request.requestBody, consentApproverJsonApi);
t.is(response.id, 14);
t.is(response.type, 'consent__approvers');
t.is(response.firstName, 'Snoop');
t.is(response.lastName, 'Dogg');
t.is(response.email, 'email');
t.is(response.title, null);
t.is(response.password, null);
t.is(response.consentFormVersion, null);
});