Manual Reference Source Test

test/unit/services.consent-strategy.js

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

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

  t.context.getResponseAll = {
    data: [
      {
        id: 13,
        type: 'consent__strategies',
        attributes: {
          name: 'Strategy-15',
          strategy_type: 'electronic',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        },
        relationships: {
          cohort: {
            data: {
              id: 19,
              type: 'cohorts'
            }
          },
          forms: {
            data: [
              {
                id: 12,
                type: 'consent__forms'
              },
              {
                id: 13,
                type: 'consent__forms'
              }
            ]
          }
        }
      },
      {
        id: 14,
        type: 'consent__strategies',
        attributes: {
          name: 'Strategy-16',
          strategy_type: 'electronic',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        },
        relationships: {
          cohort: {
            data: {
              id: 20,
              type: 'cohorts'
            }
          },
          forms: {
            data: [
              {
                id: 14,
                type: 'consent__forms'
              },
              {
                id: 15,
                type: 'consent__forms'
              },
              {
                id: 16,
                type: 'consent__forms'
              }
            ]
          }
        }
      }
    ],
    included: [
      {
        id: 19,
        type: 'cohorts',
        attributes: {
          name: 'name-19',
          cohort_type: 'static',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        }
      },
      {
        id: 12,
        type: 'consent__forms',
        attributes: {
          enabled: true,
          name: 'Form 12',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        },
        relationships: {
          consent_form_versions: {
            data: []
          }
        }
      },
      {
        id: 13,
        type: 'consent__forms',
        attributes: {
          enabled: true,
          name: 'Form 13',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        },
        relationships: {
          consent_form_versions: {
            data: []
          }
        }
      },
      {
        id: 20,
        type: 'cohorts',
        attributes: {
          name: 'name-20',
          cohort_type: 'static',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        }
      },
      {
        id: 14,
        type: 'consent__forms',
        attributes: {
          enabled: true,
          name: 'Form 14',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        },
        relationships: {
          consent_form_versions: {
            data: []
          }
        }
      },
      {
        id: 15,
        type: 'consent__forms',
        attributes: {
          enabled: true,
          name: 'Form 15',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        },
        relationships: {
          consent_form_versions: {
            data: []
          }
        }
      },
      {
        id: 16,
        type: 'consent__forms',
        attributes: {
          enabled: true,
          name: 'Form 16',
          created_at: '2018-05-09T17:43:23Z',
          updated_at: '2018-05-09T17:43:23Z'
        },
        relationships: {
          consent_form_versions: {
            data: []
          }
        }
      }
    ]
  };

  t.context.getResponseId = {
    data: {
      id: 5,
      type: 'consent__strategies',
      attributes: {
        name: 'Strategy-7',
        strategy_type: 'electronic',
        created_at: '2018-05-10T15:54:57Z',
        updated_at: '2018-05-10T15:54:57Z'
      },
      relationships: {
        cohort: {
          data: {
            id: 12,
            type: 'cohorts'
          }
        },
        forms: {
          data: []
        }
      }
    },
    included: [
      {
        id: 12,
        type: 'cohorts',
        attributes: {
          name: 'name-12',
          cohort_type: 'static',
          created_at: '2018-05-10T15:54:57Z',
          updated_at: '2018-05-10T15:54:57Z'
        }
      }
    ]
  };

  t.context.patchResponse = {
    data: {
      id: 6,
      type: 'consent__strategies',
      attributes: {
        name: 'A new form name',
        strategy_type: 'electronic',
        created_at: '2018-05-08T16:24:07Z',
        updated_at: '2018-05-08T16:24:07Z'
      },
      relationships: {
        cohort: {
          data: {
            id: 10,
            type: 'cohorts'
          }
        },
        forms: {
          data: []
        }
      }
    },
    included: [
      {
        id: 10,
        type: 'cohorts',
        attributes: {
          name: 'name-10',
          cohort_type: 'static',
          created_at: '2018-05-08T16:24:07Z',
          updated_at: '2018-05-08T16:24:07Z'
        }
      }
    ]
  };

  t.context.consentStrategyJsonApi = {
    data: {
      type: 'consent__strategies',
      attributes: {
        name: 'A form name',
        strategy_type: 'paper'
      },
      relationships: {
        cohort: {
          data: {
            id: 21,
            type: 'cohorts'
          }
        }
      }
    }
  };
});

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.consentStrategy = new ConsentStrategy(t.context.consentStrategyJsonApi);
});

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

