Commit 7d43c24a authored by samy's avatar samy
Browse files

refactor store function from Archive

parent 19dd8d3b
...@@ -22,17 +22,27 @@ ...@@ -22,17 +22,27 @@
/* global robotLib:true XMLHttpRequest */ /* global robotLib:true XMLHttpRequest */
/* exported robotController */ /* exported robotController */
function checkTranscript(user, text, keywords) {
return ((user === undefined || user.constructor !== Array) || text === undefined || (keywords === undefined || keywords.constructor !== Array));
}
robotLib.archive = function (config) { robotLib.archive = function (config) {
return { return {
store(transcript) { store(user, text, keywords) {
if (transcript === undefined) { if (checkTranscript(user, text, keywords)) {
return false; return false;
} }
const xhttp = new XMLHttpRequest(); const xhttp = new XMLHttpRequest();
xhttp.open('POST', config.archive); xhttp.open('POST', config.archive);
xhttp.setRequestHeader('Content-Type', 'application/json'); xhttp.setRequestHeader('Content-Type', 'application/json');
const transcript = {
text,
keywords,
users: user
};
xhttp.send(JSON.stringify(transcript)); xhttp.send(JSON.stringify(transcript));
return true; return xhttp.status === 200;
} }
}; };
}; };
...@@ -29,11 +29,24 @@ const config = { ...@@ -29,11 +29,24 @@ const config = {
let xhr; let xhr;
let request; let request;
describe('client/lib/archive', () => {
let user;
let text;
let keywords;
describe('client/lib/archive when status server is valide', () => {
beforeEach(() => { beforeEach(() => {
request = []; request = [];
user = ['userTest@open-paas.org'];
text = 'Transcription Text';
keywords = [{
key: 'keyTest',
value: 'testValue'
}];
xhr = sinon.useFakeXMLHttpRequest(); xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (req) { xhr.onCreate = function (req) {
req.status = 200;
request.push(req); request.push(req);
}; };
...@@ -53,19 +66,58 @@ describe('client/lib/archive', () => { ...@@ -53,19 +66,58 @@ describe('client/lib/archive', () => {
expect(global.robotLib.archive).toBeDefined(); expect(global.robotLib.archive).toBeDefined();
}); });
it('should make correct REST call on store', () => { it('should make a correct REST call', () => {
const transcript = 'transcript_test'; const result = global.archive.store(user, text, keywords);
const result = global.archive.store(transcript);
expect(result).toBe(true); expect(result).toBe(true);
expect(request[0].method).toBe('POST');
expect(request[0].url).toBe(config.archive);
expect(request[0].requestBody).toBe(JSON.stringify(transcript));
}); });
it('should not make a REST call on store without transcript', () => { it('should not make a REST call on store without parameters', () => {
const transcript = undefined; const result = global.archive.store();
const result = global.archive.store(transcript); expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should describe the expected behavior when user is undefined', () => {
user = undefined;
const result = global.archive.store(user, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should describe the expected behavior when text is undefined', () => {
text = undefined;
const result = global.archive.store(user, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should describe the expected behavior when keywords are undefined', () => {
keywords = undefined;
const result = global.archive.store(user, text, keywords);
expect(result).toBe(false); expect(result).toBe(false);
expect(request[0]).toBe(undefined); expect(request[0]).toBe(undefined);
}); });
it('should describe the expected behavior when keywords is not an Array', () => {
keywords = 'invalidFormat';
const result = global.archive.store(user, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should describe the expected behavior when user value is not an Array', () => {
user = '';
const result = global.archive.store(user, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should not make a REST call on store when status value is invalid', () => {
xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (req) {
req.status = 500;
};
const result = global.archive.store(user, text, keywords);
expect(result).toBe(false);
});
}); });
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment