test/unit/helpers.entry-template.js
import test from 'ava';
import sinon from 'sinon';
import {
client,
EntryTemplate,
User,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.templateJsonApi = {
id: 12,
type: 'ediary__entry_templates',
attributes: {
category: 'manual',
created_at: '2017-06-27T20:32:59Z',
updated_at: '2017-06-27T20:32:59Z'
},
relationships: {
entry_group: {
data: {
id: 13,
type: 'ediary__entry_groups'
}
},
flow_process: {
data: {
id: 145,
type: 'data_collection__flow_processes'
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.authToken = 'valid_token';
t.context.server = sinon.fakeServer.create();
t.context.server.autoRespond = true;
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/ediary/entries`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '7',
type: 'ediary__entries',
attributes: {
created_at: '2017-06-27T20:32:07Z',
updated_at: '2017-06-27T20:32:07Z',
date: '2018-06-02'
},
relationships: {
entry_group: {
data: {
id: '8',
type: 'ediary__entry_groups'
}
},
template: {
data: {
id: '7',
type: 'ediary__entry_templates'
}
},
captured_value_group: {
data: null
},
owner: {
data: {
id: '22',
type: 'mobile_users'
}
},
status: {
data: null
}
}
},
included: [
{
id: '7',
type: 'ediary__entry_templates',
attributes: {
category: 'automatic',
created_at: '2017-06-27T20:32:07Z',
updated_at: '2017-06-27T20:32:07Z'
},
relationships: {
entry_group: {
data: {
id: '8',
type: 'ediary__entry_groups'
}
},
flow_process: {
data: {
id: '23',
type: 'data_collection__flow_processes'
}
}
}
},
{
id: '22',
type: 'mobile_users',
attributes: {
uuid: '51edc9a9-6e07-4ffd-a22a-8a620f45ac99',
account_name: 'dummy_21',
email: 'user55@fake.com',
encryption_key: null,
created_at: '2017-06-27T20:32:07Z',
updated_at: '2017-06-27T20:32:07Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null
},
relationships: {
devices: {
data: []
}
}
}
]
})]);
});
test.afterEach(t => t.context.server.restore());
/**
* @test {EntryTemplate}
*/
test('[unit] EntryTemplate should handle template data with json api format', (t) => {
const { templateJsonApi } = t.context;
const template = new EntryTemplate({ data: templateJsonApi });
t.is(template.id, 12);
t.is(template.type, 'ediary__entry_templates');
t.is(template.category, 'manual');
t.is(template.createdAt, '2017-06-27T20:32:59Z');
t.is(template.updatedAt, '2017-06-27T20:32:59Z');
});
/**
* @test {EntryTemplate}
*/
test('[unit] EntryTemplate should generate json api format when converted to string', (t) => {
const { templateJsonApi } = t.context;
const template = new EntryTemplate(templateJsonApi);
t.deepEqual(template.toJSON(), templateJsonApi);
});
/**
* @test {EntryTemplate}
*/
test('[unit] EntryTemplate.addEntry should call POST request', async (t) => {
const { templateJsonApi } = t.context;
const template = new EntryTemplate(templateJsonApi);
// await template.syncRelationships();
const attributes = { date: '2018-06-02' };
const mobileUser = new User({ id: 53 });
const response = await template.addEntry(attributes, mobileUser);
// const { method, url, requestHeaders, requestBody } = t.context.server.requests[0];
// t.is(method, 'POST');
// t.is(url, `${client.apiBaseUrl}/v3/mobile_users/${mobileUser.id}/ediary/entries`);
// t.is(requestHeaders.Accept, 'application/json');
// t.is(requestBody, JSON.stringify({
// data: {
// type: 'ediary__entries',
// attributes: {
// date: '2018-06-02'
// },
// relationships: {
// template: {
// data: {
// id: 12,
// type: 'ediary__entry_templates'
// }
// }
// }
// }
// }));
// t.is(requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(requestHeaders.Authorization, 'Token token=valid_token');
t.truthy(response);
t.is(response.constructor.name, 'Entry');
t.is(response.id, 7);
t.is(response.createdAt, '2017-06-27T20:32:07Z');
t.is(response.updatedAt, '2017-06-27T20:32:07Z');
t.is(response.date, '2018-06-02');
// expect(response.entryGroup.constructor.name, 'EntryGroup');
t.is(response.entryGroup.id, '8');
// expect(response.flow.constructor.name, 'Flow');
t.is(response.flow.id, '23');
t.is(response.owner.constructor.name, 'User');
t.is(response.owner.id, 22);
});