test/unit/helpers.cohort-assignment.js
import test from 'ava';
import nock from 'nock';
import {
client,
CohortAssignment,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
t.context.updateResponse = {
data: {
id: '1',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-17T19:59:53Z',
updated_at: '2018-04-17T19:59:53Z'
},
relationships: {
user: {
data: {
id: '278',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '2',
type: 'cohorts'
}
}
}
}
};
t.context.insertResponse = {
data: {
id: '1',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-17T19:59:53Z',
updated_at: '2018-04-17T19:59:53Z'
},
relationships: {
user: {
data: {
id: '278',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '2',
type: 'cohorts'
}
}
}
}
};
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.cohortAssignmentJsonApi = {
id: 1,
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-03T14:16:25Z',
updated_at: '2018-04-03T14:16:25Z'
}
};
t.context.cohortAssignmentJson = {
id: 1,
type: 'cohort_assignments',
created_at: '2018-04-03T14:16:25Z',
updated_at: '2018-04-03T14:16:25Z'
};
t.context.cohortAssignment = new CohortAssignment(t.context.cohortAssignmentJsonApi);
});
/**
* @test {CohortAssignment}
*/
test('[unit] CohortAssignment should handle cohortAssignment data with a normal json format', (t) => {
const { cohortAssignmentJson } = t.context;
const cohortAssignment = new CohortAssignment(cohortAssignmentJson);
t.is(cohortAssignment.id, 1);
t.is(cohortAssignment.type, 'cohort_assignments');
t.is(cohortAssignment.createdAt, '2018-04-03T14:16:25Z');
t.is(cohortAssignment.updatedAt, '2018-04-03T14:16:25Z');
});
/**
* @test {CohortAssignment}
*/
test('[unit] CohortAssignment should handle cohortAssignment data with json api format', (t) => {
const { cohortAssignmentJsonApi } = t.context;
const cohortAssignment = new CohortAssignment({ data: cohortAssignmentJsonApi });
t.is(cohortAssignment.id, 1);
t.is(cohortAssignment.type, 'cohort_assignments');
t.is(cohortAssignment.createdAt, '2018-04-03T14:16:25Z');
t.is(cohortAssignment.updatedAt, '2018-04-03T14:16:25Z');
});
/**
* @test {CohortAssignment}
*/
test('[unit] CohortAssignment should generate json api format when converted to string', (t) => {
const { cohortAssignmentJsonApi } = t.context;
let cohortAssignment = new CohortAssignment({ data: cohortAssignmentJsonApi });
let json = cohortAssignment.toJSON();
t.deepEqual(json, cohortAssignmentJsonApi);
cohortAssignment = new CohortAssignment({ data: cohortAssignmentJsonApi });
json = cohortAssignment.toJSON();
t.deepEqual(json, cohortAssignmentJsonApi);
});
/**
* @test {CohortAssignment.delete}
*/
test('[unit] CohortAssignment.delete should successfully delete a cohort assignment', async (t) => {
const { cohortAssignment } = t.context;
cohortAssignment.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/cohort_assignments/${cohortAssignment.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await cohortAssignment.delete();
t.is(request.path, `/v3/cohort_assignments/${cohortAssignment.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);
});
// CohortAssignment.save
/**
* @test {CohortAssignment.save}
*/
test('[unit] CohortAssignment.save should successfully insert a cohortAssignment with a cohortAssignment object when id does not exist', async (t) => {
const { cohortAssignmentJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(cohortAssignmentJsonApi));
delete json.id;
const cohortAssignment = new CohortAssignment({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/cohort_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await cohortAssignment.save();
t.is(request.path, `/v3/cohort_assignments`);
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, 1);
t.is(response.type, 'cohort_assignments');
t.is(response.createdAt, '2018-04-17T19:59:53Z');
t.is(response.updatedAt, '2018-04-17T19:59:53Z');
});
/**
* @test {CohortAssignment.save}
*/
test('[unit] CohortAssignment.save should successfully update a cohortAssignment with a cohortAssignment object when id exists', async (t) => {
const { cohortAssignmentJsonApi, updateResponse } = t.context;
const cohortAssignment = new CohortAssignment({ data: cohortAssignmentJsonApi });
let request = {};
nock(client.apiBaseUrl).patch(`/v3/cohort_assignments/${cohortAssignment.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await cohortAssignment.save();
t.is(request.path, `/v3/cohort_assignments/${cohortAssignment.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: cohortAssignmentJsonApi });
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, 'cohort_assignments');
t.is(response.createdAt, '2018-04-17T19:59:53Z');
t.is(response.updatedAt, '2018-04-17T19:59:53Z');
});