Manual Reference Source Test

test/unit/services.content-attribute.js

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

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

  t.context.patchResponse = {
    data: {
      id: 1,
      type: 'dynamic_content__attribute_keys',
      attributes: {
        name: 'firstname',
        attribute_type: 'string',
        required: true,
        position: 1,
        display_name: 'First Name',
        default_value: 'dummy_1266',
        show_on_index: true,
        created_at: '2018-04-24T05:40:24Z',
        updated_at: '2018-04-24T05:45:19Z'
      },
      relationships: {
        content_type: {
          data: {
            id: 1,
            type: 'dynamic_content__content_types'
          }
        }
      }
    }
  };

  t.context.postResponse = {
    data: {
      id: 2,
      type: 'dynamic_content__attribute_keys',
      attributes: {
        name: 'reminder',
        display_name: 'Your Reminder',
        required: false,
        attribute_type: 'string',
        status: 'enabled',
        position: 1,
        default_value: 'dummy_1266',
        show_on_index: true,
        created_at: '2018-04-24T05:40:24Z',
        updated_at: '2018-04-24T05:45:19Z'
      },
      relationships: {
        content_type: {
          data: {
            id: 1000,
            type: 'dynamic_content__content_types'
          }
        }
      }
    }
  };

  t.context.contentAttributeJsonApi = {
    data: {
      id: 1,
      type: 'dynamic_content__attribute_keys',
      attributes: {
        required: true
      }
    }
  };

  t.context.contentType1000 = new ContentType({ id: 1000 });
  await t.context.contentType1000.store();
});

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.storage = client.storageUtility;
  t.context.contentAttribute = new ContentAttribute(t.context.contentAttributeJsonApi);
});


// ContentAttributeService.delete method
/**
 * @test {Clinical6.delete}
 */
test('[unit] ContentAttributeService.delete should throw errors for invalid parameters', async (t) => {
  const undefinedError = 'ContentAttributeService.delete error: attribute_key does not have id';
  await t.throwsAsync(clinical6.delete(new ContentAttribute()), undefinedError);
});

/**
 * @test {Clinical6.delete}
 */
test('[unit] ContentAttributeService.delete should make a properly formatted delete request and response', async (t) => {
  const { contentAttribute } = t.context;
  contentAttribute.id = 5;
  let request = {};
  nock(client.apiBaseUrl).delete(`/v3/dynamic_content/attribute_keys/${contentAttribute.id}`).reply(function () {
    request = this.req;
    return [200, ''];
  });
  const response = await clinical6.delete(contentAttribute);
  t.is(request.path, `/v3/dynamic_content/attribute_keys/${contentAttribute.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('[unit] ContentAttributeService.delete should remove the element from local storage', async (t) => {
  const { contentAttribute, storage } = t.context;
  contentAttribute.id = 5;
  nock(client.apiBaseUrl).delete(`/v3/dynamic_content/attribute_keys/${contentAttribute.id}`).reply(200, '');
  await storage.set('dynamic_content__attribute_keys', contentAttribute.toJSON(), { id: contentAttribute.id });
  await clinical6.delete(contentAttribute);
  t.is(storage.has('dynamic_content__attribute_keys', { id: contentAttribute.id }), false);
});


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

/**
 * @test {Clinical6.insert}
 */
test('[unit] ContentAttributeService.insert should successfully insert a content attribute with a content attribute object', async (t) => {
  const { postResponse } = t.context;
  let request = {};
  nock(client.apiBaseUrl).post('/v3/dynamic_content/attribute_keys').reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, postResponse];
  });

  const requestJsonApi = {
    data: {
      type: 'dynamic_content__attribute_keys',
      attributes: {
        name: 'reminder',
        display_name: 'Your Reminder',
        required: false,
        attribute_type: 'string',
        status: 'enabled'
      },
      relationships: {
        content_type: {
          data: {
            id: 1000,
            type: 'dynamic_content__content_types'
          }
        }
      }
    }
  };

  client.mobileApplicationKey = '0355b2fb1c8fa7210bec203abb32b916';
  const contentAttribute = new ContentAttribute(requestJsonApi);
  const response = await clinical6.insert(contentAttribute);

  t.is(request.path, `/v3/dynamic_content/attribute_keys`);
  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, 2);
  t.is(response.type, 'dynamic_content__attribute_keys');
  t.is(response.name, 'reminder');
  t.is(response.displayName, 'Your Reminder');
  t.is(response.required, false);
  t.is(response.attributeType, 'string');
  t.is(response.status, 'enabled');
  t.is(response.contentType.id, 1000);
});

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

// ContentAttributeService.update method
/**
 * @test {Clinical6.update}
 */
test('[unit] ContentAttributeService.update should successfully update a contentAttribute with a contentAttribute object', async (t) => {
  const { contentAttribute, patchResponse, contentAttributeJsonApi } = t.context;
  let request = {};
  nock(client.apiBaseUrl).patch(`/v3/dynamic_content/attribute_keys/${contentAttribute.id}`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, patchResponse];
  });

  const response = await clinical6.update(contentAttribute);

  t.is(request.path, `/v3/dynamic_content/attribute_keys/${contentAttribute.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, contentAttributeJsonApi);

  t.is(response.id, 1);
  t.is(response.type, 'dynamic_content__attribute_keys');
  t.is(response.name, 'firstname');
  t.is(response.attributeType, 'string');
  t.is(response.required, true);
  t.is(response.position, 1);
  t.is(response.displayName, 'First Name');
  t.is(response.defaultValue, 'dummy_1266');
  t.is(response.showOnIndex, true);
  t.is(response.createdAt, '2018-04-24T05:40:24Z');
  t.is(response.updatedAt, '2018-04-24T05:45:19Z');
  t.is(response.relationships.relationships.content_type.data.id, 1);
  t.is(response.relationships.relationships.content_type.data.type, 'dynamic_content__content_types');
});