Manual Reference Source Test

test/unit/services.content-type.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  clinical6,
  Content,
  ContentAttribute,
  ContentType
} from '../../src';

test.before('start server', (t) => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
  const types = [
    new ContentType({
      id: '76',
      type: 'dynamic_content__content_types',
      attributes: {
        name: 'dummy_407',
        permanent_link: 'home_health_visit',
        description: null,
        created_at: '2017-07-28T15:30:39Z',
        updated_at: '2017-07-28T15:30:39Z'
      }
    }),
    new ContentType({
      id: '182',
      type: 'dynamic_content__content_types',
      attributes: {
        name: 'Mayor and Council',
        permanent_link: 'mayor_and_council',
        description: null,
        created_at: '2017-07-28T15:30:40Z',
        updated_at: '2017-07-28T15:30:40Z'
      }
    }),
    new ContentType({
      id: '183',
      type: 'dynamic_content__content_types',
      attributes: {
        name: 'Mayor and Council',
        permanent_link: 'acronyms',
        description: null,
        created_at: '2017-07-28T15:30:40Z',
        updated_at: '2017-07-28T15:30:40Z'
      }
    })
  ];
  types.forEach(type => type.store());

  t.context.getResponseTypesId = {
    data: {
      id: '76',
      type: 'dynamic_content__content_types',
      attributes: {
        name: 'dummy_407',
        permanent_link: 'home_health_visit',
        description: null,
        created_at: '2017-07-28T15:30:39Z',
        updated_at: '2017-07-28T15:30:39Z'
      }
    },
  };

  t.context.getResponseTypesAll = {
    data: [{
      id: 76,
      type: 'dynamic_content__content_types',
      attributes: {
        name: 'dummy_407',
        description: null,
        permanent_link: 'home_health_visit',
        content_type: 'acronyms',
        created_at: '2017-07-28T15:30:39Z',
        updated_at: '2017-07-28T15:30:39Z'
      }
    },
    {
      id: '182',
      type: 'dynamic_content__content_types',
      attributes: {
        name: 'Mayor and Council',
        permanent_link: 'mayor_and_council',
        description: null,
        created_at: '2017-07-28T15:30:40Z',
        updated_at: '2017-07-28T15:30:40Z'
      }
    },
    {
      id: '183',
      type: 'dynamic_content__content_types',
      attributes: {
        name: 'Mayor and Council',
        permanent_link: 'acronyms',
        description: null,
        created_at: '2017-07-28T15:30:40Z',
        updated_at: '2017-07-28T15:30:40Z'
      }
    }
    ]
  };

  t.context.getResponseAttributesAll = {
    data: [
      {
        id: '31',
        type: 'dynamic_content__attribute_keys',
        attributes: {
          name: 'dummy_1264',
          attribute_type: 'string',
          required: false,
          position: 1,
          display_name: 'dummy_1265',
          default_value: 'dummy_1266',
          show_on_index: true,
          created_at: '2018-04-13T21:50:31Z',
          updated_at: '2018-04-13T21:50:31Z'
        },
        relationships: {
          content_type: {
            data: {
              id: '32',
              type: 'dynamic_content__content_types'
            }
          }
        }
      },
      {
        id: '32',
        type: 'dynamic_content__attribute_keys',
        attributes: {
          name: 'dummy_1267',
          attribute_type: 'string',
          required: false,
          position: 2,
          display_name: 'dummy_1268',
          default_value: 'dummy_1269',
          show_on_index: true,
          created_at: '2018-04-13T21:50:31Z',
          updated_at: '2018-04-13T21:50:31Z'
        },
        relationships: {
          content_type: {
            data: {
              id: '32',
              type: 'dynamic_content__content_types'
            }
          }
        }
      }
    ]
  };
});

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

test.beforeEach((t) => {
  client.authToken = 'valid_token';
  client.cache = 'never';

  t.context.storage = client.storageUtility;
  t.context.contentJsonApi = {
    data: {
      id: 755256,
      type: 'dynamic_content__contents',
      attributes: {
        created_at: '2016-05-12T22:06:38Z',
        description: 'Workplace as a Service',
        position: 2,
        title: 'WPaaS',
        tags: [],
        content_type: 'acronyms'
      },
      relationships: {
        content_type: {
          data: {
            id: '183',
            type: 'dynamic_content__content_types'
          }
        }
      }
    }
  };
  t.context.content = new Content(t.context.contentJsonApi);
});


// Clinical6.get method
/**
 * @test {Clinical6.get}
 */
test.serial('[unit] Clinical6.get(ContentType) should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const err = `ContentTypeService.get error`;
  await t.throwsAsync(clinical6.get(ContentType), `${err}: requires authToken`);
});

/**
 * @test {Clinical6.get}
 */
