Manual Reference Source Test

test/unit/helpers.flowstep.js

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

test.before('start server', () => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
  client.config = {
    track: {
      flows: false,
      gps: false
    }
  };
  client.user = new User({ id: 1 });
});

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

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

  // Reusable Server
  t.context.server = sinon.fakeServer.create();
  t.context.server.autoRespond = true;

  // get thread comments
  t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/discuss/threads/*/comments`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: [
        {
          id: '10',
          type: 'commentable__comments',
          attributes: {
            body: 'Voluptatem cumque ducimus incidunt dolore.',
            created_at: '2017-07-26T15:04:23Z'
          },
          relationships: {
            thread: {
              data: {
                id: '5',
                type: 'commentable__threads'
              }
            },
            author: {
              data: {
                id: '2',
                type: 'users'
              }
            }
          }
        },
        {
          id: '11',
          type: 'commentable__comments',
          attributes: {
            body: 'Voluptate aliquid consequatur unde libero sequi illo est.',
            created_at: '2017-07-26T15:04:23Z'
          },
          relationships: {
            thread: {
              data: {
                id: '5',
                type: 'commentable__threads'
              }
            },
            author: {
              data: {
                id: '3',
                type: 'users'
              }
            }
          }
        },
        {
          id: '12',
          type: 'commentable__comments',
          attributes: {
            body: 'Voluptate aliquid consequatur unde libero sequi illo est.',
            created_at: '2017-07-26T15:04:23Z'
          },
          relationships: {
            thread: {
              data: {
                id: '5',
                type: 'commentable__threads'
              }
            },
            author: {
              data: {
                id: '3',
                type: 'users'
              }
            }
          }
        }
      ]
    })]);

  // get threads
  t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/discuss/threads?filters[commentable_type]=data_collection__linked_steps&filters[commentable_id]=11`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: [
        {
          id: '5',
          type: 'commentable__threads',
          attributes: {
            status: 'open',
            created_at: '2017-07-25T20:32:54Z',
            resolved_at: null
          },
          relationships: {
            commentable: {
              data: {
                id: '11',
                type: 'data_collection__linked_steps'
              }
            },
            owner: {
              data: null
            },
            comments: {
              data: [
                {
                  id: '10',
                  type: 'commentable__comments'
                },
                {
                  id: '11',
                  type: 'commentable__comments'
                },
                {
                  id: '12',
                  type: 'commentable__comments'
                }
              ]
            }
          }
        }
      ]
    })]);

  // add comment to thread
  t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/discuss/threads/5/comments`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: '13',
        type: 'commentable__comments',
        attributes: {
          body: 'That\'s one bad hat Harry.',
          created_at: '2017-07-26T15:04:23Z'
        },
        relationships: {
          thread: {
            data: {
              id: '5',
              type: 'commentable__threads'
            }
          },
          author: {
            data: {
              id: '3',
              type: 'mobile_users'
            }
          }
        }
      }
    })]);

  // add thread
  t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/discuss/threads`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: '5',
        type: 'commentable__threads',
        attributes: {
          status: null,
          created_at: '2017-07-26T15:04:37Z',
          resolved_at: null
        },
        relationships: {
          commentable: {
            data: {
              id: '11',
              type: 'data_collection__linked_steps'
            }
          },
          owner: {
            data: null
          },
          comments: {
            data: []
          }
        }
      }
    })]);

  t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/data_collection/flow_process_values`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({})]);
  t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/v2/data_collection/flow_processes/1/collect`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      status: 'ok',
      data: {
        destinations: {
          cooperative_group: 3095
        },
        process_status: 'completed'
      }
    })]);
  t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/status/transition`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: { value: 'completed' } })]);

  // Reusable Flow
  t.context.flow = new Flow({
    id: 1,
    first_step: 11,
    steps: [
      {
        id: 11,
        title: 'dummy_5',
        description: 'dummy_6',
        content_type: 'info_screen_with_help',
        help: 'ALL text',
        image: { },
        inputs: [
          {
            title: 'Age',
            question_type: 'text',
            style: 'text',
            choice_list: [],
            id: 'first_name',
          },
        ],
        paths: [
          {
            capture: true,
            button_name: 'Next',
            steps: [
              {
                step: 12,
                conditions: [],
              },
            ],
          },
        ],
      },
      {
        id: 12,
        title: 'dummy_5',
        description: 'dummy_6',
        content_type: 'info_screen_with_help',
        help: 'ALL text',
        image: { },
        inputs: [
          {
            title: 'First Name',
            question_type: 'text',
            style: '',
            choice_list: [],
            id: 'first_name',
          },
        ],
        paths: [
          {
            capture: true,
            button_name: 'Next',
            steps: [
              {
                step: 13,
                conditions: [],
              },
            ],
          },
        ],
      },
      {
        id: 13,
        title: 'dummy_5',
        description: 'dummy_6',
        content_type: 'info_screen_with_help',
        help: 'ALL text',
        image: { },
        inputs: [
          {
            title: 'First Name',
            question_type: 'text',
            style: '',
            choice_list: [],
            id: 'first_name',
          },
        ],
        paths: [],
      },
      {
        id: 14,
        title: 'slider',
        description: 'Slider Test',
        content_type: 'info_screen_with_help',
        help: 'Use the slider',
        image: { },
        inputs: [
          {
            id: 1453,
            storage_attribute: '',
            title: '',
            body: '',
            question_type: 'numeric',
            style: 'range',
            required: true,
            validation_expression: null,
            validation_details: null,
            instructions: '',
            choice_list: [],
            locked: null,
            max: 100,
            min: 0,
            max_label: 'Hurting a lot<br>Very uncomfortable<br>Severe pain',
            min_label: 'Not hurting<br>Not discomfort<br>No pain',
            interval: 1,
            labels: []
          }
        ],
        paths: [],
      },
    ],
  });
  t.context.flowStep = t.context.flow.first;
});

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

/**
 * @test {FlowStep.addComment}
 */
