Manual Reference Source Test

test/unit/helpers.content.js

import test from 'ava';
import nock from 'nock';
import { client, Content, ContentType } from '../../src';

test.before('start server', () => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
  const type = new ContentType({
    id: '35',
    type: 'dynamic_content__content_types',
    attributes: {
      name: 'dummy_407',
      permanent_link: 'home_health_visit',
      description: null,
      created_at: '2017-07-28T15:30:39Z',
      updated_at: '2017-07-28T15:30:39Z'
    }
  });
  type.store();
});

test.beforeEach((t) => {
  client.authToken = 'valid_token';
  t.context.content = new Content('permanent_link', { id: 20 });

  nock(client.apiBaseUrl).delete(/\/v3\/dynamic_content\/contents\/([0-9]*)$/).reply(200, '');

  nock(client.apiBaseUrl).post('/v3/dynamic_content/contents')
    .reply(201, {
      data: {
        id: '24',
        type: 'dynamic_content__contents',
        attributes: {
          heart_rate: 120,
          title: 'new title',
          description: null,
          image_url: '/default/default.png',
          position: 1,
          created_at: '2017-07-28T15:30:40Z',
          updated_at: '2017-07-28T15:30:40Z'
        },
        relationships: {
          content_type: {
            data: {
              id: '35',
              type: 'dynamic_content__content_types'
            }
          }
        }
      },
      included: [
        {
          id: '35',
          type: 'dynamic_content__content_types',
          attributes: {
            name: 'dummy_415',
            permanent_link: 'home_health_visit',
            description: null,
            created_at: '2017-07-28T15:30:40Z',
            updated_at: '2017-07-28T15:30:40Z'
          }
        }
      ]
    });

  nock(client.apiBaseUrl).patch('/v3/dynamic_content/contents/70')
    .reply(204, {
      data: {
        type: 'dynamic_content__contents',
        id: 70,
        attributes: {
          title: 'new',
          heart_rate: 100,
          visibility_status: 'hidden'
        }
      },
      format: 'json'
    });

  // 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=dynamic_content/content&commentable_id=23`,
  //   [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.storage = client.storageUtility;
  t.context.contentJsonApi = {
    data: {
      id: 755256,
      type: 'dynamic_content__contents',
      attributes: {
        created_at: '2016-05-12T22:06:38Z',
        description: 'Workplace as a Service',
        position: 2,
        title: 'WPaaS',
        tags: [],
        content_type: 'acronyms'
      },
      relationships: {
        content_type: {
          data: {
            id: '183',
            type: 'dynamic_content__content_types'
          }
        }
      }
    }
  };

  t.context.contentUpdateJsonApi = {
    type: 'dynamic_content__contents',
    id: 70,
    attributes: {
      title: 'new',
      heart_rate: 100,
      visibility_status: 'hidden'
    }
  };
  t.context.content = new Content(t.context.contentJsonApi);
});

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

/**
 * @test {Content.addComment}
 */
test('[unit] Content.addComment should exist', (t) => {
  const { content } = t.context;
  t.truthy(content.addComment);
});

/**
 * @test {Content.delete}
 */
test('[unit] Content.delete should successfully delete a site', async (t) => {
  const { content } = t.context;
  const response = await content.delete();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'DELETE');
  // t.is(request.url, `${client.apiBaseUrl}/v3/dynamic_content/contents/${content.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.falsy(response);
});


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

/**
 * @test {Content.save}
 */
test('[unit] Content.save should also insert if it does not exist', async (t) => {
  // const { server } = t.context;
  const content = new Content(); // no id
  content.set('title', 'new title');
  content.set('heart_rate', 120);
  content.contentType = 'home_health_visit';
  // console.log('---content', content);
  // const response = await new Promise((resolve) => {
  //   content.save().then(d => resolve(d));
  //   server.respond();
  // });
  const response = await content.save().catch(e => e);
  // server.respond();
  // const request = server.requests[0];
  // // console.log('---response', response);
  // // console.log('---request', request);
  // // console.log('---server', server);

  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/dynamic_content/contents`);
  // 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.is(request.requestBody, JSON.stringify({
  //   data: {
  //     type: 'dynamic_content__contents',
  //     attributes: {
  //       title: 'new title',
  //       heart_rate: 120
  //     },
  //     relationships: {
  //       content_type: {
  //         data: {
  //           id: 35,
  //           type: 'dynamic_content__content_types'
  //         }
  //       }
  //     }
  //   }
  // }));

  t.is(response.title, 'new title');
  // console.log(response.contentType);
  t.is(response.contentType.permanentLink, 'home_health_visit');
  t.is(response.id, 24);

  // Make sure content.id is now 1
  t.is(content.id, 24);
});

/**
 * @test {Content.save}
 */
test('[unit] Content.save should also update if ID exists', async (t) => {
  const content = new Content({ id: 70 });
  content.set('title', 'new');
  content.set('heart_rate', 100);
  content.set('visibility_status', 'hidden');
  content.contentType = 'home_health_visit';
  const response = await content.save().catch(e => e);
  t.is(response.title, 'new');
  t.is(response.heartRate, 100);
  t.is(response.visibilityStatus, 'hidden');
  t.is(response.id, 70);
});

// /**
//  * @test {Content.addComment}
//  */
// test('[unit] Content.addComment should return a comment object', async (t) => {
//   const { content } = t.context;
//   const response = await content.addComment('Some Comment');
//   const { method, url } = t.context.server.requests[0];
//   t.is(method, 'POST');
//   t.is(url, `${client.apiBaseUrl}/api/v2/comments`);
//   t.is(response.id, 1);
// });

// /**
//  * @test {Content.getComments}
//  */
// test('[unit] Content.getComments should return an array of comment objects', async (t) => {
//   const { content } = t.context;
//   const response = await content.getComments();
//   const { method, url } = t.context.server.requests[0];
//   t.is(method, 'GET');
//   t.is(url, `${client.apiBaseUrl}/v3/discuss/threads?filters[commentable_type]=dynamic_content__contents&filters[commentable_id]=23`);
//   t.is(response[0].id, 1);
// });