Manual Reference Source Test

test/unit/helpers.platform-menu.js

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

// See tests.js for testing client creation
const platformMenuJsonApi = {
  id: 322,
  type: 'menus',
  attributes: {
    icon: 'icon-star',
    title: 'vitae',
    position: 2,
    enabled: true,
    url: '#',
    access_class: null
  }
};
const platformMenuJson = {
  id: 322,
  type: 'menus',
  icon: 'icon-star',
  title: 'vitae',
  position: 2,
  enabled: true,
  url: '#',
  access_class: null
};

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/menus/*`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]);


  t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/menus`,
    [201, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: 322,
        type: 'menus',
        attributes: {
          icon: 'icon-star',
          title: 'vitae',
          position: 2,
          enabled: true,
          url: '#',
          access_class: null
        },
        relationships: {
          parent: {
            data: null
          },
          authorizable: {
            data: null
          },
          children: {
            data: []
          }
        }
      }
    })]);

  t.context.server.respondWith('PATCH', `${client.apiBaseUrl}/v3/menus/*`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: '330',
        type: 'menus',
        attributes: {
          icon: 'icon-star',
          title: 'vitae',
          position: 1,
          enabled: true,
          url: '#',
          access_class: null
        },
        relationships: {
          parent: {
            data: null
          },
          authorizable: {
            data: null
          },
          children: {
            data: []
          }
        }
      }
    })]);

  t.context.storage = client.storageUtility;
  client.platformMenu = new PlatformMenu({});
  t.context.platformMenuJsonApi = {
    data: {
      type: 'menus',
      attributes: {
        title: 'dummy_1065',
        description: 'Demo Content info...',
        redemption_points: 250
      }
    }
  };
  t.context.platformMenu = new PlatformMenu(t.context.platformMenuJsonApi);
});

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

/**
 * @test {PlatformMenu}
 */
test('[unit] PlatformMenu should handle platformMenu data with a normal json format', (t) => {
  const platformMenu = new PlatformMenu(platformMenuJson);
  t.is(platformMenu.id, 322);
  t.is(platformMenu.type, 'menus');
  t.is(platformMenu.title, 'vitae');
  t.is(platformMenu.position, 2);
  t.is(platformMenu.enabled, true);
  t.is(platformMenu.url, '#');
  t.is(platformMenu.accessClass, null);
});

/**
 * @test {PlatformMenu}
 */
test('[unit] PlatformMenu should handle platformMenu data with json api format', (t) => {
  const platformMenu = new PlatformMenu({ data: platformMenuJsonApi });
  t.is(platformMenu.id, 322);
  t.is(platformMenu.type, 'menus');
  t.is(platformMenu.title, 'vitae');
  t.is(platformMenu.position, 2);
  t.is(platformMenu.enabled, true);
  t.is(platformMenu.url, '#');
  t.is(platformMenu.accessClass, null);
});

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

  platformMenu = new PlatformMenu({ data: platformMenuJsonApi });
  t.deepEqual(platformMenu.toJSON(), platformMenuJsonApi);
});

/**
 * @test {PlatformMenu.delete}
 */
test('[unit] PlatformMenu.delete should successfully delete a platformMenu', async (t) => {
  const platformMenu = new PlatformMenu({ data: platformMenuJsonApi });
  const response = await platformMenu.delete();

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

/**
 * @test {PlatformMenu.save}
 */
test('[unit] PlatformMenu.save should successfully insert a platformMenu with a platformMenu object when id does not exist', async (t) => {
  const json = JSON.parse(JSON.stringify(platformMenuJsonApi));
  delete json.id;
  const platformMenu = new PlatformMenu({ data: json });
  const response = await platformMenu.save();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/menus`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.deepEqual(JSON.parse(request.requestBody), { data: json });
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.is(response.id, 322);
  t.is(response.type, 'menus');
  t.is(response.authorizable, null);
});

/**
 * @test {PlatformMenu.save}
 */
test('[unit] PlatformMenu.save should successfully update a platformMenu with a platformMenu object when id exists', async (t) => {
  const platformMenu = new PlatformMenu({ data: platformMenuJsonApi });
  const response = await platformMenu.save();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'PATCH');
  // t.is(request.url, `${client.apiBaseUrl}/v3/menus/${platformMenu.id}`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.deepEqual(JSON.parse(request.requestBody), { data: platformMenuJsonApi });
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.is(response.id, 330);
  t.is(response.type, 'menus');
  t.is(response.authorizable, null);
});