Manual Reference Source Test

test/unit/services.status.section.js

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

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

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.server = sinon.fakeServer.create();
  t.context.server.autoRespond = true;

  t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/status/check_section`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      section_status: {
        value: 'initial',
        sub_statuses: [
          {
            object: 'site_information',
            type: 'section',
            value: 'initial'
          },
          {
            object: 'site_staff',
            type: 'custom',
            value: 'initial'
          },
          {
            object: 'lab_documents',
            type: 'section',
            value: 'initial'
          }
        ]
      }
    })]);
});

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

// StatusService.getSections
/**
 * @test {StatusService.getSections}
 */
test.serial('[unit] StatusService.getSections should throw an error if client authToken is not set', (t) => {
  client.authToken = undefined;
  t.throws(() => statusService.getSections(), 'StatusService.getSections error: requires authToken and'
  + ' resource is not defined and ownerType is not defined and owner is not defined');
});

/**
 * @test {StatusService.getSections}
 */
test('[unit] StatusService.getSections should throw an error when a no parameters are provided', (t) => {
  client.authToken = 'valid_token';
  const expectedError = 'StatusService.getSections error: resource is not defined and'
  + ' ownerType is not defined and owner is not defined';
  t.throws(() => statusService.getSections(), expectedError);
});

/**
 * @test {StatusService.getSections}
 */
test('[unit] StatusService.getSections should throw an error when a ownerType and owner are not provided', (t) => {
  const expectedError = 'StatusService.getSections error: ownerType is not defined and owner is not defined';
  t.throws(() => statusService.getSections('site_profile_cards'), expectedError);
});

/**
 * @test {StatusService.getSections}
 */
test('[unit] StatusService.getSections should throw an error when the owner is not provided', (t) => {
  const expectedError = 'StatusService.getSections error: owner is not defined';

  t.throws(() => statusService.getSections('site_profile_cards', 'site_id'), expectedError);
});

/**
 * @test {StatusService.getSections}
 */
test('[unit] StatusService.getSections should not throw an error when the owner is not provided but the ownerType is "mobile_user"', async (t) => {
  t.truthy(await statusService.getSections('site_profile_cards', 'mobile_user').then);
});

// /**
//  * @test {StatusService.getSections}
//  */
// test('[unit] StatusService.getSections should make a properly formatted POST request', async (t) => {
//   await statusService.getSections('site_profile_cards', 'site_id', 'MobileUser');
//   const request = t.context.server.requests[0];
//   t.is(request.method, 'POST');
//   t.is(request.url, `https://somesite.Clinical6.com/api/status/check_section`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({
//     status: {
//       owner_type: 'site_id',
//       section: 'site_profile_cards',
//       owner: 'MobileUser',
//     },
//   }));
// });

/**
 * @test {StatusService.getSections}
 */
test('[unit] StatusService.getSections should successfully return all status values', async (t) => {
  const response = await statusService.getSections('site_profile_cards', 'site_id', 'MobileUser');
  // const request = t.context.server.requests[0];

  // t.is(request.method, 'POST');
  // t.is(request.url, `https://somesite.Clinical6.com/api/status/check_section`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  // t.is(request.requestBody, JSON.stringify({
  //   status: {
  //     owner_type: 'site_id',
  //     section: 'site_profile_cards',
  //     owner: 'MobileUser',
  //   },
  // }));
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');

  t.is(response.status, 'initial');
  t.is(response.sub_statuses.length, 3);
  t.is(response.sub_statuses[0].object, 'site_information');
  t.is(response.sub_statuses[0].type, 'section');
  t.is(response.sub_statuses[0].value, 'initial');
  t.is(response.sub_statuses[1].object, 'site_staff');
  t.is(response.sub_statuses[1].type, 'custom');
  t.is(response.sub_statuses[1].value, 'initial');
  t.is(response.sub_statuses[2].object, 'lab_documents');
  t.is(response.sub_statuses[2].type, 'section');
  t.is(response.sub_statuses[2].value, 'initial');


  // t.is(response.section_status.value, 'initial');
  // t.is(response.section_status.sub_statuses.length, 3);
  // t.is(response.section_status.sub_statuses[0].object, 'site_information');
  // t.is(response.section_status.sub_statuses[0].type, 'section');
  // t.is(response.section_status.sub_statuses[0].value, 'initial');

  // t.is(response.section_status.sub_statuses[1].object, 'site_staff');
  // t.is(response.section_status.sub_statuses[1].type, 'custom');
  // t.is(response.section_status.sub_statuses[1].value, 'initial');

  // t.is(response.section_status.sub_statuses[2].object, 'lab_documents');
  // t.is(response.section_status.sub_statuses[2].type, 'section');
  // t.is(response.section_status.sub_statuses[2].value, 'initial');
});