Manual Reference Source Test

test/unit/utility.captivereacherror.js

import test from 'ava';
import { Clinical6Error } from '../../src';

// See tests.js for testing client creation
let err = new Clinical6Error();

// Used to fake an async ajax request/response via jasmine-ajax
// let request = null;

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should have a be an Error', (t) => {
  err = new Clinical6Error('Test');
  t.is(err instanceof Error, true);
  t.is(err.name, 'Clinical6Error');
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle the duplicate account name response', (t) => {
  const response = {
    error_detail: {
      account_name: [
        'already_exists'
      ]
    },
    friendly_error: 'Please review each field and check for the following potential issues:\n'
      + '  - All required fields are properly filled in.\n'
      + '  - Formatting, such as date or zip code is correct\n'
      + '  - You are not using an email or site ID that has been previously used. \n'
      + '  - Make sure that you are not using incorrect punctuation or capitalization.\n',
    internal_code: 50362,
    more_info: '',
    status: 'fail',
    message: 'Please review each field and check for the following potential issues:\n'
      + '  - All required fields are properly filled in.\n'
      + '  - Formatting, such as date or zip code is correct\n'
      + '  - You are not using an email or site ID that has been previously used. \n'
      + '  - Make sure that you are not using incorrect punctuation or capitalization.\n'
  };
  err = new Clinical6Error(response);
  t.is(err.message, `Account name already exists:\n${response.friendly_error}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle the duplicate account name response in html', (t) => {
  const response = {
    error_detail: {
      account_name: [
        'already_exists'
      ]
    },
    friendly_error: 'Please review each field and check for the following potential issues:\n'
      + '  - All required fields are properly filled in.\n'
      + '  - Formatting, such as date or zip code is correct\n'
      + '  - You are not using an email or site ID that has been previously used. \n'
      + '  - Make sure that you are not using incorrect punctuation or capitalization.\n',
    internal_code: 50362,
    more_info: '',
    status: 'fail',
    message: 'Please review each field and check for the following potential issues:\n'
      + '  - All required fields are properly filled in.\n'
      + '  - Formatting, such as date or zip code is correct\n'
      + '  - You are not using an email or site ID that has been previously used. \n'
      + '  - Make sure that you are not using incorrect punctuation or capitalization.\n'
  };

  const expectedMessage = 'Please review each field and check for the following potential issues:<br />'
      + '  - All required fields are properly filled in.<br />'
      + '  - Formatting, such as date or zip code is correct<br />'
      + '  - You are not using an email or site ID that has been previously used. <br />'
      + '  - Make sure that you are not using incorrect punctuation or capitalization.<br />';
  err = new Clinical6Error(response);
  t.is(err.messageHTML, `Account name already exists:<br />${expectedMessage}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle the "something went wrong" response', (t) => {
  const response = {
    error_detail: 'internal_error',
    friendly_error: 'Something went wrong',
    internal_code: 50366,
    more_info: ''
  };
  err = new Clinical6Error(response);
  t.is(err.message, `Internal error:\n${response.friendly_error}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle the invalid user role response', (t) => {
  const response = {
    error_detail: {
      user_role: [
        'invalid_value'
      ]
    },
    friendly_error: 'Please review each field and check for the following potential issues:\n'
      + '  - All required fields are properly filled in.\n'
      + '  - Formatting, such as date or zip code is correct\n'
      + '  - You are not using an email or site ID that has been previously used. \n'
      + '  - Make sure that you are not using incorrect punctuation or capitalization.\n',
    internal_code: 50362,
    more_info: '',
    status: 'fail',
    message: 'Please review each field and check for the following potential issues:\n'
      + '  - All required fields are properly filled in.\n'
      + '  - Formatting, such as date or zip code is correct\n'
      + '  - You are not using an email or site ID that has been previously used. \n'
      + '  - Make sure that you are not using incorrect punctuation or capitalization.\n'
  };
  err = new Clinical6Error(response);
  t.is(err.message, `User role invalid value:\n${response.friendly_error}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle the device is invalid (v3)', (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3mobile_usersregistrations/create.html
  // /v3/mobile_users/registrations
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3mobile_userssessions/create.html
  // /v3/mobile_users/sessions
  const response = {
    errors: [
      {
        title: 'An error occured',
        detail: 'Device is invalid.',
        status: '422'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle the device is already in use (v3)', (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3mobile_usersregistrations/create.html
  // /v3/mobile_users/registrations
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3mobile_userssessions/create.html
  // /v3/mobile_users/session
  const response = {
    errors: [
      {
        title: 'An error occured',
        detail: 'Device already in use.',
        status: '422'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle application key cant be blank (v3)', (t) => {
  const response = {
    errors: [
      {
        source: {
          pointer: '/data/attributes/mobile_application_key'
        },
        detail: 'can\'t be blank'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle permanent_link is invalid (v3)', (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3evaluatorevaluations/create.html
  // evaluator/evaluations
  const response = {
    errors: [
      {
        source: {
          pointer: '/data/attributes/permanent_link'
        },
        detail: 'the value is invalid'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle email not found (v3)', (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3mobile_userspasswords/create.html
  // /v3/mobile_users/password
  const response = {
    errors: [
      {
        source: {
          pointer: '/data/attributes/email'
        },
        detail: 'not found'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle param is missing or the value is empty: data (v3)', (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3relationshipslikes/create.html
  // //v3/:resource/likes
  const response = {
    errors: [
      {
        title: 'Invalid parameters',
        detail: 'param is missing or the value is empty: data',
        status: '422'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle Record Not Found: MobileUser by id (v3)', (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3relationshipsprofiles/update.html
  // /v3/mobile_users/00000/profile
  const response = {
    errors: [
      {
        title: 'Record Not Found',
        detail: 'Couldn\'t find MobileUser with \'id\'=00000',
        code: 'not-found',
        status: '404'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});

/**
 * @test {Clinical6Error}
 */
test('[unit] Clinical6Error utility class should handle Email is invalid (v3)', (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3relationshipssites/update.html
  // /v3/sites/38
  const response = {
    errors: [
      {
        title: 'An error occured',
        detail: {
          email: [
            'is invalid'
          ]
        },
        status: '422'
      }
    ]
  };
  err = new Clinical6Error(response);
  const title = (response.errors[0].title) ? response.errors[0].title : `"${response.errors[0].source.pointer}"`;
  t.is(err.message, `${title}: ${response.errors[0].detail}`);
});