test/unit/services.custom-class.v3.js
import test from 'ava';
import nock from 'nock';
import {
client,
clinical6,
CustomClass,
HelperFactory,
ServiceFactory
} from '../../src';
// import c6.FormatUtility.stringToCamel as stringToCamel from '../../src';
// console.log(FormatUtility.stringToCamel);
// class Kitten extends Helper {
// constructor(json = {}) {
// super(json);
// const _response = json.data || json; // if json api is passed in directly
// const attributes = _response.attributes || _response; // if json api is passed in directly
// if (_response.id) {
// /** @type {Number} */
// this.id = parseInt(_response.id, 10);
// }
// // Object.assign(this, attributes);
// Object.keys(attributes).forEach((key) => {
// if (['type', 'relationships', 'id'].indexOf(key) === -1) {
// /** @type {Any} */
// this[FormatUtility.stringToCamel(key)] = attributes[key];
// }
// });
// this.deserializeRelationshipStubs(json);
// this.syncRelationships(json);
// }
// get type() { return 'kittens'; }
// }
class Kitten extends CustomClass {
static get type() { return 'kittens'; }
}
HelperFactory.map.kittens = Kitten;
ServiceFactory.options.kittens = { title: 'KittenService' };
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somekitten.Clinical6.com';
t.context.getResponseAll = {
data: [
{
id: '10',
type: 'kittens',
attributes: {
kitten_id: '362932',
name: 'Frankie Hermann',
email: 'lisa@kulas.info',
phone_number: null,
fax_number: null
},
},
{
id: '11',
type: 'kittens',
attributes: {
kitten_id: '362854',
name: 'Frankie Hermann',
email: 'lisa@kulas.info',
phone_number: null,
fax_number: null
},
},
{
id: '12',
type: 'kittens',
attributes: {
kitten_id: '742129',
name: 'Frankie Hermann',
email: 'lisa@kulas.info',
phone_number: null,
fax_number: null
},
}
],
};
t.context.getResponseId = {
data: {
id: '15',
type: 'kittens',
attributes: {
kitten_id: '922176',
name: 'Frankie Hermann',
email: 'lisa@kulas.info',
phone_number: null,
fax_number: null
},
}
};
t.context.getResponseMembers = {
data: [
{
id: '21',
type: 'kittens_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
},
{
id: '22',
type: 'kittens_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
kitten: {
data: {
id: '24',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '48',
type: 'mobile_users'
}
}
}
},
{
id: '23',
type: 'kittens_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
kitten: {
data: {
id: '24',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '49',
type: 'mobile_users'
}
}
}
},
{
id: '24',
type: 'kittens_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
kitten: {
data: {
id: '24',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '50',
type: 'mobile_users'
}
}
}
},
{
id: '25',
type: 'kittens_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
kitten: {
data: {
id: '24',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '51',
type: 'mobile_users'
}
}
}
},
{
id: '26',
type: 'kittens_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
kitten: {
data: {
id: '24',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '52',
type: 'mobile_users'
}
}
}
},
{
id: '27',
type: 'kittens_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
kitten: {
data: {
id: '24',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '53',
type: 'mobile_users'
}
}
}
},
{
id: '28',
type: 'kittens_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
kitten: {
data: {
id: '24',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '54',
type: 'mobile_users'
}
}
}
}
],
included: [
{
id: '47',
type: 'mobile_users',
attributes: {
uuid: 'd140c877-5414-46ca-b96f-60fd74461e83',
account_name: 'dummy_47',
email: 'user61@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '61',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '21',
type: 'kittens_members'
}
}
}
},
{
id: '61',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Stephany',
middle_initial: null,
last_name: 'Lubowitz',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/61/1.png',
small: {
url: '/uploads/test/profile/avatar/61/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/61/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/61/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user61@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '47',
type: 'mobile_users'
}
}
}
},
{
id: '48',
type: 'mobile_users',
attributes: {
uuid: '08e956a6-929c-48ce-b913-0386bd5f0220',
account_name: 'dummy_48',
email: 'user62@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '62',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '22',
type: 'kittens_members'
}
}
}
},
{
id: '62',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Rosalyn',
middle_initial: null,
last_name: 'Conn',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/62/1.png',
small: {
url: '/uploads/test/profile/avatar/62/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/62/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/62/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user62@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '48',
type: 'mobile_users'
}
}
}
},
{
id: '49',
type: 'mobile_users',
attributes: {
uuid: 'ce178761-cb55-4d59-9f8a-7d797c71290f',
account_name: 'dummy_49',
email: 'user63@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '63',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '23',
type: 'kittens_members'
}
}
}
},
{
id: '63',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Triston',
middle_initial: null,
last_name: 'Gorczany',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/63/1.png',
small: {
url: '/uploads/test/profile/avatar/63/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/63/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/63/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user63@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '49',
type: 'mobile_users'
}
}
}
},
{
id: '50',
type: 'mobile_users',
attributes: {
uuid: '901741a7-2b6d-4f9c-9d00-d470b86d2f66',
account_name: 'dummy_50',
email: 'user64@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '64',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '24',
type: 'kittens_members'
}
}
}
},
{
id: '64',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Gregg',
middle_initial: null,
last_name: 'Rodriguez',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/64/1.png',
small: {
url: '/uploads/test/profile/avatar/64/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/64/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/64/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user64@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '50',
type: 'mobile_users'
}
}
}
},
{
id: '51',
type: 'mobile_users',
attributes: {
uuid: 'ec259c81-a4a4-466c-be64-adfc7eb71e03',
account_name: 'dummy_51',
email: 'user65@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '65',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '25',
type: 'kittens_members'
}
}
}
},
{
id: '65',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Alfonso',
middle_initial: null,
last_name: 'Rippin',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/65/1.png',
small: {
url: '/uploads/test/profile/avatar/65/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/65/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/65/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user65@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '51',
type: 'mobile_users'
}
}
}
},
{
id: '52',
type: 'mobile_users',
attributes: {
uuid: 'a4b0974a-6e1b-432d-8087-6121b925dffb',
account_name: 'dummy_52',
email: 'user66@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '66',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '26',
type: 'kittens_members'
}
}
}
},
{
id: '66',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Robbie',
middle_initial: null,
last_name: 'Schinner',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/66/1.png',
small: {
url: '/uploads/test/profile/avatar/66/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/66/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/66/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user66@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '52',
type: 'mobile_users'
}
}
}
},
{
id: '53',
type: 'mobile_users',
attributes: {
uuid: '49c35ac6-4483-4ff4-b7e7-43aa3b7500c6',
account_name: 'dummy_53',
email: 'user67@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '67',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '27',
type: 'kittens_members'
}
}
}
},
{
id: '67',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Justyn',
middle_initial: null,
last_name: 'Jaskolski',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/67/1.png',
small: {
url: '/uploads/test/profile/avatar/67/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/67/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/67/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user67@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '53',
type: 'mobile_users'
}
}
}
},
{
id: '54',
type: 'mobile_users',
attributes: {
uuid: 'f7eeac76-0057-444e-9bc8-feaf29018dc3',
account_name: 'dummy_54',
email: 'user68@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '68',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '28',
type: 'kittens_members'
}
}
}
},
{
id: '68',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Verlie',
middle_initial: null,
last_name: 'Treutel',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/68/1.png',
small: {
url: '/uploads/test/profile/avatar/68/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/68/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/68/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user68@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '54',
type: 'mobile_users'
}
}
}
}
]
};
t.context.getResponseMember20 = {
data: {
id: '20',
type: 'kittens_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:11Z',
updated_at: '2018-02-07T17:48:11Z'
},
relationships: {
kitten: {
data: {
id: '22',
type: 'kittens'
}
},
mobile_user: {
data: {
id: '46',
type: 'mobile_users'
}
}
}
},
included: [
{
id: '46',
type: 'mobile_users',
attributes: {
uuid: '21660223-8d6f-4a5e-ba1a-581656e65b4e',
account_name: 'dummy_46',
email: 'user58@fake.com',
created_at: '2018-02-07T17:48:11Z',
updated_at: '2018-02-07T17:48:11Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '58',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '20',
type: 'kittens_members'
}
}
}
},
{
id: '58',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:11Z',
updated_at: '2018-02-07T17:48:11Z',
first_name: 'Ellis',
middle_initial: null,
last_name: 'Rempel',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/58/1.png',
small: {
url: '/uploads/test/profile/avatar/58/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/58/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/58/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user58@fake.com',
age: 33,
language: {
id: 1,
iso: 'en',
name: 'English',
created_at: '2018-02-07T17:47:57Z',
updated_at: '2018-02-07T17:47:57Z',
is_default: true,
image: {
url: '/default/default.png',
thumb: {
url: '/default/thumb_default.png'
}
}
}
},
relationships: {
profileable: {
data: {
id: '46',
type: 'mobile_users'
}
}
}
}
]
};
t.context.insertResponse = {
data: {
id: '12',
type: 'kittens',
attributes: {
kitten_id: '652286',
name: 'Ms. Elenor Hodkiewicz',
email: 'roger.medhurst@schoen.info',
phone_number: null,
fax_number: null
},
relationships: {
contact: {
data: null
},
location: {
data: null
},
agreement_templates: {
data: []
}
}
}
};
t.context.updateResponse = {
data: {
id: '18',
type: 'kittens',
attributes: {
kitten_id: '413326',
name: 'Ms. Elenor Hodkiewicz',
email: 'roger.medhurst@schoen.info',
phone_number: null,
fax_number: null
},
relationships: {
contact: {
data: null
},
location: {
data: {
id: '117',
type: 'locations'
}
},
agreement_templates: {
data: []
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.kittenJsonApi = {
data: {
id: 18,
type: 'kittens',
attributes: {
kitten_id: '413326',
name: 'Ms. Elenor Hodkiewicz',
email: 'roger.medhurst@schoen.info',
phone_number: null,
fax_number: null
},
relationships: {
contact: {
data: null
},
location: {
data: {
id: 117,
type: 'locations'
}
},
agreement_templates: {
data: []
}
}
}
};
t.context.kitten = new Kitten(t.context.kittenJsonApi);
});
// KittenService.delete method
/**
* @test {Clinical6.delete}
*/
test('[unit] KittenService.delete should throw errors for invalid parameters', async (t) => {
const title = `KittenService.delete error`;
await t.throwsAsync(clinical6.delete(new Kitten()), `${title}: kitten does not have id`);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] KittenService.delete should receive a valid response for a delete request', async (t) => {
const { kitten } = t.context;
kitten.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/kittens/${kitten.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await clinical6.delete(kitten);
t.is(request.path, `/v3/kittens/${kitten.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 {CustomClass.delete}
*/
test('[unit] CustomClass.delete should receive a valid response for a delete request', async (t) => {
const { kitten } = t.context;
kitten.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/kittens/${kitten.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await kitten.delete();
t.is(request.path, `/v3/kittens/${kitten.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] KittenService.delete should remove the element from local storage', async (t) => {
const { kitten, storage } = t.context;
kitten.id = 5;
nock(client.apiBaseUrl).delete(`/v3/kittens/${kitten.id}`).reply(200, '');
await storage.set(kitten.type, kitten.toJSON(), { id: kitten.id });
await clinical6.delete(kitten);
t.is(storage.has(kitten.type, { id: kitten.id }), false);
});
// KittenService.get method
/**
* @test {Clinical6.get}
*/
test.serial('[unit] KittenService.get should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const title = `KittenService.get error`;
await t.throwsAsync(clinical6.get(Kitten), `${title}: requires authToken`);
});
/**
* @test {Clinical6.get}
*/
test('[unit] KittenService.get should receive a valid response for a get request without an id', async (t) => {
const { getResponseAll } = t.context;
let request = {};
nock(client.apiBaseUrl).get(`/v3/kittens`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAll];
});
const response = await clinical6.get(Kitten);
t.is(request.path, `/v3/kittens`);
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.is(response[0].id, 10);
t.is(response[0].type, 'kittens');
t.is(response[0].kittenId, '362932');
t.is(response[0].name, 'Frankie Hermann');
t.is(response[0].email, 'lisa@kulas.info');
t.falsy(response[0].phoneNumber);
t.falsy(response[0].faxNumber);
t.is(response[1].id, 11);
t.is(response[1].type, 'kittens');
t.is(response[1].kittenId, '362854');
t.is(response[1].name, 'Frankie Hermann');
t.is(response[1].email, 'lisa@kulas.info');
t.falsy(response[1].phoneNumber);
t.falsy(response[1].faxNumber);
t.is(response[2].id, 12);
t.is(response[2].type, 'kittens');
t.is(response[2].kittenId, '742129');
t.is(response[2].name, 'Frankie Hermann');
t.is(response[2].email, 'lisa@kulas.info');
t.falsy(response[2].phoneNumber);
t.falsy(response[2].faxNumber);
});
/**
* @test {Clinical6.get}
*/
test('[unit] KittenService.get should receive a valid response for a get request with an id', async (t) => {
const { getResponseId } = t.context;
const kitten = new Kitten({ id: 15 });
nock(client.apiBaseUrl).get(`/v3/kittens/${kitten.id}`).reply(200, getResponseId);
const response = await clinical6.get(new Kitten({ id: 15 }));
t.truthy(response);
t.is(response.id, 15);
t.is(response.type, 'kittens');
t.is(response.kittenId, '922176');
t.is(response.name, 'Frankie Hermann');
t.is(response.email, 'lisa@kulas.info');
t.falsy(response.phoneNumber);
t.falsy(response.faxNumber);
});
// KittenService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] KittenService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const title = `KittenService.insert error`;
await t.throwsAsync(clinical6.insert(new Kitten()), `${title}: requires authToken`);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] KittenService.insert should successfully insert a kitten with a kitten object', async (t) => {
const { insertResponse } = t.context;
let request = {};
nock(client.apiBaseUrl).post(`/v3/kittens`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const requestJsonApi = {
data: {
type: 'kittens',
attributes: {
email: 'roger.medhurst@schoen.info',
name: 'Ms. Elenor Hodkiewicz'
}
}
};
const kitten = new Kitten(requestJsonApi);
const response = await clinical6.insert(kitten);
t.is(request.path, `/v3/kittens`);
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, 12);
t.is(response.type, 'kittens');
t.is(response.kittenId, '652286');
t.is(response.name, 'Ms. Elenor Hodkiewicz');
t.is(response.email, 'roger.medhurst@schoen.info');
t.falsy(response.phoneNumber);
t.falsy(response.faxNumber);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] KittenService.insert should throw an error with an invalid token', async (t) => {
let request = {};
nock(client.apiBaseUrl).post(`/v3/kittens`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [422, {
errors: [
{
source: {
pointer: '/data/attributes/name'
},
detail: 'can\'t be blank'
}
]
}];
});
const requestJsonApi = {
data: {
type: 'kittens',
attributes: {
email: 'roger.medhurst@schoen.info'
}
}
};
const kitten = new Kitten(requestJsonApi);
const response = await clinical6.insert(kitten).catch(err => err);
t.is(request.path, `/v3/kittens`);
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.message, `"/data/attributes/name": can't be blank`);
});
/**
* @test {CustomClass.save}
*/
test('[unit] CustomClass.save should successfully insert a kitten with a kitten object', async (t) => {
const { insertResponse } = t.context;
let request = {};
nock(client.apiBaseUrl).post(`/v3/kittens`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const requestJsonApi = {
data: {
type: 'kittens',
attributes: {
email: 'roger.medhurst@schoen.info',
name: 'Ms. Elenor Hodkiewicz'
}
}
};
const kitten = new Kitten(requestJsonApi);
const response = await kitten.save();
t.is(request.path, `/v3/kittens`);
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, 12);
t.is(response.type, 'kittens');
t.is(response.kittenId, '652286');
t.is(response.name, 'Ms. Elenor Hodkiewicz');
t.is(response.email, 'roger.medhurst@schoen.info');
t.falsy(response.phoneNumber);
t.falsy(response.faxNumber);
});
// KittenService.update method
/**
* @test {Clinical6.update}
*/
test.serial('[unit] KittenService.update should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const title = `KittenService.update error`;
await t.throwsAsync(clinical6.update(new Kitten({ id: 5 })), `${title}: requires authToken`);
});
/**
* @test {Clinical6.update}
*/
test('[unit] KittenService.update should successfully update a kitten with a kitten object', async (t) => {
const { kitten, updateResponse, kittenJsonApi } = t.context;
let request = {};
nock(client.apiBaseUrl).patch(`/v3/kittens/${kitten.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await clinical6.update(kitten);
t.is(request.path, `/v3/kittens/${kitten.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, kittenJsonApi);
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 18);
t.is(response.type, 'kittens');
});
/**
* @test {CustomClass.save}
*/
test('[unit] CustomClass.save should successfully update a kitten with a kitten object', async (t) => {
const { kitten, updateResponse, kittenJsonApi } = t.context;
let request = {};
nock(client.apiBaseUrl).patch(`/v3/kittens/${kitten.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
// const response = await clinical6.update(kitten);
const response = await kitten.save();
t.is(request.path, `/v3/kittens/${kitten.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, kittenJsonApi);
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 18);
t.is(response.type, 'kittens');
});