/**
* @test {Clinical6.get}
*/
test('[unit] ConsentStrategyService.get should make a properly formatted get request', async (t) => {
  const { getResponseAll } = t.context;
  let request = {};
  nock(client.apiBaseUrl).get(`/v3/consent/strategies`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseAll];
  });
  await clinical6.get(ConsentStrategy);
  t.is(request.path, `/v3/consent/strategies`);
  t.is(request.headers.accept, 'application/json');
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');
});

/**
* @test {Clinical6.get}
*/
test('[unit] ConsentStrategyService.get should receive a valid response for a get request without an id', async (t) => {
  const { getResponseAll } = t.context;
  nock(client.apiBaseUrl).get(`/v3/consent/strategies`).reply(200, getResponseAll);
  const response = await clinical6.get(ConsentStrategy);
  t.truthy(response);
  t.is(Object.keys(response).length, 2);

  t.is(response[0].id, 13);
  t.is(response[0].type, 'consent__strategies');
  t.is(response[0].name, 'Strategy-15');
  t.is(response[0].strategyType, 'electronic');
  t.is(response[0].createdAt, '2018-05-09T17:43:23Z');
  t.is(response[0].updatedAt, '2018-05-09T17:43:23Z');
  t.is(response[0].cohort.id, 19);
  t.is(response[0].cohort.type, 'cohorts');
  t.is(response[0].cohort.name, 'name-19');
  t.is(response[0].cohort.cohortType, 'static');
  t.is(response[0].cohort.createdAt, '2018-05-09T17:43:23Z');
  t.is(response[0].cohort.updatedAt, '2018-05-09T17:43:23Z');

  t.is(response[0].forms.length, 2);
  t.is(response[0].forms[0].id, 12);
  t.is(response[0].forms[0].type, 'consent__forms');
  t.is(response[0].forms[0].enabled, true);
  t.is(response[0].forms[0].name, 'Form 12');
  t.is(response[0].forms[0].createdAt, '2018-05-09T17:43:23Z');
  t.is(response[0].forms[0].updatedAt, '2018-05-09T17:43:23Z');
  t.is(response[0].forms[0].consentFormVersions.length, 0);

  t.is(response[0].forms[1].id, 13);
  t.is(response[0].forms[1].type, 'consent__forms');
  t.is(response[0].forms[1].enabled, true);
  t.is(response[0].forms[1].name, 'Form 13');
  t.is(response[0].forms[1].createdAt, '2018-05-09T17:43:23Z');
  t.is(response[0].forms[1].updatedAt, '2018-05-09T17:43:23Z');
  t.is(response[0].forms[1].consentFormVersions.length, 0);

  t.is(response[1].id, 14);
  t.is(response[1].type, 'consent__strategies');
  t.is(response[1].name, 'Strategy-16');
  t.is(response[1].strategyType, 'electronic');
  t.is(response[1].createdAt, '2018-05-09T17:43:23Z');
  t.is(response[1].updatedAt, '2018-05-09T17:43:23Z');
  t.is(response[1].cohort.id, 20);
  t.is(response[1].cohort.type, 'cohorts');
  t.is(response[1].cohort.name, 'name-20');
  t.is(response[1].cohort.cohortType, 'static');
  t.is(response[1].cohort.createdAt, '2018-05-09T17:43:23Z');
  t.is(response[1].cohort.updatedAt, '2018-05-09T17:43:23Z');

  t.is(response[1].forms.length, 3);
  t.is(response[1].forms[0].id, 14);
  t.is(response[1].forms[0].type, 'consent__forms');
  t.is(response[1].forms[0].enabled, true);
  t.is(response[1].forms[0].name, 'Form 14');
  t.is(response[1].forms[0].createdAt, '2018-05-09T17:43:23Z');
  t.is(response[1].forms[0].updatedAt, '2018-05-09T17:43:23Z');
  t.is(response[1].forms[0].consentFormVersions.length, 0);

  t.is(response[1].forms[1].id, 15);
  t.is(response[1].forms[1].type, 'consent__forms');
  t.is(response[1].forms[1].enabled, true);
  t.is(response[1].forms[1].name, 'Form 15');
  t.is(response[1].forms[1].createdAt, '2018-05-09T17:43:23Z');
  t.is(response[1].forms[1].updatedAt, '2018-05-09T17:43:23Z');
  t.is(response[1].forms[1].consentFormVersions.length, 0);

  t.is(response[1].forms[2].id, 16);
  t.is(response[1].forms[2].type, 'consent__forms');
  t.is(response[1].forms[2].enabled, true);
  t.is(response[1].forms[2].name, 'Form 16');
  t.is(response[1].forms[2].createdAt, '2018-05-09T17:43:23Z');
  t.is(response[1].forms[2].updatedAt, '2018-05-09T17:43:23Z');
  t.is(response[1].forms[2].consentFormVersions.length, 0);
});

