Manual Reference Source Test

test/unit/helpers.notification.js

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

// See tests.js for testing client creation
const notificationJsonApi = {
  id: 5,
  type: 'notification__deliveries',
  attributes: {
    action: null,
    action_object: null,
    channels: [
      'push notification'
    ],
    message: 'Some message',
    status: 'pending',
    title: 'Some title',
    opts: null,
    created_at: '2017-11-02T21:04:57Z',
    updated_at: '2017-11-02T21:04:57Z',
    archived_at: '2017-11-02T21:04:57Z'
  }
};
const notificationJson = {
  id: '5',
  type: 'notification__deliveries',
  action: null,
  action_object: null,
  channels: [
    'push notification'
  ],
  message: 'Some message',
  status: 'pending',
  title: 'Some title',
  opts: null,
  created_at: '2017-11-02T21:04:57Z',
  updated_at: '2017-11-02T21:04:57Z',
  archived_at: '2017-11-02T21:04:57Z'
};

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.server = sinon.fakeServer.create();
  t.context.server.autoRespond = true;

  t.context.server.respondWith('DELETE', `${client.apiBaseUrl}/v3/notification/deliveries/:id`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]);

  t.context.server.respondWith('PATCH', `${client.apiBaseUrl}/v3/notification/deliveries/:id`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: '6',
        type: 'notification__deliveries',
        action: null,
        actionObject: null,
        channels: [
          'push notification'
        ],
        message: 'Some message',
        status: 'read',
        title: 'Some title',
        opts: null,
        created_at: '2017-11-02T21:04:57Z',
        updated_at: '2017-11-02T21:04:57Z',
        archived_at: '2017-11-02T21:04:57Z'
      }
    })]);

  t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/:resource/:resource_id/notification/deliveries/:id`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: '5',
        type: 'notification__deliveries',
        action: null,
        actionObject: null,
        channels: [
          'push notification'
        ],
        message: 'Some message',
        status: 'pending',
        title: 'Some title',
        opts: null,
        created_at: '2017-11-02T21:04:57Z',
        updated_at: '2017-11-02T21:04:57Z',
        archived_at: '2017-11-02T21:04:57Z'
      }
    })]);

  t.context.storage = client.storageUtility;
  client.notification = new Notification({});
  t.context.notificationJsonApi = {
    data: {
      type: 'notification__deliveries',
      action: null,
      action_object: null,
      channels: [
        'push notification'
      ],
      message: 'Some message',
      status: 'read',
      title: 'Some title',
      opts: null
    }
  };
  t.context.notification = new Notification(t.context.notificationJsonApi);
});

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

/**
 * @test {Notification}
 */
test('[unit] Notification should handle notification data with a normal json format', (t) => {
  const notification = new Notification(notificationJson);
  t.is(notification.id, 5);
  t.is(notification.type, 'notification__deliveries');
  t.is(notification.action, null);
  t.is(notification.actionObject, null);
  t.is(notification.channels[0], 'push notification');
  t.is(notification.message, 'Some message');
  t.is(notification.status, 'pending');
  t.is(notification.title, 'Some title');
  t.is(notification.opts, null);
  t.is(notification.createdAt, '2017-11-02T21:04:57Z');
  t.is(notification.updatedAt, '2017-11-02T21:04:57Z');
  t.is(notification.archivedAt, '2017-11-02T21:04:57Z');
});

/**
 * @test {Notification}
 */
test('[unit] Notification should handle notification data with json api format', (t) => {
  const notification = new Notification({ data: notificationJsonApi });
  t.is(notification.id, 5);
  t.is(notification.type, 'notification__deliveries');
  t.is(notification.action, null);
  t.is(notification.actionObject, null);
  t.is(notification.channels[0], 'push notification');
  t.is(notification.message, 'Some message');
  t.is(notification.status, 'pending');
  t.is(notification.title, 'Some title');
  t.is(notification.opts, null);
  t.is(notification.createdAt, '2017-11-02T21:04:57Z');
  t.is(notification.updatedAt, '2017-11-02T21:04:57Z');
  t.is(notification.archivedAt, '2017-11-02T21:04:57Z');
});

/**
 * @test {notification}
 */
test('[unit] notification should generate json api format when converted to string', (t) => {
  let notification = new Notification({ data: notificationJsonApi });
  t.deepEqual(notification.toJSON(), notificationJsonApi);

  notification = new Notification({ data: notificationJsonApi });
  t.deepEqual(notification.toJSON(), notificationJsonApi);
});

/**
 * @test {notification.delete}
 */
test('[unit] notification.delete should successfully delete a notification', async (t) => {
  const notification = new Notification({ data: notificationJsonApi });
  const response = await notification.delete();
  t.truthy(response);
  t.is(response.status, 'ok');
});

/**
 * @test {notification.save}
 */
test('[unit] notification.save should successfully update a notification with a notification object when id exists', async (t) => {
  const notification = new Notification({ data: notificationJsonApi });
  const response = await notification.save();
  t.is(response.id, 6);
  t.is(response.type, 'notification__deliveries');
});

/**
 * @test {notification.save}
 */
test('[unit] notification.save should throw an error when there is no notification id', async (t) => {
  const notification = new Notification();
  const expectedError = 'Notification.save error: notification does not have id';
  await t.throwsAsync(notification.save(), expectedError);
});

/**
 * @test {notification.read}
 */
test('[unit] notification.read should successfully update a notification status to read', async (t) => {
  const notification = new Notification({ data: notificationJsonApi });
  const response = await notification.read();
  t.is(response.id, 6);
  t.is(response.type, 'notification__deliveries');
});