Manual Reference Source Test

test/unit/helpers.flow-key-value.js

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

// See tests.js for testing client creation
const flowKeyValueJsonApi = {
  id: 'dummy_725_published_23',
  type: 'data_collection__flow_process_values',
  attributes: {
    56: 'male',
    57: 15
  },
  relationships: {
    flow_process: {
      data: {
        id: 94,
        type: 'data_collection__flow_processes'
      }
    },
    captured_value_group: {
      data: {
        id: 23,
        type: 'data_collection__captured_value_groups'
      }
    },
    owner: {
      data: {
        id: 59,
        type: 'mobile_users'
      }
    }
  }
};
const flowKeyValueJson = {
  id: 'dummy_725_published_23',
  type: 'data_collection__flow_process_values',
  attributes: {
    56: 'male',
    57: 15
  },
  relationships: {
    flow_process: {
      data: {
        id: 94,
        type: 'data_collection__flow_processes'
      }
    },
    captured_value_group: {
      data: {
        id: 23,
        type: 'data_collection__captured_value_groups'
      }
    },
    owner: {
      data: {
        id: 59,
        type: 'mobile_users'
      }
    }
  }
};

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.server = sinon.fakeServer.create();
  t.context.server.autoRespond = true;

  t.context.server.respondWith('DELETE', `${client.apiBaseUrl}/v3/data_collection/flow_process_values/*`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]);

  t.context.server.respondWith('PATCH', `${client.apiBaseUrl}/v3/data_collection/flow_process_values/*`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: '4',
        type: 'data_collection__flow_process_values',
        attributes: {
          created_at: '2017-08-09T17:26:59Z',
          updated_at: '2017-08-09T17:26:59Z',
          title: 'dummy_1065',
          description: 'Demo Content info...',
          redemption_points: 250,
          image: {
            image: {
              url: '/uploads/test/reward/image/4/1.png',
              small: {
                url: '/uploads/test/reward/image/4/small_1.png'
              },
              small_hd: {
                url: '/uploads/test/reward/image/4/small_hd_1.png'
              },
              fullscreen: {
                url: '/uploads/test/reward/image/4/fullscreen_1.png'
              },
              main: {
                url: '/uploads/test/reward/image/4/main_1.png'
              },
              fullscreen_hd: {
                url: '/uploads/test/reward/image/4/fullscreen_hd_1.png'
              },
              main_hd: {
                url: '/uploads/test/reward/image/4/main_hd_1.png'
              }
            }
          },
          position: 4,
          enabled: true,
          featured: null,
          based_on: null,
          threshold: null,
          time_unit: null,
          start_point: null,
          start_at: null,
          disabled_image: {
            disabled_image: {
              url: '/default/default.png',
              small: {
                url: '/default/small_default.png'
              },
              small_hd: {
                url: '/default/small_hd_default.png'
              },
              fullscreen: {
                url: '/default/fullscreen_default.png'
              },
              main: {
                url: '/default/main_default.png'
              },
              fullscreen_hd: {
                url: '/default/fullscreen_hd_default.png'
              },
              main_hd: {
                url: '/default/main_hd_default.png'
              }
            }
          },
          cache_token: 'iP78O8Ag65c'
        }
      }
    })]);

  t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/data_collection/flow_process_values`,
    [201, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: 'dummy_660_published_22',
        type: 'data_collection__flow_process_values',
        attributes: {
          46: 'male',
          47: 15
        },
        relationships: {
          flow_process: {
            data: {
              id: '90',
              type: 'data_collection__flow_processes'
            }
          },
          captured_value_group: {
            data: {
              id: '22',
              type: 'data_collection__captured_value_groups'
            }
          },
          owner: {
            data: {
              id: '58',
              type: 'mobile_users'
            }
          }
        }
      },
      included: [
        {
          id: '90',
          type: 'data_collection__flow_processes',
          attributes: {
            name: 'repellendus',
            permanent_link: 'dummy_660_published',
            consent_credentials: null,
            created_at: '2018-01-11T20:52:56Z',
            updated_at: '2018-01-11T20:52:57Z',
            owner_type: 'MobileUser',
            published_at: '2018-01-11T20:52:57Z',
            conditional_paths: null
          },
          relationships: {
            linked_steps: {
              data: [
                {
                  id: '43',
                  type: 'data_collection__linked_steps'
                },
                {
                  id: '44',
                  type: 'data_collection__linked_steps'
                },
                {
                  id: '45',
                  type: 'data_collection__linked_steps'
                },
                {
                  id: '46',
                  type: 'data_collection__linked_steps'
                },
                {
                  id: '47',
                  type: 'data_collection__linked_steps'
                },
                {
                  id: '48',
                  type: 'data_collection__linked_steps'
                }
              ]
            },
            published: {
              data: null
            },
            draft: {
              data: {
                id: '89',
                type: 'data_collection__flow_processes'
              }
            },
            initial_step: {
              data: {
                id: '43',
                type: 'data_collection__linked_steps'
              }
            }
          }
        },
        {
          id: '22',
          type: 'data_collection__captured_value_groups',
          attributes: {
            submitted_at: '2018-01-11T20:52:58Z',
            created_at: '2018-01-11T20:52:58Z',
            updated_at: '2018-01-11T20:52:58Z',
            owner_type: 'MobileUser',
            final_submission: false
          },
          relationships: {
            owner: {
              data: {
                id: '58',
                type: 'mobile_users'
              }
            },
            creator: {
              data: {
                id: '58',
                type: 'mobile_users'
              }
            },
            flow_process: {
              data: {
                id: '90',
                type: 'data_collection__flow_processes'
              }
            },
            captured_values: {
              data: [
                {
                  id: '5',
                  type: 'data_collection__captured_values'
                },
                {
                  id: '6',
                  type: 'data_collection__captured_values'
                }
              ]
            },
            status: {
              data: {
                id: '34',
                type: 'statuses'
              }
            },
            ediary_entry: {
              data: null
            }
          }
        },
        {
          id: '58',
          type: 'mobile_users',
          attributes: {
            uuid: 'ba6f338e-10c5-4da4-a85a-623544ca170f',
            account_name: 'dummy_51',
            email: 'user116@fake.com',
            created_at: '2018-01-11T20:52:57Z',
            updated_at: '2018-01-11T20:52:57Z',
            verified_at: null,
            invitation_sent_at: null,
            invitation_accepted_at: null,
            disabled: false,
            disabled_at: null
          },
          relationships: {
            devices: {
              data: []
            },
            user_role: {
              data: null
            },
            profile: {
              data: {
                id: '125',
                type: 'profiles'
              }
            },
            language: {
              data: {
                id: '1',
                type: 'languages'
              }
            },
            patient: {
              data: null
            }
          }
        }
      ]
    })]);

  t.context.storage = client.storageUtility;
  client.flowKeyValue = new FlowKeyValue({});
  t.context.flowKeyValueJsonApi = {
    data: {
      type: 'data_collection__flow_process_values',
      attributes: {
        title: 'dummy_1065',
        description: 'Demo Content info...',
        redemption_points: 250
      }
    }
  };
  t.context.flowKeyValue = new FlowKeyValue(t.context.flowKeyValueJsonApi);
});

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

/**
 * @test {FlowKeyValue}
 */
test('[unit] FlowKeyValue should handle flowKeyValue data with a normal json format', (t) => {
  const flowKeyValue = new FlowKeyValue(flowKeyValueJson);

  t.is(flowKeyValue.id, 'dummy_725_published_23');
  t.is(flowKeyValue.type, 'data_collection__flow_process_values');
  t.is(flowKeyValue['56'], 'male');
  t.is(flowKeyValue['57'], 15);
});

/**
 * @test {FlowKeyValue}
 */
test('[unit] FlowKeyValue should handle flowKeyValue data with json api format', (t) => {
  const flowKeyValue = new FlowKeyValue({ data: flowKeyValueJsonApi });
  t.is(flowKeyValue.id, 'dummy_725_published_23');
  t.is(flowKeyValue.type, 'data_collection__flow_process_values');
  t.is(flowKeyValue['56'], 'male');
  t.is(flowKeyValue['57'], 15);
});

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

  flowKeyValue = new FlowKeyValue({ data: flowKeyValueJsonApi });
  t.deepEqual(flowKeyValue.toJSON(), flowKeyValueJsonApi);
});

/**
 * @test {FlowKeyValue.delete}
 */
test('[unit] FlowKeyValue.delete should successfully delete a flowKeyValue', async (t) => {
  const flowKeyValue = new FlowKeyValue({ data: flowKeyValueJsonApi });
  const response = await flowKeyValue.delete();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'DELETE');
  // t.is(request.url, `${client.apiBaseUrl}/v3/data_collection/flow_process_values/${flowKeyValue.id}`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.truthy(response);
});

/**
 * @test {FlowKeyValue.save}
 */
test('[unit] FlowKeyValue.save should successfully insert a flowKeyValue with a flowKeyValue object when id does not exist', async (t) => {
  const json = JSON.parse(JSON.stringify(flowKeyValueJsonApi));
  delete json.id;
  const flowKeyValue = new FlowKeyValue({ data: json });
  flowKeyValue.flow = new Flow({ id: 94 });
  flowKeyValue.flow.store();
  flowKeyValue.owner = new User({ id: 94 });
  flowKeyValue.owner.store();
  const response = await flowKeyValue.save();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/data_collection/flow_process_values`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.deepEqual(JSON.parse(request.requestBody), { data: json });
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.is(response.id, 'dummy_660_published_22');
  t.is(response.type, 'data_collection__flow_process_values');
});

/**
 * @test {FlowKeyValue.save}
 */
test('[unit] FlowKeyValue.save should successfully update a flowKeyValue with a flowKeyValue object when id exists', async (t) => {
  const flowKeyValue = new FlowKeyValue({ data: flowKeyValueJsonApi });
  flowKeyValue.flow = new Flow({ id: 94 });
  flowKeyValue.flow.store();
  flowKeyValue.owner = new User({ id: 94 });
  flowKeyValue.owner.store();
  const response = await flowKeyValue.save();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/data_collection/flow_process_values`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.deepEqual(JSON.parse(request.requestBody), { data: flowKeyValueJsonApi });
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.is(response.id, 'dummy_660_published_22');
  t.is(response.type, 'data_collection__flow_process_values');
});