Manual Reference Source Test

test/unit/services.approver-group.js

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

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

  t.context.getResponseAll = {
    data: [
      {
        id: 17,
        type: 'consent__approver_groups',
        attributes: {
          name: 'Approver Group 17',
          created_at: '2018-05-08T21:12:24Z',
          updated_at: '2018-05-08T21:12:24Z'
        },
        relationships: {
          approver_assignments: {
            data: [
              {
                id: 19,
                type: 'consent__approver_assignments'
              },
              {
                id: 20,
                type: 'consent__approver_assignments'
              }
            ]
          },
          group_assignments: {
            data: [
              {
                id: 9,
                type: 'consent__approver_group_assignments'
              }
            ]
          },
          approvers: {
            data: [
              {
                id: 31,
                type: 'consent__approvers'
              },
              {
                id: 32,
                type: 'consent__approvers'
              }
            ]
          }
        }
      },
      {
        id: 18,
        type: 'consent__approver_groups',
        attributes: {
          name: 'Approver Group 18',
          created_at: '2018-05-08T21:12:25Z',
          updated_at: '2018-05-08T21:12:25Z'
        },
        relationships: {
          approver_assignments: {
            data: [
              {
                id: 21,
                type: 'consent__approver_assignments'
              },
              {
                id: 22,
                type: 'consent__approver_assignments'
              }
            ]
          },
          group_assignments: {
            data: [
              {
                id: 10,
                type: 'consent__approver_group_assignments'
              }
            ]
          },
          approvers: {
            data: [
              {
                id: 33,
                type: 'consent__approvers'
              },
              {
                id: 34,
                type: 'consent__approvers'
              }
            ]
          }
        }
      },
      {
        id: 19,
        type: 'consent__approver_groups',
        attributes: {
          name: 'Approver Group 19',
          created_at: '2018-05-08T21:12:25Z',
          updated_at: '2018-05-08T21:12:25Z'
        },
        relationships: {
          approver_assignments: {
            data: [
              {
                id: 23,
                type: 'consent__approver_assignments'
              },
              {
                id: 24,
                type: 'consent__approver_assignments'
              }
            ]
          },
          group_assignments: {
            data: [
              {
                id: 11,
                type: 'consent__approver_group_assignments'
              }
            ]
          },
          approvers: {
            data: [
              {
                id: 35,
                type: 'consent__approvers'
              },
              {
                id: 36,
                type: 'consent__approvers'
              }
            ]
          }
        }
      },
      {
        id: 20,
        type: 'consent__approver_groups',
        attributes: {
          name: 'Approver Group 20',
          created_at: '2018-05-08T21:12:25Z',
          updated_at: '2018-05-08T21:12:25Z'
        },
        relationships: {
          approver_assignments: {
            data: [
              {
                id: 25,
                type: 'consent__approver_assignments'
              },
              {
                id: 26,
                type: 'consent__approver_assignments'
              }
            ]
          },
          group_assignments: {
            data: [
              {
                id: 12,
                type: 'consent__approver_group_assignments'
              }
            ]
          },
          approvers: {
            data: [
              {
                id: 37,
                type: 'consent__approvers'
              },
              {
                id: 38,
                type: 'consent__approvers'
              }
            ]
          }
        }
      },
      {
        id: 21,
        type: 'consent__approver_groups',
        attributes: {
          name: 'Approver Group 21',
          created_at: '2018-05-08T21:12:25Z',
          updated_at: '2018-05-08T21:12:25Z'
        },
        relationships: {
          approver_assignments: {
            data: [
              {
                id: 27,
                type: 'consent__approver_assignments'
              },
              {
                id: 28,
                type: 'consent__approver_assignments'
              }
            ]
          },
          group_assignments: {
            data: [
              {
                id: 13,
                type: 'consent__approver_group_assignments'
              }
            ]
          },
          approvers: {
            data: [
              {
                id: 39,
                type: 'consent__approvers'
              },
              {
                id: 40,
                type: 'consent__approvers'
              }
            ]
          }
        }
      }
    ]
  };

  t.context.getResponseId = {
    data: {
      id: 23,
      type: 'consent__approver_groups',
      attributes: {
        name: 'Approver Group 26',
        created_at: '2018-05-08T21:12:25Z',
        updated_at: '2018-05-08T21:12:25Z'
      },
      relationships: {
        approver_assignments: {
          data: [
            {
              id: 29,
              type: 'consent__approver_assignments'
            },
            {
              id: 30,
              type: 'consent__approver_assignments'
            }
          ]
        },
        group_assignments: {
          data: [
            {
              id: 14,
              type: 'consent__approver_group_assignments'
            }
          ]
        },
        approvers: {
          data: [
            {
              id: 41,
              type: 'consent__approvers'
            },
            {
              id: 42,
              type: 'consent__approvers'
            }
          ]
        }
      }
    }
  };

  t.context.approverGroupJsonApi = {
    data: {
      type: 'consent__approver_groups',
      attributes: {
        name: 'A group name'
      }
    }
  };

  t.context.patchResponse = {
    data: {
      id: 19,
      type: 'consent__approver_groups',
      attributes: {
        name: 'A group name',
        created_at: '2018-05-04T16:44:32Z',
        updated_at: '2018-05-04T16:44:32Z'
      },
      relationships: {
        approver_assignments: {
          data: []
        },
        group_assignments: {
          data: []
        },
        approvers: {
          data: []
        },
        form_versions: {
          data: []
        }
      }
    }
  };
});

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.storage = client.storageUtility;
  t.context.approverGroup = new ApproverGroup(t.context.approverGroupJsonApi);
});

