test/unit/helpers.consent-additional-signer.js
import test from 'ava';
import nock from 'nock';
import {
client,
ConsentAdditionalSigner,
} 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: 2,
type: 'consent__additional_signatures',
attributes: {
name: 'A proxy signer',
deleted_at: null,
created_at: '2018-09-18T21:35:04Z',
updated_at: '2018-09-18T21:35:04Z'
},
relationships: {
form: {
data: {
id: 29,
type: 'consent__forms'
}
}
}
}
};
t.context.consentAdditionalSignerJsonApi = {
type: 'consent__additional_signatures',
attributes: {
name: 'A proxy signer',
deleted_at: null,
created_at: '2018-09-18T21:35:04Z',
updated_at: '2018-09-18T21:35:04Z',
},
relationships: {
form: {
data: {
id: 29,
type: 'consent__forms'
}
}
}
};
t.context.consentAdditionalSignerJson = {
type: 'consent__additional_signatures',
name: 'A proxy signer',
deleted_at: null,
created_at: '2018-09-18T21:35:04Z',
updated_at: '2018-09-18T21:35:04Z',
relationships: {
form: {
data: {
id: 29,
type: 'consent__forms'
}
}
}
};
client.storageUtility.clear();
t.context.storage = client.storageUtility;
client.consentAdditionalSigner = new ConsentAdditionalSigner({});
t.context.consentAdditionalSigner = new ConsentAdditionalSigner({ data: t.context.consentAdditionalSignerJsonApi });
});
/**
* @test {ConsentAdditionalSigner}
*/
test('[unit] consentAdditionalSigner should handle consentAdditionalSigner data with a normal json format', (t) => {
const { consentAdditionalSignerJson } = t.context;
const consentAdditionalSigner = new ConsentAdditionalSigner(consentAdditionalSignerJson);
t.is(consentAdditionalSigner.type, 'consent__additional_signatures');
t.is(consentAdditionalSigner.name, 'A proxy signer');
t.is(consentAdditionalSigner.deletedAt, null);
t.is(consentAdditionalSigner.createdAt, '2018-09-18T21:35:04Z');
t.is(consentAdditionalSigner.updatedAt, '2018-09-18T21:35:04Z');
t.is(consentAdditionalSigner.form.id, 29);
t.is(consentAdditionalSigner.form.type, 'consent__forms');
});
/**
* @test {ConsentAdditionalSigner}
*/
test('[unit] ConsentAdditionalSigner should handle consentAdditionalSigner data with json api format', (t) => {
const { consentAdditionalSignerJsonApi } = t.context;
const consentAdditionalSigner = new ConsentAdditionalSigner({ data: consentAdditionalSignerJsonApi });
t.is(consentAdditionalSigner.type, 'consent__additional_signatures');
t.is(consentAdditionalSigner.name, 'A proxy signer');
t.is(consentAdditionalSigner.deletedAt, null);
t.is(consentAdditionalSigner.createdAt, '2018-09-18T21:35:04Z');
t.is(consentAdditionalSigner.updatedAt, '2018-09-18T21:35:04Z');
t.is(consentAdditionalSigner.form.id, 29);
t.is(consentAdditionalSigner.form.type, 'consent__forms');
});
/**
* @test {ConsentAdditionalSigner}
*/
test('[unit] ConsentAdditionalSigner should generate json api format when converted to string', (t) => {
const { consentAdditionalSignerJsonApi } = t.context;
let consentAdditionalSigner = new ConsentAdditionalSigner({ data: consentAdditionalSignerJsonApi });
t.deepEqual(consentAdditionalSigner.toJSON(), consentAdditionalSignerJsonApi);
consentAdditionalSigner = new ConsentAdditionalSigner({ data: consentAdditionalSignerJsonApi });
t.deepEqual(consentAdditionalSigner.toJSON(), consentAdditionalSignerJsonApi);
});
/**
* @test {ConsentAdditionalSigner.save}
*/
test('[unit] ConsentAdditionalSigner.save should successfully save a new consentAdditionalSigner when id does not exist', async (t) => {
const { consentAdditionalSignerJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(consentAdditionalSignerJsonApi));
// delete json.id;
const consentAdditionalSigner = new ConsentAdditionalSigner({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/additional_signatures`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await consentAdditionalSigner.save();
t.is(request.path, `/v3/consent/additional_signatures`);
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, 2);
t.is(response.type, 'consent__additional_signatures');
t.is(response.deletedAt, null);
t.is(response.createdAt, '2018-09-18T21:35:04Z');
t.is(response.updatedAt, '2018-09-18T21:35:04Z');
t.is(response.form.id, 29);
t.is(response.form.type, 'consent__forms');
});