Manual Reference Source Test

test/unit/helpers.content-attribute.js

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

// See tests.js for testing client creation
test.before('start server', () => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
  client.authToken = 'valid_token';
});

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

  t.context.insertResponse = {
    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,
        status: 'enabled',
        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.contentAttributeJsonApi = {
    id: 1,
    type: 'dynamic_content__attribute_keys',
    attributes: {
      required: true,
      attribute_type: undefined,
      created_at: undefined,
      default_value: undefined,
      display_name: undefined,
      name: undefined,
      position: undefined,
      show_on_index: undefined,
      status: 'enabled',
      updated_at: undefined,
    },
  };

  t.context.contentAttributeJson = {
    id: 1,
    type: 'dynamic_content__attribute_keys',
    required: true,
    attribute_type: undefined,
    created_at: undefined,
    default_value: undefined,
    display_name: undefined,
    name: undefined,
    position: undefined,
    show_on_index: undefined,
    updated_at: undefined,
  };

  client.storageUtility.clear();
  t.context.storage = client.storageUtility;
  client.contentAttribute = new ContentAttribute({});
  t.context.contentAttribute = new ContentAttribute({ data: t.context.contentAttributeJsonApi });
});

/**
 * @test {ContentAttribute}
 */
test('[unit] ContentAttribute should handle contentAttribute data with a normal json format', (t) => {
  const { contentAttributeJson } = t.context;
  const contentAttribute = new ContentAttribute(contentAttributeJson);
  t.is(contentAttribute.type, 'dynamic_content__attribute_keys');
  t.is(contentAttribute.required, true);
});

/**
 * @test {ContentAttribute}
 */
test('[unit] ContentAttribute should handle contentAttribute data with json api format', (t) => {
  const { contentAttributeJsonApi } = t.context;
  const contentAttribute = new ContentAttribute({ data: contentAttributeJsonApi });
  t.is(contentAttribute.type, 'dynamic_content__attribute_keys');
  t.is(contentAttribute.required, true);
});

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

  contentAttribute = new ContentAttribute({ data: contentAttributeJsonApi });
  t.deepEqual(contentAttribute.toJSON(), contentAttributeJsonApi);
});

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

  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 {ContentAttribute.save}
 */
test('[unit] ContentAttribute.save should successfully update a contentAttribute with a contentAttribute object when an id exists', async (t) => {
  const { contentAttributeJsonApi } = t.context;
  let requestBody = {};
  const server = nock(client.apiBaseUrl)
    .patch('/v3/dynamic_content/attribute_keys/1', (body) => {
      requestBody = body;
      return { data: json };
    })
    .reply(201, {
      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,
          status: 'enabled',
          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'
            }
          }
        }
      }
    });

  const json = JSON.parse(JSON.stringify(contentAttributeJsonApi));
  const contentAttribute = new ContentAttribute({ data: json });
  const response = await contentAttribute.save();
  t.deepEqual(requestBody, { data: json });
  server.done();

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