test/unit/services.status.js
import test from 'ava';
import nock from 'nock';
import { client, statusService, FlowDataGroup } from '../../src';
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
});
test.after('server shut down', () => {});
test.beforeEach(() => {
client.cache = 'never';
client.authToken = 'valid_token';
});
// StatusService.check method
/**
* @test {StatusService.check}
*/
test.serial('[unit] StatusService.check should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const title = `StatusService.check error`;
await t.throwsAsync(statusService.check(new FlowDataGroup()), `${title}: requires authToken and obj does not have id`);
});
/**
* @test {StatusService.check}
*/
test('[unit] StatusService.check should successfully check a site with a site object', async (t) => {
const checkResponse = {
data: {
id: '73',
type: 'statuses',
attributes: {
value: 'initial'
},
relationships: {
owner: {
data: null
},
statusable: {
data: {
id: '49',
type: 'data_collection__captured_value_groups'
}
}
}
}
};
let request = {};
nock(client.apiBaseUrl).post(`/v3/statuses/check`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, checkResponse];
});
const requestJsonApi = {
data: {
id: 49,
type: 'data_collection__captured_value_groups',
attributes: {
}
}
};
const fdg = new FlowDataGroup({ id: 49 });
const response = await statusService.check(fdg);
t.is(request.path, `/v3/statuses/check`);
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, 73);
t.is(response.type, 'statuses');
t.is(response.statusable.id, '49');
t.is(response.statusable.type, 'data_collection__captured_value_groups');
});