Manual Reference Source Test

test/unit/services.event.v2.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  eventService,
  Event,
  Rule,
  User,
} from '../../src';

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

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

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

  t.context.storage = client.storageUtility;
  t.context.eventJsonApi = {
    data: {
      id: 7,
      type: 'reminder__events',
      attributes: {
        date: '2016-06-06T00:00:00Z'
      },
      relationships: {
        rule: {
          data: {
            type: 'reminder__rules',
            id: 19
          }
        },
        mobile_user: {
          data: {
            type: 'mobile_users',
            id: 215
          }
        }
      }
    }
  };

  t.context.event = new Event(t.context.eventJsonApi);
  t.context.event.rule = new Rule({ id: 19 });
  t.context.event.rule.store();
  t.context.event.mobileUser = new User({ id: 215 });
  t.context.event.mobileUser.store();

  t.context.reportedActionJsonApi = {
    action: 'callback',
    resource: 'patient',
    context: {
      value: 'approved'
    }
  };
});

/**
 * @test {EventService.trackEvents}
 */
test('[unit] EventService.trackEvents should successfully submit this event to track', async (t) => {
  const { reportedActionJsonApi } = t.context;
  const json = JSON.parse(JSON.stringify(reportedActionJsonApi));
  nock(client.apiBaseUrl).post(`/api/v2/event/reported_actions`).reply(204, {
    action: 'callback',
    resource: 'patient',
    context: {
      value: 'approved'
    }
  });
  const response = await eventService.trackEvents(json.resource, json.action, json.context.value);
  t.is(response.action, 'callback');
  t.is(response.resource, 'patient');
  t.is(response.context.value, 'approved');
});

/**
 * @test {EventService.trackEvents}
 */
// test('[unit] EventService.trackEvents should reply with a friendly error message for missing/invalid tokens', async (t) => {
//   nock(client.apiBaseUrl).post(`/api/v2/event/reported_actions`).reply(404, {
//     error_detail: 'Trigger not found: invalid_value',
//     friendly_error: 'Trigger not found using the value:Trigger not found',
//     internal_code: 50364,
//     more_info: '',
//     status: 'fail',
//     message: 'Trigger not found using the value:Trigger not found'
//   });
//   const response = await eventService.trackEvents().catch(r => r);
//   t.is(response.status, 'fail');
//   t.is(response.details, 'Trigger not found: invalid_value');
//   t.is(response.friendly, 'Trigger not found using the value:Trigger not found');
//   t.is(response.code, 50364);
// });