Manual Reference Source Test

test/unit/helpers.content-type.js

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

// See tests.js for testing client creation

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

  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'
            }
          }
        }
      }
    ]
  };

  t.context.getResponseAttributesId = {
    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'
          }
        }
      }
    }
  };
});

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

  client.storageUtility.clear();
  t.context.storage = client.storageUtility;

  t.context.contentTypeJsonApi = {
    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.contentTypeJson = {
    id: 76,
    type: 'dynamic_content__content_types',
    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.contentTypeJsonApiRaw = {
    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.contentType = new ContentType(t.context.contentTypeJsonApiRaw);
});

/**
 * @test {ContentType}
 */
test('[unit] ContentType should handle site data with a normal json format', (t) => {
  const { contentTypeJson } = t.context;
  const contentType = new ContentType(contentTypeJson);
  t.is(contentType.id, 76);
  t.is(contentType.type, 'dynamic_content__content_types');
  t.is(contentType.name, 'dummy_407');
  t.is(contentType.permanentLink, 'home_health_visit');
  t.is(contentType.description, null);
  t.is(contentType.createdAt, '2017-07-28T15:30:39Z');
  t.is(contentType.updatedAt, '2017-07-28T15:30:39Z');
});

/**
 * @test {ContentType}
 */
test('[unit] ContentType should handle site data with json api format', (t) => {
  const { contentTypeJsonApi } = t.context;
  const contentType = new ContentType({ data: contentTypeJsonApi });
  t.is(contentType.id, 76);
  t.is(contentType.type, 'dynamic_content__content_types');
  t.is(contentType.name, 'dummy_407');
  t.is(contentType.permanentLink, 'home_health_visit');
  t.is(contentType.description, null);
  t.is(contentType.createdAt, '2017-07-28T15:30:39Z');
  t.is(contentType.updatedAt, '2017-07-28T15:30:39Z');
});

/**
 * @test {ContentType}
 */
test('[unit] ContentType should generate json api format when converted to string', (t) => {
  const { contentTypeJsonApi } = t.context;
  let contentType = new ContentType({ data: contentTypeJsonApi });
  t.deepEqual(contentType.toJSON(), contentTypeJsonApi);

  contentType = new ContentType({ data: contentTypeJsonApi });
  t.deepEqual(contentType.toJSON(), contentTypeJsonApi);
});

// /**
//  * @test {ContentType.delete}
//  */
// test('[unit] ContentType.delete should successfully delete a site', async (t) => {
//   const { contentTypeJsonApi } = t.context;
//   const contentType = new ContentType({ data: contentTypeJsonApi });
//   let request = {};
//   nock(client.apiBaseUrl).delete(`/v3/trials/sites/${contentType.id}`).reply(function (uri, requestBody) {
//     request = this.req;
//     request.requestBody = requestBody;
//     return [200, ''];
//   });
//   const response = await contentType.delete();

//   t.is(request.path, `/v3/trials/sites/${contentType.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);
// });


// ContentType.getAttributes method
/**
 * @test {ContentType.getAttributes}
 */
test.serial('[unit] ContentType.getAttributes should throw an error when there is no authToken or invalid params', async (t) => {
  const { contentTypeJsonApi } = t.context;
  const contentType = new ContentType({ data: contentTypeJsonApi });
  client.authToken = undefined;
  const title = `ContentTypeService.getChildren error`;

  await contentType.getAttributes(contentType, { type: 'dynamic_content__attribute_keys' })
    .then(() => t.fail())
    .catch(e => t.is(e.message, `${title}: requires authToken`));
});

/**
 * @test {ContentType.getAttributes}
 */
test(`[unit] ContentType.getAttributes should receive a valid request and response for a get request without an id`, 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 contentType.getAttributes();
  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');
});

/**
 * @test {ContentType.getAttributes}
 */
test('[unit] ContentType.getAttributes should receive a valid response for a get request with an id', async (t) => {
  const { getResponseAttributesId } = t.context;
  const contentType = new ContentType({ id: 10 });
  let request = {};
  nock(client.apiBaseUrl).get(`/v3/dynamic_content/content_types/${contentType.id}/dynamic_content/attribute_keys/31`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseAttributesId];
  });
  const response = await contentType.getAttributes({ id: 31 });
  t.is(request.path, `/v3/dynamic_content/content_types/${contentType.id}/dynamic_content/attribute_keys/31`);
  t.is(request.headers.accept, 'application/json');
  t.is(request.headers['content-type'], 'application/json');

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


// // ContentType.save
// /**
//  * @test {ContentType.save}
//  */
// test('[unit] ContentType.save should successfully insert a site with a site object when id does not exist', async (t) => {
//   const { contentTypeJsonApi, insertResponse } = t.context;
//   const json = JSON.parse(JSON.stringify(contentTypeJsonApi));
//   delete json.id;
//   const contentType = new ContentType({ data: json });
//   let request = {};
//   // nock(client.apiBaseUrl).post(`/v3/trials/sites`).reply(201, t.context.insertResponse);
//   nock(client.apiBaseUrl).post(`/v3/trials/sites`).reply(function (uri, requestBody) {
//     request = this.req;
//     request.requestBody = requestBody;
//     return [201, insertResponse];
//   });
//   const response = await contentType.save();

//   t.is(request.path, `/v3/trials/sites`);
//   t.is(request.headers.accept, 'application/json');
//   t.deepEqual(request.requestBody, { data: json });
//   t.is(request.headers['content-type'], 'application/json');
//   t.is(request.headers.authorization, 'Token token=valid_token');
//   t.is(response.id, 16);
//   t.is(response.type, 'dynamic_content__content_types');
// });

// /**
//  * @test {ContentType.save}
//  */
// test('[unit] ContentType.save should successfully update a site with a site object when id exists', async (t) => {
//   const { contentTypeJsonApi, updateResponse } = t.context;
//   const contentType = new ContentType({ data: contentTypeJsonApi });
//   let request = {};
//   nock(client.apiBaseUrl).patch(`/v3/trials/sites/${contentType.id}`).reply(function (uri, requestBody) {
//     request = this.req;
//     request.requestBody = requestBody;
//     return [200, updateResponse];
//   });
//   const response = await contentType.save();

//   t.is(request.path, `/v3/trials/sites/${contentType.id}`);
//   t.is(request.headers.accept, 'application/json');
//   t.deepEqual(request.requestBody, { data: contentTypeJsonApi });
//   t.is(request.headers['content-type'], 'application/json');
//   t.is(request.headers.authorization, 'Token token=valid_token');
//   t.is(response.id, 4);
//   t.is(response.type, 'dynamic_content__content_types');
// });