Manual Reference Source Test

test/unit/helpers.filter-group.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  FilterGroup,
} 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: 5,
      type: 'filter_groups',
      attributes: {
        operator: 'and',
        created_at: '2018-04-30T21:39:05Z',
        updated_at: '2018-04-30T21:39:05Z'
      },
      relationships: {
        cohort: {
          data: {
            id: 74,
            type: 'cohorts'
          }
        }
      }
    }
  };

  t.context.filterGroupJsonApi = {
    type: 'filter_groups',
    attributes: {
      operator: 'and',
      created_at: undefined,
      updated_at: undefined,
    },
    relationships: {
      cohort: {
        data: {
          id: 74,
          type: 'cohorts'
        }
      }
    }
  };

  t.context.filterGroupJson = {
    type: 'filter_groups',
    operator: 'and',
    created_at: undefined,
    updated_at: undefined,
    relationships: {
      cohort: {
        data: {
          id: 74,
          type: 'cohorts'
        }
      }
    }
  };

  client.storageUtility.clear();
  t.context.storage = client.storageUtility;
  client.filterGroup = new FilterGroup({});
  t.context.filterGroup = new FilterGroup({ data: t.context.filterGroupJsonApi });
});

/**
 * @test {FilterGroup}
 */
test('[unit] FilterGroup should handle filterGroup data with a normal json format', (t) => {
  const { filterGroupJson } = t.context;
  const filterGroup = new FilterGroup(filterGroupJson);
  t.is(filterGroup.operator, 'and');
  t.is(filterGroup.createdAt, undefined);
  t.is(filterGroup.updatedAt, undefined);
  t.is(filterGroup.relationships.relationships.cohort.data.id, 74);
  t.is(filterGroup.relationships.relationships.cohort.data.type, 'cohorts');
});


/**
 * @test {FilterGroup}
 */
test('[unit] FilterGroup should handle filterGroup data with json api format', (t) => {
  const { filterGroupJsonApi } = t.context;
  const filterGroup = new FilterGroup({ data: filterGroupJsonApi });
  t.is(filterGroup.operator, 'and');
  t.is(filterGroup.createdAt, undefined);
  t.is(filterGroup.updatedAt, undefined);
  t.is(filterGroup.relationships.relationships.cohort.data.id, 74);
  t.is(filterGroup.relationships.relationships.cohort.data.type, 'cohorts');
});

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

  filterGroup = new FilterGroup({ data: filterGroupJsonApi });
  t.deepEqual(filterGroup.toJSON(), filterGroupJsonApi);
});

/**
  * @test {FilterGroup.save}
  */
test('[unit] FilterGroup.save should successfully save a new filterGroup when id does not exist', async (t) => {
  const { filterGroupJsonApi, insertResponse } = t.context;
  const json = JSON.parse(JSON.stringify(filterGroupJsonApi));
  // delete json.id;
  const filterGroup = new FilterGroup({ data: json });
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/filter_groups`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [201, insertResponse];
  });

  const response = await filterGroup.save();

  t.is(request.path, `/v3/filter_groups`);
  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, 5);
  t.is(response.type, 'filter_groups');
  t.is(response.operator, 'and');
  t.is(response.createdAt, '2018-04-30T21:39:05Z');
  t.is(response.updatedAt, '2018-04-30T21:39:05Z');
  t.is(response.cohort.id, 74);
  t.is(response.cohort.type, 'cohorts');
});