test('[unit] Clinical6.get(ContentType) should make a properly formatted GET request and response', async (t) => {
  const { getResponseTypesAll } = t.context;
  let request = {};
  nock(client.apiBaseUrl).get(`/v3/dynamic_content/content_types`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseTypesAll];
  });
  const response = await clinical6.get(ContentType);
  t.is(request.path, `/v3/dynamic_content/content_types`);
  t.is(request.headers.accept, 'application/json');
  t.is(request.headers['content-type'], 'application/json');

  t.truthy(response);
  t.is(response.length, 3);
  t.is(response[0].id, 76);
  t.is(response[0].type, 'dynamic_content__content_types');
  t.is(response[0].name, 'dummy_407');
  t.is(response[0].permanentLink, 'home_health_visit');
  t.is(response[0].description, null);
  t.is(response[0].createdAt, '2017-07-28T15:30:39Z');
  t.is(response[0].updatedAt, '2017-07-28T15:30:39Z');
  t.is(response[1].id, 182);
  t.is(response[1].type, 'dynamic_content__content_types');
  t.is(response[1].name, 'Mayor and Council');
  t.is(response[1].permanentLink, 'mayor_and_council');
  t.is(response[1].description, null);
  t.is(response[1].createdAt, '2017-07-28T15:30:40Z');
  t.is(response[1].updatedAt, '2017-07-28T15:30:40Z');
  t.is(response[2].id, 183);
  t.is(response[2].type, 'dynamic_content__content_types');
  t.is(response[2].name, 'Mayor and Council');
  t.is(response[2].permanentLink, 'acronyms');
  t.is(response[2].description, null);
  t.is(response[2].createdAt, '2017-07-28T15:30:40Z');
  t.is(response[2].updatedAt, '2017-07-28T15:30:40Z');
});


/**
 * @test {Clinical6.get}
 */
test('[unit] Clinical6.get(ContentType) should receive a valid response for a get request', async (t) => {
  const { getResponseTypesId } = t.context;
  nock(client.apiBaseUrl).get(`/v3/dynamic_content/content_types`).reply(200, getResponseTypesId);

  const response = await clinical6.get(new ContentType({ id: 76 }));
  t.truthy(response);
  t.is(response.id, 76);
  t.is(response.type, 'dynamic_content__content_types');
  t.is(response.name, 'dummy_407');
  t.is(response.permanentLink, 'home_health_visit');
  t.is(response.description, null);
  t.is(response.createdAt, '2017-07-28T15:30:39Z');
  t.is(response.updatedAt, '2017-07-28T15:30:39Z');
});

// Clinical6.getChildren method
/**
 * @test {Clinical6.getChildren}
 */
test.serial('[unit] Clinical6.getChildren(ContentType) should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const contentType = new ContentType({ id: 31 });
  const err = `ContentTypeService.getChildren error`;
  await t.throwsAsync(clinical6.getChildren(contentType, ContentAttribute), `${err}: requires authToken`);
});

/**
 * @test {Clinical6.getChildren}
 */
test('[unit] Clinical6.getChildren(ContentType) should make a properly formatted GET request and response', async (t) => {
  const { getResponseAttributesAll } = t.context;
  const contentType = new ContentType({ id: 31 });
  let request = {};
  nock(client.apiBaseUrl).get(`/v3/dynamic_content/content_types/${contentType.id}/dynamic_content/attribute_keys`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseAttributesAll];
  });
  const response = await clinical6.getChildren(contentType, ContentAttribute);
  t.is(request.path, `/v3/dynamic_content/content_types/${contentType.id}/dynamic_content/attribute_keys`);
  t.is(request.headers.accept, 'application/json');
  t.is(request.headers['content-type'], 'application/json');

  t.truthy(response);
  t.is(response.length, 2);
  t.is(response[0].id, 31);
  t.is(response[0].type, 'dynamic_content__attribute_keys');
  t.is(response[0].name, 'dummy_1264');
  t.is(response[0].attributeType, 'string');
  t.is(response[0].required, false);
  t.is(response[0].displayName, 'dummy_1265');
  t.is(response[0].defaultValue, 'dummy_1266');
  t.is(response[0].position, 1);
  t.is(response[0].showOnIndex, true);
  t.is(response[0].createdAt, '2018-04-13T21:50:31Z');
  t.is(response[0].updatedAt, '2018-04-13T21:50:31Z');
  t.is(response[1].id, 32);
  t.is(response[1].type, 'dynamic_content__attribute_keys');
  t.is(response[1].name, 'dummy_1267');
  t.is(response[1].attributeType, 'string');
  t.is(response[1].required, false);
  t.is(response[1].displayName, 'dummy_1268');
  t.is(response[1].defaultValue, 'dummy_1269');
  t.is(response[1].position, 2);
  t.is(response[1].showOnIndex, true);
  t.is(response[1].createdAt, '2018-04-13T21:50:31Z');
  t.is(response[1].updatedAt, '2018-04-13T21:50:31Z');
});