Manual Reference Source Test

test/unit/services.consultation-participant.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  clinical6,
  Consultation,
  ConsultationParticipant,
  User
} from '../../src';

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

  t.context.getResponseAll = {};

  t.context.getResponseId = {};

  t.context.patchResponse = {
    data: {
      id: '1',
      type: 'video_consultation_participants',
      attributes: {
        created_at: '2018-07-26T02:48:37Z',
        updated_at: '2018-07-26T02:48:37Z'
      },
      relationships: {
        video_consultation: {
          data: {
            id: '1',
            type: 'video_consultations'
          }
        },
        participant: {
          data: {
            id: '33',
            type: 'mobile_users'
          }
        }
      }
    },
    included: [
      {
        id: '1',
        type: 'video_consultations',
        attributes: {
          name: 'General Consultation',
          start_at: '2018-07-26T02:48:08Z',
          end_at: null,
          status: null,
          confirmed_at: null,
          deleted_at: null,
          created_at: '2018-07-26T02:48:37Z',
          updated_at: '2018-07-26T02:48:37Z'
        },
        relationships: {
          video_consultation_participants: {
            data: [
              {
                id: '1',
                type: 'video_consultation_participants'
              }
            ]
          }
        }
      },
      {
        id: '33',
        type: 'mobile_users',
        attributes: {
          uuid: 'f22462a2-4a0d-4e8b-8762-9b677284d290',
          account_name: 'dummy_33',
          email: 'user103@fake.com',
          created_at: '2018-07-26T02:48:37Z',
          updated_at: '2018-07-26T02:48:37Z',
          invitation_sent_at: null,
          invitation_accepted_at: null,
          disabled: false,
          disabled_at: null,
          password_expired_at: '2018-10-24T02:48:37Z'
        },
        relationships: {
          devices: {
            data: []
          },
          user_role: {
            data: null
          },
          profile: {
            data: {
              id: '103',
              type: 'profiles'
            }
          },
          language: {
            data: {
              id: '1',
              type: 'languages'
            }
          },
          patient: {
            data: null
          },
          threads: {
            data: []
          },
          site_member: {
            data: null
          },
          overall_status: {
            data: null
          }
        }
      }
    ]
  };

  t.context.postResponse = {
    data: {
      id: '1',
      type: 'video_consultation_participants',
      attributes: {
        created_at: '2018-07-26T02:48:37Z',
        updated_at: '2018-07-26T02:48:37Z'
      },
      relationships: {
        video_consultation: {
          data: {
            id: '1',
            type: 'video_consultations'
          }
        },
        participant: {
          data: {
            id: '33',
            type: 'mobile_users'
          }
        }
      }
    },
    included: [
      {
        id: '1',
        type: 'video_consultations',
        attributes: {
          name: 'General Consultation',
          start_at: '2018-07-26T02:48:08Z',
          end_at: null,
          status: null,
          confirmed_at: null,
          deleted_at: null,
          created_at: '2018-07-26T02:48:37Z',
          updated_at: '2018-07-26T02:48:37Z'
        },
        relationships: {
          video_consultation_participants: {
            data: [
              {
                id: '1',
                type: 'video_consultation_participants'
              }
            ]
          }
        }
      },
      {
        id: '33',
        type: 'mobile_users',
        attributes: {
          uuid: 'f22462a2-4a0d-4e8b-8762-9b677284d290',
          account_name: 'dummy_33',
          email: 'user103@fake.com',
          created_at: '2018-07-26T02:48:37Z',
          updated_at: '2018-07-26T02:48:37Z',
          invitation_sent_at: null,
          invitation_accepted_at: null,
          disabled: false,
          disabled_at: null,
          password_expired_at: '2018-10-24T02:48:37Z'
        },
        relationships: {
          devices: {
            data: []
          },
          user_role: {
            data: null
          },
          profile: {
            data: {
              id: '103',
              type: 'profiles'
            }
          },
          language: {
            data: {
              id: '1',
              type: 'languages'
            }
          },
          patient: {
            data: null
          },
          threads: {
            data: []
          },
          site_member: {
            data: null
          },
          overall_status: {
            data: null
          }
        }
      }
    ]
  };

  t.context.participantJsonApi = {
    data: {
      id: 1,
      type: 'video_consultation_participants',
      attributes: {},
      relationships: {
        video_consultation: {
          data: {
            id: 1,
            type: 'video_consultations'
          }
        },
        participant: {
          data: {
            id: 33,
            type: 'mobile_users'
          }
        }
      }
    }
  };
});

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.participant = new ConsultationParticipant(t.context.participantJsonApi);
});


// ConsultationParticipantService.delete method
/**
 * @test {Clinical6.delete}
 */
test('[unit] ConsultationParticipantService.delete should throw errors for invalid parameters', async (t) => {
  const title = `ConsultationParticipantService.delete error`;
  await t.throwsAsync(clinical6.delete(new ConsultationParticipant()), `${title}: video_consultation_participant does not have id`);
});

/**
 * @test {Clinical6.delete}
 */
