Commit 2b9e4324 authored by Tom JORQUERA's avatar Tom JORQUERA
Browse files

Merge branch '54-Refactor-libArchive-API-clean' into 'master'

54-Refactor-libArchive-API-clean

Closes #54

See merge request !45
parents 19dd8d3b 2a28a04e
Pipeline #5147 passed with stage
in 40 seconds
...@@ -22,15 +22,25 @@ ...@@ -22,15 +22,25 @@
/* global robotLib:true XMLHttpRequest */ /* global robotLib:true XMLHttpRequest */
/* exported robotController */ /* exported robotController */
function isValidTranscript(users, text, keywords) {
return !((users === undefined || users.constructor !== Array) || text === undefined || (keywords === undefined || keywords.constructor !== Array));
}
robotLib.archive = function (config) { robotLib.archive = function (config) {
return { return {
store(transcript) { store(users, text, keywords) {
if (transcript === undefined) { if (!isValidTranscript(users, 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
};
xhttp.send(JSON.stringify(transcript)); xhttp.send(JSON.stringify(transcript));
return true; return true;
} }
......
...@@ -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 users;
let text;
let keywords;
describe('client/lib/archive when status server is valide', () => {
beforeEach(() => { beforeEach(() => {
request = []; request = [];
users = ['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,18 +66,48 @@ describe('client/lib/archive', () => { ...@@ -53,18 +66,48 @@ 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(users, 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 not make a REST call on store users is undefined', () => {
users = undefined;
const result = global.archive.store(users, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should not make a REST call on store text is undefined', () => {
text = undefined;
const result = global.archive.store(users, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should not make a REST call on store when keywords are undefined', () => {
keywords = undefined;
const result = global.archive.store(users, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should not make a REST call on store when keywords is not an Array', () => {
keywords = 'invalidFormat';
const result = global.archive.store(users, text, keywords);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
it('should not make a REST call on store when users is not an Array', () => {
users = '';
const result = global.archive.store(users, text, keywords);
expect(result).toBe(false); expect(result).toBe(false);
expect(request[0]).toBe(undefined); expect(request[0]).toBe(undefined);
}); });
......
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