Manual Reference Source Test

test/unit/services.agreement.js

import test from 'ava';
import sinon from 'sinon';
import {
  client,
  agreementService,
} from '../../src';

test.before('start server', () => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
});

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

test.beforeEach((t) => {
  client.authToken = 'valid_token';
  t.context.server = sinon.fakeServer.create();
  t.context.server.autoRespond = true;
  t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/v2/agreements/request_sign`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      id: 23,
      agreement_template_id: 5,
      status: 'approved',
      template: {
        id: 5,
        redirect_url: 'http://www.google.com'
      },
      signatures: []
    })]);
});

test.afterEach(t => t.context.server.restore());

// AgreementService.get
/**
 * @test {AgreementService.get}
 */
test('[unit] AgreementService.get should have a get method', async (t) => {
  await t.truthy(agreementService.get);
});

/**
 * @test {AgreementService.get}
 */
test.serial('[unit] AgreementService.get should throw an error when there is no authToken or incorrect parameters', (t) => {
  const pre = 'AgreementService.get error';

  client.authToken = undefined;
  t.throws(() => agreementService.get(), `${pre}: requires authToken and id is not defined and recipients is not defined`);

  client.authToken = 'valid_token';
  t.throws(() => agreementService.get(), `${pre}: id is not defined and recipients is not defined`);
  t.throws(() => agreementService.get(23), `${pre}: recipients is not defined`);
});

/**
 * @test {AgreementService.get}
 */
test('[unit] AgreementService.get should return a promise', async (t) => {
  await t.truthy(agreementService.get(23, ['test']).then);
});

/**
 * @test {AgreementService.get}
 */
test('[unit] AgreementService.get should make a properly formatted POST request and receive a valid response', async (t) => {
  const response = await agreementService.get(23, ['test']);
  const { method, url, requestHeaders } = t.context.server.requests[0];
  t.is(method, 'POST');
  t.is(url, `${client.apiBaseUrl}/api/v2/agreements/request_sign`);
  t.is(requestHeaders.Accept, 'application/json');
  t.is(requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  t.is(requestHeaders.Authorization, 'Token token=valid_token');

  t.truthy(response);
  t.is(response.id, 23);
  t.is(response.templateId, 5);
  t.is(response.status, 'approved');
  t.is(response.template.id, 5);
  t.is(response.template.redirectUrl, 'http://www.google.com');
  t.deepEqual(response.signatures, []);
});