test/unit/services.site-language.js
import test from 'ava';
import nock from 'nock';
import { client, SiteLanguage, clinical6 } from '../../src';
test.before('start server', (t) => {
t.context.storage = client.storageUtility;
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.siteLanguageJsonApi = {
data: {
type: 'trials__site_supported_languages',
relationships: {
language: {
data: {
id: 29,
type: 'languages'
}
},
site: {
data: {
id: 29,
type: 'trials__sites'
}
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.siteLanguage = new SiteLanguage(t.context.siteLanguageJsonApi);
});
// SiteLanguageService.delete method
/**
* @test {Clinical6.delete}
*/
test('[unit] SiteLanguageService.delete should throw errors for invalid parameters', async (t) => {
const title = `SiteLanguageService.delete error`;
await t.throwsAsync(clinical6.delete(new SiteLanguage()), `${title}: site_supported_language does not have id`);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] SiteLanguageService.delete should receive a valid response for a delete request', async (t) => {
const { siteLanguage } = t.context;
siteLanguage.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/trials/site_supported_languages/${siteLanguage.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [204, ''];
});
const response = await clinical6.delete(siteLanguage);
t.is(request.path, `/v3/trials/site_supported_languages/${siteLanguage.id}`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.falsy(response);
});
/**
* @test {Clinical6.delete}
*/
test.serial('[unit] SiteLanguageService.delete should remove the element from local storage', async (t) => {
const { siteLanguage, storage } = t.context;
siteLanguage.id = 5;
nock(client.apiBaseUrl).delete(`/v3/trials/site_supported_languages/${siteLanguage.id}`).reply(204, '');
await storage.set(siteLanguage.type, siteLanguage.toJSON(), { id: siteLanguage.id });
await clinical6.delete(siteLanguage);
t.is(storage.has(siteLanguage.type, { id: siteLanguage.id }), false);
});
// SiteLanguageService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] SiteLanguageService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'SiteLanguageService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new SiteLanguage()), expectedError);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] SiteLanguageService.insert should successfully insert a siteLanguage with a siteLanguage object', async (t) => {
let request = {};
nock(client.apiBaseUrl).post(`/v3/trials/site_supported_languages`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, {
data: {
id: 1,
type: 'trials__site_supported_languages',
attributes: {
created_at: '2018-05-15T05:00:43Z',
updated_at: '2018-05-15T05:00:43Z'
},
relationships: {
language: {
data: {
id: 29,
type: 'languages'
}
},
site: {
data: {
id: 29,
type: 'trials__sites'
}
}
}
},
included: [
{
id: 29,
type: 'languages',
attributes: {
iso: 'dummy_2449',
name: 'provident',
is_default: false
}
}
]
}];
});
const requestJsonApi = {
data: {
type: 'trials__site_supported_languages',
attributes: {},
relationships: {
language: {
data: {
id: 29,
type: 'languages'
}
},
site: {
data: {
id: 29,
type: 'trials__sites'
}
}
}
}
};
const siteLanguage = new SiteLanguage(requestJsonApi);
const response = await clinical6.insert(siteLanguage);
t.is(request.path, `/v3/trials/site_supported_languages`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, requestJsonApi);
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 1);
t.is(response.type, 'trials__site_supported_languages');
t.is(response.createdAt, '2018-05-15T05:00:43Z');
t.is(response.updatedAt, '2018-05-15T05:00:43Z');
t.is(response.language.id, 29);
t.is(response.language.type, 'languages');
t.is(response.language.iso, 'dummy_2449');
t.is(response.language.name, 'provident');
t.is(response.language.isDefault, false);
t.is(response.site.id, 29);
t.is(response.site.type, 'trials__sites');
});