/**
 * @test {Clinical6.get}
 */
test('[unit] ConsentStrategyService.get should receive a valid response for a get request with an id', async (t) => {
  const { getResponseId } = t.context;
  nock(client.apiBaseUrl).get('/v3/consent/strategies/5').reply(200, getResponseId);
  const response = await clinical6.get(new ConsentStrategy({ id: 5 }));
  t.truthy(response);
  t.is(response.id, 5);
  t.is(response.type, 'consent__strategies');
  t.is(response.name, 'Strategy-7');
  t.is(response.createdAt, '2018-05-10T15:54:57Z');
  t.is(response.updatedAt, '2018-05-10T15:54:57Z');
  t.is(response.cohort.id, 12);
  t.is(response.cohort.type, 'cohorts');
  t.is(response.cohort.name, 'name-12');
  t.is(response.cohort.cohortType, 'static');
  t.is(response.cohort.createdAt, '2018-05-10T15:54:57Z');
  t.is(response.cohort.updatedAt, '2018-05-10T15:54:57Z');
  t.is(response.forms.length, 0);
});

// ConsentStrategyService.delete strategy
/**
 * @test {Clinical6.delete}
 */
test('[unit] ConsentStrategyService.delete should throw errors for invalid parameters', async (t) => {
  const title = `ConsentStrategyService.delete error`;
  await t.throwsAsync(clinical6.delete(new ConsentStrategy()), `${title}: strategy does not have id`);
});

/**
 * @test {Clinical6.delete}
 */
