Manual Reference Source Test

test/unit/services.consent-additional-signer.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  clinical6,
  ConsentAdditionalSigner,
} from '../../src';

test.before('start server', (t) => {
  t.context.storage = client.storageUtility;
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
  t.context.consentAdditionalSignerJsonApi = {
    data: {
      type: 'consent__additional_signers',
      attributes: {
        name: 'A proxy signer'
      },
      relationships: {
        form: {
          data: {
            id: 29,
            type: 'consent__forms'
          }
        }
      }
    }
  };
});

test.after('server shut down', () => {});

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.consentAdditionalSigner = new ConsentAdditionalSigner(t.context.consentAdditionalSignerJsonApi);
});


// ConsentAdditionalSignerService.insert method
/**
 * @test {Clinical6.insert}
 */
test.serial('[unit] ConsentAdditionalSignerService.insert should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const expectedError = 'ConsentAdditionalSignerService.insert error: requires authToken';
  await t.throwsAsync(clinical6.insert(new ConsentAdditionalSigner()), expectedError);
});

/**
 * @test {Clinical6.insert}
 */

test('[unit] ConsentAdditionalSignerService.insert should successfully insert a consentAdditionalSigner with a consentAdditionalSigner object', async (t) => {
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/consent/additional_signatures`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [201, {
      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'
            }
          }
        }
      }
    }];
  });

  const requestJsonApi = {
    data: {
      type: 'consent__additional_signatures',
      attributes: {
        name: 'A proxy signer'
      },
      relationships: {
        form: {
          data: {
            id: 29,
            type: 'consent__forms'
          }
        }
      }
    }
  };

  const consentAdditionalSigner = new ConsentAdditionalSigner(requestJsonApi);
  const response = await clinical6.insert(consentAdditionalSigner);

  t.is(request.path, `/v3/consent/additional_signatures`);
  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, 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');
});