test/unit/helpers.site-contact.js
import test from 'ava';
import nock from 'nock';
import {
client,
SiteContact,
} from '../../src';
// See tests.js for testing client creation
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.insertResponse = {
data: {
id: 4,
type: 'trials__site_contacts',
attributes: {
first_name: 'Eliezer',
last_name: 'Dickens',
email: 'oma.senger@hoppe.io',
phone: null,
fax: null,
content_type: 'referring_doctor',
contact_type: 'Main',
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'
}
]
}
}
}
]
};
t.context.siteContactJsonApi = {
type: 'trials__site_contacts',
attributes: {
first_name: 'Eliezer',
last_name: 'Dickens',
email: 'oma.senger@hoppe.io',
phone: undefined,
fax: undefined,
content_type: 'referring_doctor',
contact_type: 'Main',
primary_contact: true,
created_at: undefined,
updated_at: undefined
},
relationships: {
site: {
data: {
id: 21,
type: 'trials__sites'
}
}
}
};
t.context.siteContactJson = {
type: 'trials__site_contacts',
first_name: 'Eliezer',
last_name: 'Dickens',
email: 'oma.senger@hoppe.io',
phone: undefined,
fax: undefined,
primary_contact: true,
created_at: undefined,
updated_at: undefined,
relationships: {
site: {
data: {
id: 21,
type: 'trials__sites'
}
}
}
};
t.context.siteContactPatchJsonApi = {
id: 2,
type: 'trials__site_contacts',
attributes: {
first_name: 'Marilou',
last_name: 'Prohaska',
email: 'adalberto@ondrickabartoletti.com'
}
};
client.storageUtility.clear();
t.context.storage = client.storageUtility;
client.siteContact = new SiteContact({});
t.context.siteContact = new SiteContact({ data: t.context.siteContactJsonApi });
});
/**
* @test {SiteContact}
*/
test('[unit] SiteContact should handle siteContact data with a normal json format', (t) => {
const { siteContactJson } = t.context;
const siteContact = new SiteContact(siteContactJson);
t.is(siteContact.type, 'trials__site_contacts');
t.is(siteContact.firstName, 'Eliezer');
t.is(siteContact.lastName, 'Dickens');
t.is(siteContact.email, 'oma.senger@hoppe.io');
t.is(siteContact.phone, undefined);
t.is(siteContact.fax, undefined);
t.is(siteContact.primaryContact, true);
t.is(siteContact.createdAt, undefined);
t.is(siteContact.updatedAt, undefined);
t.is(siteContact.relationships.relationships.site.data.id, 21);
t.is(siteContact.relationships.relationships.site.data.type, 'trials__sites');
});
/**
* @test {SiteContact}
*/
test('[unit] SiteContact should handle siteContact data with json api format', (t) => {
const { siteContactJsonApi } = t.context;
const siteContact = new SiteContact({ data: siteContactJsonApi });
t.is(siteContact.type, 'trials__site_contacts');
t.is(siteContact.firstName, 'Eliezer');
t.is(siteContact.lastName, 'Dickens');
t.is(siteContact.email, 'oma.senger@hoppe.io');
t.is(siteContact.phone, undefined);
t.is(siteContact.fax, undefined);
t.is(siteContact.primaryContact, true);
t.is(siteContact.createdAt, undefined);
t.is(siteContact.updatedAt, undefined);
t.is(siteContact.relationships.relationships.site.data.id, 21);
t.is(siteContact.relationships.relationships.site.data.type, 'trials__sites');
});
/**
* @test {SiteContact}
*/
test('[unit] SiteContact should generate json api format when converted to string', (t) => {
const { siteContactJsonApi } = t.context;
let siteContact = new SiteContact({ data: siteContactJsonApi });
t.deepEqual(siteContact.toJSON(), siteContactJsonApi);
siteContact = new SiteContact({ data: siteContactJsonApi });
t.deepEqual(siteContact.toJSON(), siteContactJsonApi);
});
/**
* @test {SiteContact.save}
*/
test('[unit] SiteContact.save should successfully save a new siteContact when id does not exist', async (t) => {
const { siteContactJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(siteContactJsonApi));
// delete json.id;
const siteContact = new SiteContact({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/trials/site_contacts`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await siteContact.save();
t.is(request.path, `/v3/trials/site_contacts`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: json });
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.contentType, 'referring_doctor');
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');
});
/**
* @test {SiteContact.save}
*/
test('[unit] SiteContact.save should successfully update a siteContact with a siteContact object when an id exists', async (t) => {
const { siteContactPatchJsonApi } = t.context;
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 siteContact.save();
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');
});