test('[unit] ConsentStrategyService.delete should receive a valid response for a delete request', async (t) => {
  const { consentStrategy } = t.context;
  consentStrategy.id = 5;
  let request = {};
  nock(client.apiBaseUrl).delete(`/v3/consent/strategies/${consentStrategy.id}`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [204, ''];
  });
  const response = await clinical6.delete(consentStrategy);
  t.is(request.path, `/v3/consent/strategies/${consentStrategy.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] ConsentStrategyService.delete should remove the element from local storage', async (t) => {
  const { consentStrategy, storage } = t.context;
  consentStrategy.id = 5;
  nock(client.apiBaseUrl).delete(`/v3/consent/strategies/${consentStrategy.id}`).reply(204, '');
  await storage.set(consentStrategy.type, consentStrategy.toJSON(), { id: consentStrategy.id });
  await clinical6.delete(consentStrategy);
  t.is(storage.has(consentStrategy.type, { id: consentStrategy.id }), false);
});

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

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

test('[unit] ConsentStrategyService.insert should successfully insert a consentStrategy with a consentStrategy object', async (t) => {
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/consent/strategies`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [201, {
      data: {
        id: 8,
        type: 'consent__strategies',
        attributes: {
          name: 'A form name',
          strategy_type: 'paper',
          created_at: '2018-05-06T04:21:43Z',
          updated_at: '2018-05-06T04:21:43Z'
        },
        relationships: {
          cohort: {
            data: {
              id: 21,
              type: 'cohorts'
            }
          },
          forms: {
            data: []
          }
        }
      },
      included: [
        {
          id: 21,
          type: 'cohorts',
          attributes: {
            name: 'name-24',
            cohort_type: 'static',
            created_at: '2018-05-06T04:21:43Z',
            updated_at: '2018-05-06T04:21:43Z'
          }
        }
      ]
    }];
  });

  const requestJsonApi = {
    data: {
      type: 'consent__strategies',
      attributes: {
        name: 'A form name',
        strategy_type: 'paper'
      },
      relationships: {
        cohort: {
          data: {
            id: 21,
            type: 'cohorts'
          }
        }
      }
    }
  };

  const consentStrategy = new ConsentStrategy(requestJsonApi);
  const response = await clinical6.insert(consentStrategy);

  t.is(request.path, `/v3/consent/strategies`);
  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, 8);
  t.is(response.type, 'consent__strategies');
  t.is(response.name, 'A form name');
  t.is(response.strategyType, 'paper');
  t.is(response.createdAt, '2018-05-06T04:21:43Z');
  t.is(response.updatedAt, '2018-05-06T04:21:43Z');
  t.is(response.cohort.id, 21);
  t.is(response.cohort.type, 'cohorts');
  t.is(response.cohort.name, 'name-24');
  t.is(response.cohort.cohortType, 'static');
  t.is(response.cohort.createdAt, '2018-05-06T04:21:43Z');
  t.is(response.cohort.updatedAt, '2018-05-06T04:21:43Z');
  t.is(response.forms.length, 0);
});

// ConsentStrategyService.update strategy
/**
 * @test {Clinical6.update}
 */
test('[unit] ConsentStrategyService.update should successfully update a consentStrategy with a consentStrategy object', async (t) => {
  const { patchResponse } = t.context;
  const consentStrategyPatchJsonApi = {
    data: {
      id: 6,
      type: 'consent__strategies',
      attributes: {
        name: 'A new form name'
      },
    }
  };
  const consentStrategy = new ConsentStrategy(consentStrategyPatchJsonApi);
  let request = {};
  client.reset();
  client.authToken = 'valid_token';
  nock(client.apiBaseUrl).patch(`/v3/consent/strategies/${consentStrategy.id}`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, patchResponse];
  });
  const response = await clinical6.update(consentStrategy);

  t.is(request.path, `/v3/consent/strategies/${consentStrategy.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.deepEqual(request.requestBody, consentStrategyPatchJsonApi);

  t.is(response.id, 6);
  t.is(response.type, 'consent__strategies');
  t.is(response.name, 'A new form name');
  t.is(response.strategyType, 'electronic');
  t.is(response.createdAt, '2018-05-08T16:24:07Z');
  t.is(response.updatedAt, '2018-05-08T16:24:07Z');
  t.is(response.cohort.id, 10);
  t.is(response.cohort.type, 'cohorts');
  t.is(response.cohort.name, 'name-10');
  t.is(response.cohort.cohortType, 'static');
  t.is(response.cohort.createdAt, '2018-05-08T16:24:07Z');
  t.is(response.cohort.updatedAt, '2018-05-08T16:24:07Z');
  t.is(response.forms.length, 0);
});