test/unit/services.event.v3.js
import test from 'ava';
import nock from 'nock';
import {
client,
eventService,
Event,
Rule,
User,
} from '../../src';
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
nock(client.apiBaseUrl).delete(/\/v3\/reminder\/events\/(\d*)$/).reply(200, { status: 'ok' });
nock(client.apiBaseUrl).get(`/v3/reminder/events`).reply(200, {
data: [
{
id: '33',
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z',
status: 5,
extras: null,
created_at: '2017-09-12T05:35:12Z',
updated_at: '2017-09-12T05:35:12Z'
},
relationships: {
mobile_user: {
data: null
},
rule: {
data: {
id: '68',
type: 'reminder__rules'
}
}
}
},
{
id: '34',
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z',
status: 5,
extras: null,
created_at: '2017-09-12T05:35:12Z',
updated_at: '2017-09-12T05:35:12Z'
},
relationships: {
mobile_user: {
data: null
},
rule: {
data: {
id: '69',
type: 'reminder__rules'
}
}
}
},
{
id: '35',
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z',
status: 5,
extras: null,
created_at: '2017-09-12T05:35:12Z',
updated_at: '2017-09-12T05:35:12Z'
},
relationships: {
mobile_user: {
data: null
},
rule: {
data: {
id: '70',
type: 'reminder__rules'
}
}
}
}
]
});
nock(client.apiBaseUrl).get(/\/v3\/reminder\/events\/(\d*)$/).reply(200, {
data: {
id: '32',
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z',
status: 5,
extras: null,
created_at: '2017-09-12T05:35:12Z',
updated_at: '2017-09-12T05:35:12Z'
},
relationships: {
mobile_user: {
data: null
},
rule: {
data: {
id: '67',
type: 'reminder__rules'
}
}
}
}
});
nock(client.apiBaseUrl).patch(/\/v3\/reminder\/events\/(\d*)$/).reply(200, {
data: {
id: '7',
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z',
status: 5,
extras: null,
created_at: '2017-09-12T05:36:52Z',
updated_at: '2017-09-12T05:36:52Z'
},
relationships: {
mobile_user: {
data: {
id: '215',
type: 'mobile_users'
}
},
rule: {
data: {
id: '19',
type: 'reminder__rules'
}
}
}
}
});
t.context.storage = client.storageUtility;
t.context.eventJsonApi = {
data: {
id: 7,
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z'
},
relationships: {
rule: {
data: {
type: 'reminder__rules',
id: 19
}
},
mobile_user: {
data: {
type: 'mobile_users',
id: 215
}
}
}
}
};
t.context.event = new Event(t.context.eventJsonApi);
t.context.event.rule = new Rule({ id: 19 });
t.context.event.rule.store();
t.context.event.mobileUser = new User({ id: 215 });
t.context.event.mobileUser.store();
});
// EventService.delete method
/**
* @test {EventService.delete}
*/
test('[unit] EventService.delete should exist', (t) => {
t.truthy(eventService.delete);
});
/**
* @test {EventService.delete}
*/
test('[unit] EventService.delete should return a promise', async (t) => {
const { event } = t.context;
t.truthy(await eventService.delete(event).then);
});
/**
* @test {EventService.delete}
*/
test('[unit] EventService.delete should throw errors for invalid parameters', (t) => {
const undefinedError = 'EventService.delete error: event is not defined';
t.throws(() => eventService.delete(), undefinedError);
});
// /**
// * @test {EventService.delete}
// */
// test('[unit] EventService.delete should make a properly formatted delete request', async (t) => {
// const { event } = t.context;
// await eventService.delete(event);
// const request = t.context.server.requests[0];
// t.is(request.method, 'DELETE');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/events/${event.id}`);
// 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 {EventService.delete}
*/
test('[unit] EventService.delete should receive a valid response for a delete request', async (t) => {
const { event } = t.context;
const response = await eventService.delete(event);
// const request = t.context.server.requests[0];
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.truthy(response);
});
/**
* @test {EventService.delete}
*/
test('[unit] EventService.delete should remove the element from local storage', async (t) => {
const { event, storage } = t.context;
await storage.set('events', event.toJSON(), { id: event.id });
await eventService.delete(event);
t.is(storage.has('events', { id: event.id }), false);
});
// EventService.get method
/**
* @test {EventService.get}
*/
test('[unit] EventService.get should exist', (t) => {
t.truthy(eventService.get);
});
/**
* @test {EventService.get}
*/
test.serial('[unit] EventService.get should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'EventService.get error: requires authToken';
t.throws(() => eventService.get(), expectedError);
});
/**
* @test {EventService.get}
*/
test('[unit] EventService.get should return a promise', async (t) => {
t.truthy(await eventService.get().then);
});
// /**
// * @test {EventService.get}
// */
// test('[unit] EventService.get should make a properly formatted get request', async (t) => {
// await eventService.get();
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/events`);
// 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 {EventService.get}
*/
test('[unit] EventService.get should receive a valid response for a get request without an id', async (t) => {
const response = await eventService.get();
t.truthy(response);
t.is(Object.keys(response).length, 3);
});
/**
* @test {EventService.get}
*/
test('[unit] EventService.get should receive a valid response for a get request with an id', async (t) => {
const rule = new Rule({ id: 67 });
rule.store();
const response = await eventService.get({ id: 32 });
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/events/32`);
// 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.is(response.id, 32);
t.is(response.type, 'reminder__events');
t.is(response.createdAt, '2017-09-12T05:35:12Z');
t.is(response.updatedAt, '2017-09-12T05:35:12Z');
t.is(response.date, '2016-06-06T00:00:00Z');
t.is(response.status, 5);
t.is(response.rule.id, 67);
t.is(response.rule.type, 'reminder__rules');
});
// EventService.insert method
/**
* @test {EventService.insert}
*/
test.serial('[unit] EventService.insert should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'EventService.insert error: requires authToken';
t.throws(() => eventService.insert(new Event({ id: 23 })), expectedError);
});
/**
* @test {EventService.insert}
*/
test('[unit] EventService.insert should successfully insert a event with a event object', async (t) => {
nock(client.apiBaseUrl).post(`/v3/reminder/events`).reply(201, {
data: {
id: '8',
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z',
status: 5,
extras: null,
created_at: '2017-09-12T05:36:52Z',
updated_at: '2017-09-12T05:36:52Z'
},
relationships: {
mobile_user: {
data: {
id: '217',
type: 'mobile_users'
}
},
rule: {
data: {
id: '20',
type: 'reminder__rules'
}
}
}
}
});
const requestJsonApi = {
data: {
type: 'reminder__events',
attributes: {
date: '2016-06-06T00:00:00Z'
},
relationships: {
rule: {
data: {
type: 'reminder__rules',
id: 20
}
},
mobile_user: {
data: {
type: 'mobile_users',
id: 217
}
}
}
}
};
const event = new Event(requestJsonApi);
event.rule = new Rule({ id: 20 });
event.rule.store();
event.mobileUser = new User({ id: 217 });
event.mobileUser.store();
const response = await eventService.insert(event);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/events`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.deepEqual(JSON.parse(request.requestBody), requestJsonApi);
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.id, 8);
t.is(response.type, 'reminder__events');
t.is(response.createdAt, '2017-09-12T05:36:52Z');
t.is(response.updatedAt, '2017-09-12T05:36:52Z');
t.is(response.date, '2016-06-06T00:00:00Z');
t.is(response.status, 5);
t.is(response.rule.id, 20);
t.is(response.rule.type, 'reminder__rules');
t.is(response.mobileUser.id, 217);
t.is(response.mobileUser.type, 'mobile_users');
});
/**
* @test {EventService.insert}
*/
// test('[unit] EventService.insert should throw an error with an invalid token', async (t) => {
// eventService.cacheMode = 'networkOnly';
// t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/reminder/events`,
// [422, { 'Content-Type': 'application/json' }, JSON.stringify({
// errors: [
// {
// source: {
// pointer: '/data/attributes/mobile_application_key'
// },
// detail: 'can\'t be blank'
// }
// ]
// })]
// );
// const requestJsonApi = {
// data: {
// type: 'events',
// attributes: {
// push_id: 'FAKE_ID',
// mobile_application_key: 'invalid'
// }
// }
// };
// client.mobileApplicationKey = 'invalid';
// let response;
// await eventService.insert().catch(res => (response = res));
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/events`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify(requestJsonApi));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// t.truthy(response);
// });
// EventService.update method
/**
* @test {EventService.update}
*/
test('[unit] EventService.update should throw errors for invalid parameters', (t) => {
const undefinedError = 'EventService.update error: event is not defined';
t.throws(() => eventService.update(), undefinedError);
});
/**
* @test {EventService.update}
*/
test('[unit] EventService.update should successfully update a event with a event object', async (t) => {
const { event } = t.context; // eventJsonApi
const response = await eventService.update(event);
// const request = t.context.server.requests[0];
// t.is(request.method, 'PATCH');
// t.is(request.url, `${client.apiBaseUrl}/v3/reminder/events/${event.id}`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.deepEqual(JSON.parse(request.requestBody), eventJsonApi);
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.id, 7);
t.is(response.type, 'reminder__events');
t.is(response.createdAt, '2017-09-12T05:36:52Z');
t.is(response.updatedAt, '2017-09-12T05:36:52Z');
t.is(response.date, '2016-06-06T00:00:00Z');
t.is(response.status, 5);
t.is(response.rule.id, 19);
t.is(response.rule.type, 'reminder__rules');
t.is(response.mobileUser.id, 215);
t.is(response.mobileUser.type, 'mobile_users');
});