test/unit/services.related-user.js
import test from 'ava';
import sinon from 'sinon';
import { client, RelatedUser, relatedUserService } from '../../src';
const relatedUserJsonApi = {
id: 2,
type: 'related_users',
attributes: {
created_at: '2018-02-09T05:28:37Z',
updated_at: '2018-02-09T05:28:37Z',
relationship: null
},
relationships: {
followed_user: {
data: {
id: 61,
type: 'mobile_users'
}
},
follower_user: {
data: {
id: 60,
type: 'mobile_users'
}
}
}
};
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.server = sinon.fakeServer.create();
t.context.server.autoRespond = true;
t.context.server.respondWith('DELETE', `${client.apiBaseUrl}/v3/related_users/*`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/mobile_users/*/related_users?filter=followed`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '3',
type: 'related_users',
attributes: {
created_at: '2018-02-09T05:28:38Z',
updated_at: '2018-02-09T05:28:38Z',
relationship: 'specific-relationship-name'
},
relationships: {
followed_user: {
data: {
id: 64,
type: 'mobile_users'
}
},
follower_user: {
data: {
id: 46,
type: 'users'
}
}
}
}
})]);
t.context.server.respondWith('PATCH', `${client.apiBaseUrl}/v3/related_users/*`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: 3,
type: 'related_users',
attributes: {
created_at: '2018-02-09T05:28:38Z',
updated_at: '2018-02-09T05:28:38Z',
relationship: 'specific-relationship-name'
},
relationships: {
followed_user: {
data: {
id: 64,
type: 'mobile_users'
}
},
follower_user: {
data: {
id: 46,
type: 'users'
}
}
}
}
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/related_users`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: 4,
type: 'related_users',
attributes: {
created_at: '2018-02-09T05:28:38Z',
updated_at: '2018-02-09T05:28:38Z',
relationship: 'specific-relationship-name'
},
relationships: {
followed_user: {
data: {
id: 64,
type: 'mobile_users'
}
},
follower_user: {
data: {
id: 46,
type: 'users'
}
}
}
}
})]);
t.context.storage = client.storageUtility;
client.relatedUser = new RelatedUser({});
t.context.relatedUserJsonApi = {
data: {
type: 'related_users',
attributes: {
created_at: '2018-02-09T05:28:38Z',
updated_at: '2018-02-09T05:28:38Z',
relationship: 'specific-relationship-name'
}
}
};
t.context.relatedUser = new RelatedUser(t.context.relatedUserJsonApi);
});
test.afterEach(t => t.context.server.restore());
// RelatedUserService.delete method
/**
* @test {RelatedUserService.delete}
*/
test('[unit] RelatedUserService.delete should exist', (t) => {
t.truthy(relatedUserService.delete);
});
/**
* @test {RelatedUserService.delete}
*/
test('[unit] RelatedUserService.delete should receive a valid response for a delete request', async (t) => {
const { relatedUser } = t.context;
relatedUser.id = 5;
const response = await relatedUserService.delete(relatedUser);
// const request = t.context.server.requests[0];
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.truthy(response);
});
// RelatedUserService.get method
/**
* @test {RelatedUserService.get}
*/
test('[unit] RelatedUserService.get should exist', (t) => {
t.truthy(relatedUserService.get);
});
/**
* @test {RelatedUserService.get}
*/
test.serial('[unit] RelatedUserService.get should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'RelatedUserService.get error: requires authToken';
t.throws(() => relatedUserService.getByUser({ id: 5, type: 'mobile_users', relationship: 'followed' }), expectedError);
});
/**
* @test {RelatedUserService.get}
*/
test('[unit] RelatedUserService.get should throw an error when there is no id', (t) => {
const expectedError = 'RelatedUserService.get error: params does not have id';
t.throws(() => relatedUserService.getByUser({ type: 'mobile_users', relationship: 'followed' }), expectedError);
});
/**
* @test {RelatedUserService.get}
*/
test('[unit] RelatedUserService.get should throw an error when there is no type', (t) => {
const expectedError = 'RelatedUserService.get error: params does not have type';
t.throws(() => relatedUserService.getByUser({ id: 5, relationship: 'followed' }), expectedError);
});
/**
* @test {RelatedUserService.get}
*/
test('[unit] RelatedUserService.get should throw an error when there is no relationship', (t) => {
const expectedError = 'RelatedUserService.get error: params does not have relationship';
t.throws(() => relatedUserService.getByUser({ id: 5, type: 'mobile_users' }), expectedError);
});
/**
* @test {RelatedUserService.get}
*/
test('[unit] RelatedUserService.get should return a promise', async (t) => {
t.truthy(await relatedUserService.getByUser({ id: 5, type: 'mobile_users', relationship: 'followed' }).then);
});
// /**
// * @test {RelatedUserService.get}
// */
// test('[unit] RelatedUserService.get should make a properly formatted get request', async (t) => {
// const params = { id: 5, type: 'mobile_users', relationship: 'followed' };
// await relatedUserService.getByUser(params);
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/mobile_users/${params.id}/related_users?filter=${params.relationship}`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// });
/**
* @test {RelatedUserService.get}
*/
test('[unit] RelatedUserService.get should receive a valid response for a get request with an id', async (t) => {
const params = { id: 1, type: 'mobile_users', relationship: 'followed' };
const response = await relatedUserService.getByUser(params);
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/mobile_users/1/related_users?filter=followed`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.truthy(response);
t.true(response instanceof RelatedUser);
t.is(response.id, 3);
t.is(response.type, 'related_users');
t.is(response.createdAt, '2018-02-09T05:28:38Z');
t.is(response.updatedAt, '2018-02-09T05:28:38Z');
t.is(response.relationship, 'specific-relationship-name');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
});
// RelatedUserService.insert method
/**
* @test {RelatedUserService.insert}
*/
test.serial('[unit] RelatedUserService.insert should throw an error when there is no authToken', (t) => {
const relatedUser = new RelatedUser(relatedUserJsonApi);
client.authToken = undefined;
const expectedError = 'RelatedUserService.insert error: requires authToken';
t.throws(() => relatedUserService.insert(relatedUser), expectedError);
});
/**
* @test {RelatedUserService.insert}
*/
test('[unit] RelatedUserService.insert should successfully insert a relatedUser with a relatedUser object', async (t) => {
const relatedUser = new RelatedUser(relatedUserJsonApi);
const response = await relatedUserService.insert(relatedUser);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/v3/related_users`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({ data: relatedUserJsonApi }));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.true(response instanceof RelatedUser);
t.is(response.id, 4);
t.is(response.type, 'related_users');
t.is(response.createdAt, '2018-02-09T05:28:38Z');
t.is(response.updatedAt, '2018-02-09T05:28:38Z');
t.is(response.relationship, 'specific-relationship-name');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
});
// RelatedUserService.update method
/**
* @test {RelatedUserService.update}
*/
test('[unit] RelatedUserService.update should throw errors for invalid parameters', (t) => {
const undefinedError = 'RelatedUserService.update error: relatedUser is not defined';
t.throws(() => relatedUserService.update(), undefinedError);
});
/**
* @test {RelatedUserService.update}
*/
test('[unit] RelatedUserService.update should successfully update a relatedUser with a relatedUser object', async (t) => {
const relatedUser = new RelatedUser(relatedUserJsonApi);
const response = await relatedUserService.update(relatedUser);
// const request = t.context.server.requests[0];
// t.is(request.method, 'PATCH');
// t.is(request.url, `${client.apiBaseUrl}/v3/related_users/${relatedUser.id}`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({ data: relatedUserJsonApi }));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.true(response instanceof RelatedUser);
t.is(response.id, 3);
t.is(response.type, 'related_users');
t.is(response.createdAt, '2018-02-09T05:28:38Z');
t.is(response.updatedAt, '2018-02-09T05:28:38Z');
t.is(response.relationship, 'specific-relationship-name');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
t.is(response.followed.id, 64);
t.is(response.followed.type, 'mobile_users');
t.is(response.follower.id, 46);
t.is(response.follower.type, 'users');
});