test/unit/services.site-contact.js
import test from 'ava';
import nock from 'nock';
import { client, SiteContact, clinical6 } from '../../src';
test.before('start server', (t) => {
t.context.storage = client.storageUtility;
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.siteContactJsonApi = {
data: {
type: 'trials__site_contacts',
attributes: {
first_name: 'Eliezer',
last_name: 'Dickens',
email: 'oma.senger@hoppe.io',
phone: undefined,
fax: undefined,
primary_contact: true,
},
relationships: {
site: {
data: {
id: 21,
type: 'trials__sites'
}
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.siteContact = new SiteContact(t.context.siteContactJsonApi);
});
// SiteContactService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] SiteContactService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'SiteContactService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new SiteContact()), expectedError);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] SiteContactService.insert should successfully insert a siteContact with a siteContact object', async (t) => {
let request = {};
nock(client.apiBaseUrl).post(`/v3/trials/site_contacts`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, {
data: {
id: 4,
type: 'trials__site_contacts',
attributes: {
first_name: 'Eliezer',
last_name: 'Dickens',
email: 'oma.senger@hoppe.io',
phone: null,
fax: null,
primary_contact: true
},
relationships: {
site: {
data: {
id: 21,
type: 'trials__sites'
}
}
}
},
included: [
{
id: 21,
type: 'trials__sites',
attributes: {
site_id: '911572',
external_identifier: '911572',
name: 'Lonny Dicki',
email: 'oma.senger@hoppe.io',
phone_number: null,
fax_number: null,
contact_name: 'Eliezer Dickens',
contact_email: 'oma.senger@hoppe.io',
contact_phone: null,
contact_fax: null,
created_at: '2018-05-31T20:35:56Z',
updated_at: '2018-05-31T20:35:56Z'
},
relationships: {
location: {
data: {
id: 55,
type: 'locations'
}
},
agreement_templates: {
data: []
},
site_contacts: {
data: [
{
id: 4,
type: 'trials__site_contacts'
}
]
}
}
}
]
}];
});
const requestJsonApi = {
data: {
type: 'trials__site_contacts',
attributes: {
first_name: 'Eliezer',
last_name: 'Dickens',
email: 'oma.senger@hoppe.io',
primary_contact: true,
},
relationships: {
site: {
data: {
id: 21,
type: 'trials__sites'
}
}
}
}
};
const siteContact = new SiteContact(requestJsonApi);
const response = await clinical6.insert(siteContact);
t.is(request.path, `/v3/trials/site_contacts`);
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, 4);
t.is(response.type, 'trials__site_contacts');
t.is(response.firstName, 'Eliezer');
t.is(response.lastName, 'Dickens');
t.is(response.email, 'oma.senger@hoppe.io');
t.is(response.phone, null);
t.is(response.fax, null);
t.is(response.primaryContact, true);
t.is(response.site.id, 21);
t.is(response.site.type, 'trials__sites');
t.is(response.site.siteId, '911572');
t.is(response.site.name, 'Lonny Dicki');
t.is(response.site.location.id, 55);
t.is(response.site.location.type, 'locations');
t.is(response.site.agreementTemplates.length, 0);
t.is(response.site.contacts.length, 1);
t.is(response.site.contacts[0].id, 4);
t.is(response.site.contacts[0].type, 'trials__site_contacts');
});
// SiteContactService.update method
/**
* @test {SiteContactService.update}
*/
test('[unit] SiteContactService.update should successfully update a siteContact with a siteContact object when an id exists', async (t) => {
const siteContactPatchJsonApi = {
id: 2,
type: 'trials__site_contacts',
attributes: {
first_name: 'Marilou',
last_name: 'Prohaska',
email: 'adalberto@ondrickabartoletti.com'
}
};
let requestBody = {};
const server = nock(client.apiBaseUrl)
.patch('/v3/trials/site_contacts/2', (body) => {
requestBody = body;
return { data: json };
})
.reply(201, {
data: {
id: 2,
type: 'trials__site_contacts',
attributes: {
first_name: 'Marilou',
last_name: 'Prohaska',
email: 'adalberto@ondrickabartoletti.com',
phone: null,
fax: null,
primary_contact: false
},
relationships: {
site: {
data: {
id: 48,
type: 'trials__sites'
}
}
}
},
included: [
{
id: 48,
type: 'trials__sites',
attributes: {
site_id: '138479',
external_identifier: '138479',
name: 'Norberto Nicolas',
email: 'oscar@shields.com',
phone_number: null,
fax_number: null,
contact_name: 'Christina Rutherford',
contact_email: 'oscar@shields.com',
contact_phone: null,
contact_fax: null,
created_at: '2018-06-05T15:47:08Z',
updated_at: '2018-06-05T15:47:08Z'
},
relationships: {
location: {
data: {
id: 91,
type: 'locations'
}
},
agreement_templates: {
data: []
},
site_contacts: {
data: [
{
id: 1,
type: 'trials__site_contacts'
},
{
id: 2,
type: 'trials__site_contacts'
}
]
}
}
}
]
});
const json = JSON.parse(JSON.stringify(siteContactPatchJsonApi));
const siteContact = new SiteContact({ data: json });
const response = await clinical6.update(siteContact);
t.deepEqual(requestBody, { data: json });
server.done();
t.is(response.id, 2);
t.is(response.type, 'trials__site_contacts');
t.is(response.firstName, 'Marilou');
t.is(response.lastName, 'Prohaska');
t.is(response.email, 'adalberto@ondrickabartoletti.com');
t.is(response.phone, null);
t.is(response.fax, null);
t.is(response.primaryContact, false);
t.is(response.site.id, 48);
t.is(response.site.type, 'trials__sites');
t.is(response.site.siteId, '138479');
t.is(response.site.name, 'Norberto Nicolas');
});