Manual Reference Source Test

test/unit/helpers.entry-group.js

import test from 'ava';
import sinon from 'sinon';
import {
  client,
  EntryGroup,
} from '../../src';

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

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

test.beforeEach((t) => {
  client.authToken = 'valid_token';
  t.context.server = sinon.fakeServer.create();
  t.context.server.autoRespond = true;
  t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/ediary/entry_groups/12/ediary/entry_templates`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: [
        {
          id: '9',
          type: 'ediary__entry_templates',
          attributes: {
            category: 'automatic',
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z'
          },
          relationships: {
            entry_group: {
              data: {
                id: '12',
                type: 'ediary__entry_groups'
              }
            },
            flow_process: {
              data: {
                id: '142',
                type: 'data_collection__flow_processes'
              }
            }
          }
        },
        {
          id: '10',
          type: 'ediary__entry_templates',
          attributes: {
            category: 'automatic',
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z'
          },
          relationships: {
            entry_group: {
              data: {
                id: '12',
                type: 'ediary__entry_groups'
              }
            },
            flow_process: {
              data: {
                id: '143',
                type: 'data_collection__flow_processes'
              }
            }
          }
        },
        {
          id: '11',
          type: 'ediary__entry_templates',
          attributes: {
            category: 'automatic',
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z'
          },
          relationships: {
            entry_group: {
              data: {
                id: '12',
                type: 'ediary__entry_groups'
              }
            },
            flow_process: {
              data: {
                id: '144',
                type: 'data_collection__flow_processes'
              }
            }
          }
        }
      ],
      included: [
        {
          id: '12',
          type: 'ediary__entry_groups',
          attributes: {
            name: 'dummy_1534',
            permanent_link: 'dummy_1535',
            position: 1,
            category: null,
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z'
          },
          relationships: {
            ediary_entries: {
              data: []
            },
            entry_templates: {
              data: [
                {
                  id: '9',
                  type: 'ediary__entry_templates'
                },
                {
                  id: '10',
                  type: 'ediary__entry_templates'
                },
                {
                  id: '11',
                  type: 'ediary__entry_templates'
                }
              ]
            },
            child_entry_groups: {
              data: []
            },
            parent_entry_group: {
              data: null
            }
          }
        },
        {
          id: '142',
          type: 'data_collection__flow_processes',
          attributes: {
            name: 'veritatis',
            permanent_link: 'dummy_1536',
            consent_credentials: null,
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z',
            owner_type: 'MobileUser',
            published_at: null,
            conditional_paths: null
          },
          relationships: {
            linked_steps: {
              data: []
            },
            published: {
              data: null
            },
            draft: {
              data: null
            },
            initial_step: {
              data: null
            }
          }
        },
        {
          id: '143',
          type: 'data_collection__flow_processes',
          attributes: {
            name: 'veritatis',
            permanent_link: 'dummy_1537',
            consent_credentials: null,
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z',
            owner_type: 'MobileUser',
            published_at: null,
            conditional_paths: null
          },
          relationships: {
            linked_steps: {
              data: []
            },
            published: {
              data: null
            },
            draft: {
              data: null
            },
            initial_step: {
              data: null
            }
          }
        },
        {
          id: '144',
          type: 'data_collection__flow_processes',
          attributes: {
            name: 'veritatis',
            permanent_link: 'dummy_1538',
            consent_credentials: null,
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z',
            owner_type: 'MobileUser',
            published_at: null,
            conditional_paths: null
          },
          relationships: {
            linked_steps: {
              data: []
            },
            published: {
              data: null
            },
            draft: {
              data: null
            },
            initial_step: {
              data: null
            }
          }
        }
      ]
    })]);
  t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/ediary/entry_groups/13/ediary/entry_templates?filters[category]=manual`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: [
        {
          id: '12',
          type: 'ediary__entry_templates',
          attributes: {
            category: 'manual',
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z'
          },
          relationships: {
            entry_group: {
              data: {
                id: '13',
                type: 'ediary__entry_groups'
              }
            },
            flow_process: {
              data: {
                id: '145',
                type: 'data_collection__flow_processes'
              }
            }
          }
        }
      ],
      included: [
        {
          id: '13',
          type: 'ediary__entry_groups',
          attributes: {
            name: 'dummy_1539',
            permanent_link: 'dummy_1540',
            position: 1,
            category: null,
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z'
          },
          relationships: {
            ediary_entries: {
              data: []
            },
            entry_templates: {
              data: [
                {
                  id: '12',
                  type: 'ediary__entry_templates'
                },
                {
                  id: '13',
                  type: 'ediary__entry_templates'
                },
                {
                  id: '14',
                  type: 'ediary__entry_templates'
                }
              ]
            },
            child_entry_groups: {
              data: []
            },
            parent_entry_group: {
              data: null
            }
          }
        },
        {
          id: '145',
          type: 'data_collection__flow_processes',
          attributes: {
            name: 'veritatis',
            permanent_link: 'dummy_1541',
            consent_credentials: null,
            created_at: '2017-06-27T20:32:59Z',
            updated_at: '2017-06-27T20:32:59Z',
            owner_type: 'MobileUser',
            published_at: null,
            conditional_paths: null
          },
          relationships: {
            linked_steps: {
              data: []
            },
            published: {
              data: null
            },
            draft: {
              data: null
            },
            initial_step: {
              data: null
            }
          }
        }
      ]
    })]);

  t.context.groupJsonApi = {
    id: 1,
    type: 'ediary__entry_groups',
    attributes: {
      category: null,
      created_at: '2017-06-20T17:55:03Z',
      name: 'name_0',
      permanent_link: 'perm_link_0',
      position: 1,
      updated_at: '2017-06-20T17:55:03Z'
    },
    relationships: {
      ediary_entries: {
        data: []
      },
      entry_templates: {
        data: [
          {
            id: 1,
            type: 'ediary__entry_templates'
          }
        ]
      },
      child_entry_groups: {
        data: []
      },
      parent_entry_group: {
        data: null
      }
    }
  };
});

