Manual Reference Source Test

test/unit/helpers.profile.js

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

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

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;
  t.context.server.respondWith('PATCH', `${client.apiBaseUrl}/v3/mobile_users/*/profile`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: '167',
        type: 'profiles',
        attributes: {
          height: 180,
          diabetes: true,
          cancer: false,
          created_at: '2017-08-14T22:55:55Z',
          updated_at: '2017-08-14T22:55:55Z',
          first_name: 'Davion',
          middle_initial: null,
          last_name: 'Kunde',
          prefix: null,
          suffix: null,
          gender: 'male',
          state: 'VA',
          phone: '703',
          ethnicity: '',
          occupation: 'doctor',
          avatar: {
            url: '/uploads/test/profile/avatar/167/1.png',
            small: {
              url: '/uploads/test/profile/avatar/167/small_1.png'
            },
            main: {
              url: '/uploads/test/profile/avatar/167/main_1.png'
            },
            thumb: {
              url: '/uploads/test/profile/avatar/167/thumb_1.png'
            }
          },
          username: null,
          dob: null,
          street: null,
          city: null,
          zip_code: 20170,
          timezone: 'UTC'
        },
        relationships: {
          profileable: {
            data: {
              id: '164',
              type: 'mobile_users'
            }
          }
        }
      }
    })]);

  t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/v2/comments`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        type: 'comments',
        id: 1,
        attributes: {
          body: 'Some Comment',
          created_at: '2016-12-22T16:57:12.127Z'
        },
        relations: {
          author: {
            email: 'developer+8@parallel6.com',
            username: null,
            user_role: 'Physician'
          }
        }
      }
    })]);
  t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/v2/comments?commentable_type=profile&commentable_id=1560`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: [{
        type: 'comments',
        id: 1,
        attributes: {
          body: 'Some Comment',
          created_at: '2016-12-22T16:57:12.127Z'
        },
        relations: {
          author: {
            email: 'developer+8@parallel6.com',
            username: null,
            user_role: 'Physician'
          }
        }
      }]
    })]);
  // Reusable Flow
  const user = new User({ id: 164 });
  user.store();
  t.context.profile = new Profile({
    data: {
      id: '167',
      type: 'profiles',
      attributes: {
        height: 180,
        diabetes: true,
        cancer: false,
        created_at: '2017-08-14T22:55:55Z',
        updated_at: '2017-08-14T22:55:55Z',
        first_name: 'Davion',
        middle_initial: null,
        last_name: 'Kunde',
        prefix: null,
        suffix: null,
        state: 'VA',
        phone: '703',
        gender: 'male',
        ethnicity: '',
        occupation: 'doctor',
        avatar: {
          url: '/uploads/test/profile/avatar/167/1.png',
          small: {
            url: '/uploads/test/profile/avatar/167/small_1.png'
          },
          main: {
            url: '/uploads/test/profile/avatar/167/main_1.png'
          },
          thumb: {
            url: '/uploads/test/profile/avatar/167/thumb_1.png'
          }
        },
        username: null,
        dob: null,
        street: null,
        city: null,
        zip_code: null,
        timezone: 'UTC'
      },
      relationships: {
        profileable: {
          data: {
            id: '164',
            type: 'mobile_users'
          }
        }
      }
    }
  });
  t.context.profile.user = user;
});

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

// // Profile helper class
// /**
//  * @test {Profile.addComment}
//  */
// test('[unit] Profile.addComment method should exist', (t) => {
//   const { profile } = t.context;
//   t.truthy(profile.addComment);
// });

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

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

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

/**
 * @test {Profile.getFullName}
 */
test('[unit] Profile.getFullName method should exist', (t) => {
  const { profile } = t.context;
  t.truthy(profile.getFullName);
});

/**
 * @test {Profile.getFullAddress}
 */
test('[unit] Profile.getFullAddress method should exist', (t) => {
  const { profile } = t.context;
  t.truthy(profile.getFullAddress);
});

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

/**
 * @test {Profile.save}
 */
test('[unit] Profile.save method should return a promise', async (t) => {
  const { profile } = t.context;
  t.truthy(await profile.save().then);
});

/**
 * @test {Profile.save}
 */
test('[unit] Profile.save should successfully update user profile', async (t) => {
  const { profile } = t.context;
  profile.zip_code = '20170';
  const response = await profile.save();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'PATCH');
  // t.is(request.url, `${client.apiBaseUrl}/v3/mobile_users/${profile.user.id}/profile`);
  // t.is(request.requestHeaders.Accept, 'application/json');

  // // Check payload
  // t.deepEqual(JSON.parse(request.requestBody), { data: profile.toJSON() });
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.is(response.constructor.name, 'Profile');
  t.is(response.avatar.url, '/uploads/test/profile/avatar/167/1.png');
  t.is(response.avatar.small, '/uploads/test/profile/avatar/167/small_1.png');
  t.is(response.avatar.main, '/uploads/test/profile/avatar/167/main_1.png');
  t.is(response.avatar.thumb, '/uploads/test/profile/avatar/167/thumb_1.png');
  t.is(response.zipCode, 20170);
});

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

// /**
//  * @test {Profile.getComments}
//  */
// test('[unit] Profile.getComments should return an array of comment objects', async (t) => {
//   const { profile } = t.context;
//   const comments = await profile.getComments();
//   t.is(comments[0].id, 1);
// });