test('[unit] ConsultationParticipantService.delete should receive a valid response for a delete request', async (t) => {
  const { participant } = t.context;
  participant.id = 5;
  let request = {};
  nock(client.apiBaseUrl).delete(`/v3/video_consultation_participants/${participant.id}`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, ''];
  });
  const response = await clinical6.delete(participant);
  t.is(request.path, `/v3/video_consultation_participants/${participant.id}`);
  t.is(request.headers.accept, 'application/json');
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');

  t.falsy(response);
});

/**
 * @test {Clinical6.delete}
 */
test.serial('[unit] ConsultationParticipantService.delete should remove the element from local storage', async (t) => {
  const { participant, storage } = t.context;
  participant.id = 5;
  nock(client.apiBaseUrl).delete(`/v3/video_consultation_participants/${participant.id}`).reply(200, '');
  await storage.set(participant.type, participant.toJSON(), { id: participant.id });
  await clinical6.delete(participant);
  t.is(storage.has(participant.type, { id: participant.id }), false);
});


// ConsultationParticipantService.get method
/**
 * @test {Clinical6.get}
 */
test.serial('[unit] ConsultationParticipantService.get should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const expectedError = 'ConsultationParticipantService.get error: requires authToken';
  await t.throwsAsync(clinical6.get(ConsultationParticipant), expectedError);
});

// /**
//  * @test {Clinical6.get}
//  */
// test('[unit] ConsultationParticipantService.get should make a properly formatted get request and response without an id', async (t) => {
//   const { getResponseAll } = t.context;
//   let request = {};
//   nock(client.apiBaseUrl).get('/v3/video_consultation_participants').reply(function () {
//     request = this.req;
//     return [200, getResponseAll];
//   });
//   const response = await clinical6.get(ConsultationParticipant);
//   t.is(request.path, `/v3/video_consultation_participants`);
//   t.is(request.headers.accept, 'application/json');
//   t.is(request.headers['content-type'], 'application/json');
//   t.is(request.headers.authorization, 'Token token=valid_token');
//   t.truthy(response);
//   t.is(Object.keys(response).length, 5);
// });

// /**
//  * @test {Clinical6.get}
//  */
// test('[unit] ConsultationParticipantService.get should receive a valid response for a get request with an id', async (t) => {
//   const { getResponseId } = t.context;
//   nock(client.apiBaseUrl).get('/v3/video_consultation_participants/4').reply(200, getResponseId);
//   const response = await clinical6.get(new ConsultationParticipant({ id: 4 }));
//   t.truthy(response);
//   t.is(response.id, 23);
//   t.is(response.type, 'video_consultations');
//   t.is(response.createdAt, '2018-04-17T16:14:08Z');
//   t.is(response.updatedAt, '2018-04-17T16:14:08Z');
//   t.is(response.name, 'name-25');
//   t.is(response.participantType, 'dynamic');
// });


// ConsultationParticipantService.insert method
/**
 * @test {Clinical6.insert}
 */
test.serial('[unit] ConsultationParticipantService.insert should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const expectedError = 'ConsultationParticipantService.insert error: requires authToken';
  await t.throwsAsync(clinical6.insert(new ConsultationParticipant()), expectedError);
});

/**
 * @test {Clinical6.insert}
 */
test('[unit] ConsultationParticipantService.insert should successfully insert a participant with a participant object', async (t) => {
  const { postResponse } = t.context;
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/video_consultation_participants`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, postResponse];
  });

  const requestJsonApi = {
    data: {
      type: 'video_consultation_participants',
      attributes: {},
      relationships: {
        video_consultation: {
          data: {
            id: 1,
            type: 'video_consultations'
          }
        },
        participant: {
          data: {
            id: 33,
            type: 'mobile_users'
          }
        }
      }
    }
  };

  const participant = new ConsultationParticipant();
  participant.consultation = new Consultation({ id: 1 });
  participant.user = new User({ id: 33, type: 'mobile_users' });
  const response = await clinical6.insert(participant);

  t.is(request.path, `/v3/video_consultation_participants`);
  t.is(request.headers.accept, 'application/json');
  t.deepEqual(request.requestBody, requestJsonApi);
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');
  t.is(response.id, 1);
  t.is(response.type, 'video_consultation_participants');
  t.is(response.createdAt, '2018-07-26T02:48:37Z');
  t.is(response.updatedAt, '2018-07-26T02:48:37Z');
});

// ConsultationParticipantService.update method
/**
 * @test {Clinical6.update}
 */
test('[unit] ConsultationParticipantService.update should successfully update a participant with a participant object', async (t) => {
  const { participant, patchResponse, participantJsonApi } = t.context;
  let request = {};
  nock(client.apiBaseUrl).patch(/\/v3\/video_consultation_participants\/([0-9]*)$/).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, patchResponse];
  });
  const response = await clinical6.update(participant);

  t.is(request.path, `/v3/video_consultation_participants/${participant.id}`);
  t.is(request.headers.accept, 'application/json');
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');
  t.deepEqual(request.requestBody, participantJsonApi);
  t.is(response.id, 1);
  t.is(response.type, 'video_consultation_participants');
});