test.afterEach(t => t.context.server.restore());

/**
 * @test {EntryGroup}
 */
test('[unit] EntryGroup should handle group data with json api format', (t) => {
  const { groupJsonApi } = t.context;
  const group = new EntryGroup({ data: groupJsonApi });
  t.is(group.id, 1);
  t.is(group.type, 'ediary__entry_groups');
  t.is(group.name, 'name_0');
  t.is(group.permanentLink, 'perm_link_0');
  t.is(group.createdAt, '2017-06-20T17:55:03Z');
  t.is(group.updatedAt, '2017-06-20T17:55:03Z');
});

/**
 * @test {EntryGroup}
 */
test('[unit] EntryGroup should generate json api format when converted to string', (t) => {
  const { groupJsonApi } = t.context;
  const group = new EntryGroup(groupJsonApi);
  t.deepEqual(group.toJSON(), groupJsonApi);
});

/**
 * @test {EntryGroup.getTemplates}
 */
test('[unit] EntryGroup.getTemplates should receive a valid response (api example 1)', async (t) => {
  const group = new EntryGroup({ id: 12 });
  const response = await group.getTemplates(undefined, 'networkOnly');

  // const { method, url, requestHeaders } = t.context.server.requests[0];
  // t.is(method, 'GET');
  // t.is(url, `${client.apiBaseUrl}/v3/ediary/entry_groups/12/ediary/entry_templates`);
  // t.is(requestHeaders.Accept, 'application/json');
  // t.is(requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(requestHeaders.Authorization, 'Token token=valid_token');

  t.truthy(response);
  t.is(response.length, 3);
  t.is(response[0].id, 9);
  t.is(response[0].type, 'ediary__entry_templates');
  t.is(response[0].category, 'automatic');
  t.is(response[0].createdAt, '2017-06-27T20:32:59Z');
  t.is(response[0].updatedAt, '2017-06-27T20:32:59Z');
  t.is(response[0].entryGroup.id, 12);
  t.is(response[0].entryGroup.name, 'dummy_1534');
  t.is(response[0].entryGroup.permanentLink, 'dummy_1535');
  t.is(response[0].entryGroup.position, 1);
  t.is(response[0].entryGroup.createdAt, '2017-06-27T20:32:59Z');
  t.is(response[0].entryGroup.updatedAt, '2017-06-27T20:32:59Z');
  t.is(response[0].flow.id, 142);
  t.is(response[0].flow.permanentLink, 'dummy_1536');
  t.is(response[0].flow.name, 'veritatis');
  t.is(response[0].flow.owner_type, 'MobileUser');
  t.is(response[0].flow.ownerType, 'MobileUser');
  t.is(response[0].flow.createdAt, '2017-06-27T20:32:59Z');
  t.is(response[0].flow.updatedAt, '2017-06-27T20:32:59Z');
});

/**
 * @test {EntryGroup.getTemplates}
 */
test('[unit] EntryGroup.getTemplates should receive a valid response (api example 2)', async (t) => {
  const group = new EntryGroup({ id: 13 });
  const response = await group.getTemplates({ category: 'manual' });

  // const { method, url, requestHeaders } = t.context.server.requests[0];
  // t.is(method, 'GET');
  // t.is(url, `${client.apiBaseUrl}/v3/ediary/entry_groups/13/ediary/entry_templates?filters[category]=manual`);
  // t.is(requestHeaders.Accept, 'application/json');
  // t.is(requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(requestHeaders.Authorization, 'Token token=valid_token');

  t.truthy(response);
  t.is(response.length, 1);
  t.is(response[0].id, 12);
  t.is(response[0].type, 'ediary__entry_templates');
  t.is(response[0].category, 'manual');
  t.is(response[0].createdAt, '2017-06-27T20:32:59Z');
  t.is(response[0].updatedAt, '2017-06-27T20:32:59Z');
  t.is(response[0].entryGroup.id, 13);
  t.is(response[0].entryGroup.name, 'dummy_1539');
  t.is(response[0].entryGroup.permanentLink, 'dummy_1540');
  t.is(response[0].entryGroup.position, 1);
  t.is(response[0].entryGroup.createdAt, '2017-06-27T20:32:59Z');
  t.is(response[0].entryGroup.updatedAt, '2017-06-27T20:32:59Z');
  t.is(response[0].flow.id, 145);
  t.is(response[0].flow.permanentLink, 'dummy_1541');
  t.is(response[0].flow.name, 'veritatis');
  t.is(response[0].flow.owner_type, 'MobileUser');
  t.is(response[0].flow.ownerType, 'MobileUser');
  t.is(response[0].flow.createdAt, '2017-06-27T20:32:59Z');
  t.is(response[0].flow.updatedAt, '2017-06-27T20:32:59Z');
});