test/unit/helpers.consent-strategy.js
import test from 'ava';
import nock from 'nock';
import {
client,
ConsentStrategy
} from '../../src';
// See tests.js for testing client creation
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.insertResponse = {
data: {
id: 8,
type: 'consent__strategies',
attributes: {
name: 'A form name',
strategy_type: 'paper',
created_at: '2018-05-06T04:21:43Z',
updated_at: '2018-05-06T04:21:43Z'
},
relationships: {
cohort: {
data: {
id: 21,
type: 'cohorts'
}
},
forms: {
data: []
}
}
},
included: [
{
id: 21,
type: 'cohorts',
attributes: {
name: 'name-24',
cohort_type: 'static',
created_at: '2018-05-06T04:21:43Z',
updated_at: '2018-05-06T04:21:43Z'
}
}
]
};
t.context.consentStrategyJsonApi = {
type: 'consent__strategies',
attributes: {
name: 'A form name',
strategy_type: 'paper',
created_at: undefined,
updated_at: undefined
},
relationships: {
cohort: {
data: {
id: 21,
type: 'cohorts'
}
}
}
};
t.context.consentStrategyJson = {
type: 'consent__strategies',
name: 'A form name',
strategy_type: 'paper',
created_at: undefined,
updated_at: undefined,
relationships: {
cohort: {
data: {
id: 21,
type: 'cohorts'
}
}
}
};
t.context.consentStrategyPatchJsonApi = {
id: 6,
type: 'consent__strategies',
attributes: {
name: 'A new form name'
}
};
client.storageUtility.clear();
t.context.storage = client.storageUtility;
client.consentStrategy = new ConsentStrategy({});
t.context.consentStrategy = new ConsentStrategy({ data: t.context.consentStrategyJsonApi });
});
/**
* @test {ConsentStrategy}
*/
test('[unit] ConsentStrategy should handle consentStrategy data with a normal json format', (t) => {
const { consentStrategyJson } = t.context;
const consentStrategy = new ConsentStrategy(consentStrategyJson);
t.is(consentStrategy.type, 'consent__strategies');
t.is(consentStrategy.name, 'A form name');
t.is(consentStrategy.strategyType, 'paper');
t.is(consentStrategy.relationships.relationships.cohort.data.id, 21);
t.is(consentStrategy.relationships.relationships.cohort.data.type, 'cohorts');
});
/**
* @test {ConsentStrategy}
*/
test('[unit] ConsentStrategy should handle consentStrategy data with json api format', (t) => {
const { consentStrategyJsonApi } = t.context;
const consentStrategy = new ConsentStrategy({ data: consentStrategyJsonApi });
t.is(consentStrategy.type, 'consent__strategies');
t.is(consentStrategy.name, 'A form name');
t.is(consentStrategy.strategyType, 'paper');
t.is(consentStrategy.relationships.relationships.cohort.data.id, 21);
t.is(consentStrategy.relationships.relationships.cohort.data.type, 'cohorts');
});
/**
* @test {ConsentStrategy}
*/
test('[unit] ConsentStrategy should generate json api format when converted to string', (t) => {
const { consentStrategyJsonApi } = t.context;
let consentStrategy = new ConsentStrategy({ data: consentStrategyJsonApi });
t.deepEqual(consentStrategy.toJSON(), consentStrategyJsonApi);
consentStrategy = new ConsentStrategy({ data: consentStrategyJsonApi });
t.deepEqual(consentStrategy.toJSON(), consentStrategyJsonApi);
});
// ConsentStrategy.delete method
/**
* @test {ConsentStrategy.delete}
*/
test('[unit] ConsentStrategy.delete should successfully delete a consentStrategy', async (t) => {
const { consentStrategyJsonApi } = t.context;
const consentStrategy = new ConsentStrategy({ data: consentStrategyJsonApi });
consentStrategy.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/consent/strategies/${consentStrategy.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await consentStrategy.delete();
t.is(request.path, `/v3/consent/strategies/${consentStrategy.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 {ConsentStrategy.save}
*/
test('[unit] ConsentStrategy.save should successfully save a new consentStrategy when id does not exist', async (t) => {
const { consentStrategyJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(consentStrategyJsonApi));
// delete json.id;
const consentStrategy = new ConsentStrategy({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/strategies`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await consentStrategy.save();
t.is(request.path, `/v3/consent/strategies`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: json });
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 8);
t.is(response.type, 'consent__strategies');
t.is(response.name, 'A form name');
t.is(response.strategyType, 'paper');
t.is(response.createdAt, '2018-05-06T04:21:43Z');
t.is(response.updatedAt, '2018-05-06T04:21:43Z');
t.is(response.cohort.id, 21);
t.is(response.cohort.type, 'cohorts');
t.is(response.cohort.name, 'name-24');
t.is(response.cohort.cohortType, 'static');
t.is(response.cohort.createdAt, '2018-05-06T04:21:43Z');
t.is(response.cohort.updatedAt, '2018-05-06T04:21:43Z');
t.is(response.forms.length, 0);
});
/**
* @test {ConsentStrategy.save}
*/
test('[unit] ConsentStrategy.save should successfully update a consentStrategy with a consentStrategy object when an id exists', async (t) => {
const { consentStrategyPatchJsonApi } = t.context;
let requestBody = {};
const server = nock(client.apiBaseUrl)
.patch('/v3/consent/strategies/6', (body) => {
requestBody = body;
return { data: json };
})
.reply(201, {
data: {
id: 6,
type: 'consent__strategies',
attributes: {
name: 'A new form name',
strategy_type: 'electronic',
created_at: '2018-05-08T16:24:07Z',
updated_at: '2018-05-08T16:24:07Z'
},
relationships: {
cohort: {
data: {
id: 10,
type: 'cohorts'
}
},
forms: {
data: []
}
}
},
included: [
{
id: 10,
type: 'cohorts',
attributes: {
name: 'name-10',
cohort_type: 'static',
created_at: '2018-05-08T16:24:07Z',
updated_at: '2018-05-08T16:24:07Z'
}
}
]
});
const json = JSON.parse(JSON.stringify(consentStrategyPatchJsonApi));
const consentStrategy = new ConsentStrategy({ data: json });
const response = await consentStrategy.save();
t.deepEqual(requestBody, { data: json });
server.done();
t.is(response.id, 6);
t.is(response.type, 'consent__strategies');
t.is(response.name, 'A new form name');
t.is(response.strategyType, 'electronic');
t.is(response.createdAt, '2018-05-08T16:24:07Z');
t.is(response.updatedAt, '2018-05-08T16:24:07Z');
t.is(response.cohort.id, 10);
t.is(response.cohort.type, 'cohorts');
t.is(response.cohort.name, 'name-10');
t.is(response.cohort.cohortType, 'static');
t.is(response.cohort.createdAt, '2018-05-08T16:24:07Z');
t.is(response.cohort.updatedAt, '2018-05-08T16:24:07Z');
t.is(response.forms.length, 0);
});