// ApproverGroupService.get method
/**
 * @test {ApproverGroupService.get}
 */
test('[unit] ApproverGroupService.get should exist', (t) => {
  t.truthy(clinical6.get(ApproverGroup));
});

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

/**
 * @test {ApproverGroupService.get}
 */
test('[unit] ApproverGroupService.get should make a properly formatted get request', async (t) => {
  const response = await clinical6.get(ApproverGroup);
  t.truthy(response);
});

/**
 * @test {Clinical6.get}
 */
test('[unit] ApproverGroupService.get should make a properly formatted get request and response without an id', async (t) => {
  const { getResponseAll } = t.context;
  let request = {};
  nock(client.apiBaseUrl).get('/v3/consent/approver_groups').reply(function () {
    request = this.req;
    return [200, getResponseAll];
  });
  const response = await clinical6.get(ApproverGroup);
  t.is(request.path, `/v3/consent/approver_groups`);
  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, 5);
  t.truthy(response[0] instanceof ApproverGroup);
});

/**
 * @test {Clinical6.get}
 */
test.serial('[unit ApproverGroupService.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/approver_groups/23').reply(200, getResponseId);
  const response = await clinical6.get(new ApproverGroup({ id: 23 }));
  t.truthy(response);
  t.is(response.id, 23);
  t.is(response.type, 'consent__approver_groups');
  t.is(response.name, 'Approver Group 26');
  t.is(response.createdAt, '2018-05-08T21:12:25Z');
  t.is(response.updatedAt, '2018-05-08T21:12:25Z');
  t.is(response.approverAssignments[0].id, 29);
  t.is(response.approverAssignments[0].type, 'consent__approver_assignments');
  t.is(response.approverAssignments.length, 2);
});

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

/**
 * @test {Clinical6.insert}
 */
test('[unit] ApproverGroupService.insert should successfully insert a approverGroup with a approverGroup object', async (t) => {
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/consent/approver_groups`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [201, {
      data: {
        id: 3,
        type: 'consent__approver_groups',
        attributes: {
          name: 'A group name'
        },
        relationships: {
          approver_assignments: {
            data: []
          },
          approvers: {
            data: []
          }
        }
      }
    }];
  });

  const requestJsonApi = {
    data: {
      type: 'consent__approver_groups',
      attributes: {
        name: 'A group name'
      },
      relationships: {
        approver_assignments: {
          data: []
        },
        approvers: {
          data: []
        },
        form_versions: {
          data: []
        }
      }
    }
  };

  const approverGroup = new ApproverGroup(requestJsonApi);
  const response = await clinical6.insert(approverGroup);

  t.is(request.path, `/v3/consent/approver_groups`);
  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, 3);
  t.is(response.type, 'consent__approver_groups');
  t.is(response.name, 'A group name');
  t.is(response.approvers.length, 0);
  t.is(response.approverAssignments.length, 0);
});

// ApproverGroupService.update method
/**
 * @test {Clinical6.update}
 */
test('[unit] ApproverGroupService.update should successfully update an approverGroup with an approverGroup object', async (t) => {
  const { patchResponse } = t.context;
  const updateAproverGroupJsonApi = {
    id: 19,
    type: 'consent__approver_groups',
    attributes: {
      name: 'A group name',
      created_at: '2018-05-04T16:44:32Z',
      updated_at: '2018-05-04T16:44:32Z'
    },
    relationships: {
      approver_assignments: {
        data: [],
      },
      approvers: {
        data: [],
      },
      form_versions: {
        data: []
      }
    }
  };
  const approverGroup = new ApproverGroup({ data: updateAproverGroupJsonApi });

  let request = {};
  nock(client.apiBaseUrl).patch(`/v3/consent/approver_groups/${approverGroup.id}`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, patchResponse];
  });
  const response = await clinical6.update(approverGroup);

  t.is(request.path, `/v3/consent/approver_groups/${approverGroup.id}`);
  t.is(request.headers.accept, 'application/json');
  t.deepEqual(request.requestBody, { data: updateAproverGroupJsonApi });
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');
  t.is(response.id, 19);
  t.is(response.type, 'consent__approver_groups');
  t.is(response.createdAt, '2018-05-04T16:44:32Z');
  t.is(response.updatedAt, '2018-05-04T16:44:32Z');
  t.is(response.name, 'A group name');
  t.is(response.approverAssignments.length, 0);
  t.is(response.approvers.length, 0);
  t.is(response.formVersions.length, 0);
});