Manual Reference Source Test

test/unit/services.setting.v3.js

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

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

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

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

  t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/settings`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: [
        {
          id: 'advertisements',
          type: 'settings',
          attributes: {
            value: {
              display_time: 5,
              transition_time: 1
            }
          }
        },
        {
          id: 'timeout',
          type: 'settings',
          attributes: {
            value: 30
          }
        },
        {
          id: 'logo',
          type: 'settings',
          attributes: {
            value: 'logo.png'
          }
        },
        {
          id: 'original_default_image',
          type: 'settings',
          attributes: {
            value: 'default.png'
          }
        },
        {
          id: 'locale',
          type: 'settings',
          attributes: {
            value: 'en'
          }
        },
        {
          id: 'log_active',
          type: 'settings',
          attributes: {
            value: 'enabled'
          }
        },
        {
          id: 'sign_up_enabled',
          type: 'settings',
          attributes: {
            value: 'enabled'
          }
        },
        {
          id: 'guests_enabled',
          type: 'settings',
          attributes: {
            value: 'enabled'
          }
        }
      ]
    })]);

  t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/settings/*`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      id: 'log_active',
      type: 'settings',
      attributes: {
        value: 'enabled'
      }
    })]);

  t.context.server.respondWith('PATCH', `${client.apiBaseUrl}/v3/settings/*`,
    [200, { 'Content-Type': 'application/json' }, JSON.stringify({
      data: {
        id: 'logo',
        type: 'settings',
        attributes: {
          value: 'new logo url'
        }
      }
    })]);

  t.context.storage = client.storageUtility;
  client.setting = new Setting({});
  t.context.settingJsonApi = {
    data: {
      id: 'advertisements',
      type: 'settings',
      attributes: {
        value: {
          display_time: 5,
          transition_time: 1
        }
      }
    }
  };
  t.context.setting = new Setting(t.context.settingJsonApi);
});

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

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

// /**
//  * @test {Clinical6.get}
//  */
// test('[unit] SettingService.get should make a properly formatted get request', async (t) => {
//   await clinical6.get(Setting);
//   const request = t.context.server.requests[0];
//   t.is(request.method, 'GET');
//   t.is(request.url, `${client.apiBaseUrl}/v3/settings`);
//   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');
// });

/**
 * @test {Clinical6.get}
 */
test('[unit] SettingService.get should receive a valid response for a get request without an id', async (t) => {
  const response = await clinical6.get(Setting);
  t.truthy(response);
  t.is(Object.keys(response).length, 8);
  t.is(response[0].id, 'advertisements');
  t.is(response[0].type, 'settings');
  t.deepEqual(response[0].value, { displayTime: 5, transitionTime: 1 });
  t.is(response[1].id, 'timeout');
  t.is(response[1].type, 'settings');
  t.is(response[1].value, 30);
  t.is(response[2].id, 'logo');
  t.is(response[2].type, 'settings');
  t.is(response[2].value, 'logo.png');
});

/**
 * @test {Clinical6.get}
 */
// test('[unit] SettingService.get should receive a valid response for a get request with an id', async (t) => {
//   const response = await clinical6.get(new Setting({ id: 15 }));
//   t.truthy(response);
//   t.is(response.id, 15);
//   t.is(response.type, 'settings');
//   t.is(response.settingId, '922176');
//   t.is(response.name, 'Frankie Hermann');
//   t.is(response.email, 'lisa@kulas.info');
//   t.falsy(response.phoneNumber);
//   t.falsy(response.faxNumber);
// });


// SettingService.update method
/**
 * @test {Clinical6.update}
 */
test.serial('[unit] SettingService.update should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const title = 'SettingService.update error';
  await t.throwsAsync(clinical6.update(new Setting({ id: 5 })), `${title}: requires authToken`);
});

/**
 * @test {Clinical6.update}
 */
test('[unit] SettingService.update should successfully update a setting with a setting object', async (t) => {
  const setting = new Setting('logo', 'new logo url');
  // const settingJsonApi = {
  //   data: {
  //     id: 'logo',
  //     type: 'settings',
  //     attributes: {
  //       value: 'new logo url'
  //     }
  //   }
  // };
  const response = await clinical6.update(setting);

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'PATCH');
  // t.is(request.url, `${client.apiBaseUrl}/v3/settings/${setting.id}`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.deepEqual(JSON.parse(request.requestBody), settingJsonApi);
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.is(response.id, 'logo');
  t.is(response.type, 'settings');
  t.is(response.value, 'new logo url');
});