test('[unit] FlowStep.addComment should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.addComment);
});

/**
 * @test {FlowStep.addComment}
 */
test('[unit] FlowStep.addComment method should return a promise', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.addComment('test').then);
});

/**
 * @test {FlowStep.getComments}
 */
test('[unit] FlowStep.getComments should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.getComments);
});

/**
 * @test {FlowStep.getComments}
 */
test('[unit] FlowStep.getComments method should return a promise', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.getComments().then);
});

/**
 * @test {FlowStep.set}
 */
test('[unit] FlowStep.set should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.set);
});

/**
 * @test {FlowStep.getPath}
 */
test('[unit] FlowStep.getPath should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.getPath);
});

/**
 * @test {FlowStep.go}
 */
test('[unit] FlowStep.go should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.go);
});

/**
 * @test {FlowStep.go}
 */
test('[unit] FlowStep.go should exist that return a promise', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.go('Next').then);
});

/**
 * @test {FlowStep.hasFields}
 */
test('[unit] FlowStep.hasFields should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.hasFields);
});

/**
 * @test {FlowStep.hasSteps}
 */
test('[unit] FlowStep.hasSteps should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.hasSteps);
});

/**
 * @test {FlowStep.initFields}
 */
test('[unit] FlowStep.initFields should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.initFields);
});

/**
 * @test {FlowStep.isAutoSave}
 */
test('[unit] FlowStep.isAutoSave should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.isAutoSave);
});

/**
 * @test {FlowStep.save}
 */
test('[unit] FlowStep.save should exist', (t) => {
  const { flowStep } = t.context;
  t.truthy(flowStep.save);
});

/**
 * @test {FlowStep.save}
 */
test('[unit] FlowStep.save method should return a promise', (t) => {
  const { flowStep } = t.context;
  flowStep.set('first_name', 'jdoe');
  t.truthy(flowStep.save().then);
});

/**
 * @test {Flow.first}
 */
test('[unit] Flow.first field should be the first flowstep', (t) => {
  const { flow } = t.context;
  t.truthy(flow.first.id);
});

/**
 * @test {Flow.first}
 */
test('[unit] Flow.first field should have paths flow field to be the next flowstep', async (t) => {
  const { flow } = t.context;
  const step = await flow.first.go('Next');
  t.truthy(step.id);
});

/**
 * @test {FlowStep.addComment}
 */
test('[unit] FlowStep.addComment should return a comment object', async (t) => {
  const { flow } = t.context;
  const thread = await flow.first.addComment('Some Comment');
  t.is(thread.id, 5);
});

/**
 * @test {FlowStep.getComments}
 */
test('[unit] FlowStep.getComments should return an array of comment objects', async (t) => {
  const { flow } = t.context;
  const threads = await flow.first.getComments();
  t.is(threads[0].id, 5);
});