test/unit/services.filter-group.js
import test from 'ava';
import nock from 'nock';
import { client, FilterGroup, clinical6 } from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.filterGroupJsonApi = {
data: {
type: 'filter_groups',
attributes: {
operator: 'and'
},
relationships: {
cohort: {
data: {
id: 74,
type: 'cohorts'
}
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.filterGroup = new FilterGroup(t.context.filterGroupJsonApi);
});
// FilterGroupService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] FilterGroupService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'FilterGroupService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new FilterGroup()), expectedError);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] FilterGroupService.insert should successfully insert a filterGroup with a filterGroup object', async (t) => {
let request = {};
nock(client.apiBaseUrl).post(`/v3/filter_groups`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, {
data: {
id: 5,
type: 'filter_groups',
attributes: {
operator: 'and',
created_at: '2018-04-30T21:39:05Z',
updated_at: '2018-04-30T21:39:05Z'
},
relationships: {
cohort: {
data: {
id: 74,
type: 'cohorts'
}
}
}
}
}];
});
const requestJsonApi = {
data: {
type: 'filter_groups',
attributes: {
operator: 'and'
},
relationships: {
cohort: {
data: {
id: 74,
type: 'cohorts'
}
}
}
}
};
const filterGroup = new FilterGroup(requestJsonApi);
const response = await clinical6.insert(filterGroup);
t.is(request.path, `/v3/filter_groups`);
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, 5);
t.is(response.type, 'filter_groups');
t.is(response.operator, 'and');
t.is(response.createdAt, '2018-04-30T21:39:05Z');
t.is(response.updatedAt, '2018-04-30T21:39:05Z');
t.is(response.cohort.id, 74);
t.is(response.cohort.type, 'cohorts');
});