Commit 09f1992b authored by Sami BENHAMICHE's avatar Sami BENHAMICHE

Merge branch '54-check-mocking-framework' into 'master'

Resolve "Check mocking framework"

Closes #54

See merge request !42
parents 22b45ff3 c15e3fc1
Pipeline #4531 passed with stage
in 45 seconds
......@@ -67,7 +67,6 @@ describe('client/controller/external', () => {
global.robotController.external.load({
externalLibs: ['dep1', 'dep2', 'dep3']
});
console.log(document);
expect(document.appendedElements).toHaveLength(3);
expect(document.appendedElements[0]).toEqual(expect.objectContaining({
type: 'text/javascript',
......
......@@ -20,29 +20,24 @@
'use strict';
const robotLib = {};
const xmlHttpRequestMockFactory = () => ({
open: jest.fn().mockImplementation(() => ({})),
setRequestHeader: () => {},
send: jest.fn().mockImplementation(() => {
return {};
})
});
const sinon = require('sinon');
const robotLib = {};
const config = {
archive: 'http://openpaasstore.test'
archive: 'http://openpaasstore.test/summary'
};
let XMLHttpRequestMock;
let xhr;
let request;
describe('client/lib/archive', () => {
beforeEach(() => {
XMLHttpRequestMock = xmlHttpRequestMockFactory();
request = [];
xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (req) {
request.push(req);
};
global.robotLib = robotLib;
global.XMLHttpRequest = function () {
return XMLHttpRequestMock;
};
/* eslint-disable import/no-unassigned-import */
require('./archive.js');
......@@ -50,26 +45,27 @@ describe('client/lib/archive', () => {
global.archive = global.robotLib.archive(config);
});
test('should define robotLib.archive', () => {
expect(global.robotLib.archive).toBeDefined();
afterEach(() => {
xhr.restore();
});
test('should make correct REST call on store', () => {
const transcript = {};
global.archive.store(transcript);
expect(XMLHttpRequestMock.open).toHaveBeenCalledWith('POST', config.archive);
expect(XMLHttpRequestMock.send).toHaveBeenCalled();
it('should define robotLib.archive', () => {
expect(global.robotLib.archive).toBeDefined();
});
test('should make a REST call with valid transcript', () => {
const transcript = {};
global.archive.store(transcript);
expect(XMLHttpRequestMock.send).toHaveBeenCalledWith(JSON.stringify(transcript));
it('should make correct REST call on store', () => {
const transcript = 'transcript_test';
const result = global.archive.store(transcript);
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));
});
test('should not make a REST call without transcript', () => {
it('should not make a REST call on store without transcript', () => {
const transcript = undefined;
global.archive.store(transcript);
expect(XMLHttpRequestMock.send).not.toHaveBeenCalled();
const result = global.archive.store(transcript);
expect(result).toBe(false);
expect(request[0]).toBe(undefined);
});
});
......@@ -20,6 +20,8 @@
'use strict';
const sinon = require('sinon');
const robotLib = {};
function SockJS() {
......@@ -39,28 +41,6 @@ const StompMock = {
over: () => StompclientMock
};
const XMLHttpRequestMock = {
open: jest.fn().mockImplementation(() => ({})),
setRequestHeader: () => {},
send: jest.fn().mockImplementation(() => {
XMLHttpRequestMock.readyState = 4;
XMLHttpRequestMock.status = 200;
XMLHttpRequestMock.responseText = 'somedata';
XMLHttpRequestMock.onreadystatechange();
return {};
}),
onreadystatechange: () => {}
};
const XMLHttpRequest = function () {
// Reset mock
XMLHttpRequestMock.readyState = 0;
XMLHttpRequestMock.status = 0;
XMLHttpRequestMock.responseText = '';
XMLHttpRequestMock.onreadystatechange = () => {};
return XMLHttpRequestMock;
};
const config = {
reco: {
host: 'localhost',
......@@ -68,13 +48,19 @@ const config = {
reconnectInterval: 1000
}
};
let xhr;
let request;
describe('client/lib/reco', () => {
beforeEach(() => {
request = [];
xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (req) {
request.push(req);
};
global.robotLib = robotLib;
global.Stomp = StompMock;
global.SockJS = SockJS;
global.XMLHttpRequest = XMLHttpRequest;
/* eslint-disable import/no-unassigned-import */
require('./live-reco.js');
......@@ -88,24 +74,23 @@ describe('client/lib/reco', () => {
expect(global.robotLib.reco).toBeDefined();
});
test('should make correct REST call on start', () => {
test('should make a correct REST call on start', () => {
const confId = 'testConf';
const urlExpected = 'http://localhost:8080/stream?action=START&id=' + confId;
const reco = global.robotLib.reco(config);
reco.start(confId);
expect(XMLHttpRequestMock.open).toHaveBeenCalledWith('GET', urlExpected, expect.anything());
expect(XMLHttpRequestMock.send).toHaveBeenCalled();
const result = reco.start(confId);
expect(result).toBe(true);
expect(request[0].method).toBe('GET');
expect(request[0].url).toBe(urlExpected);
});
test('should make correct REST call on stop', () => {
test('should make a correct REST call on stop', () => {
const confId = 'testConf';
const urlExpected = 'http://localhost:8080/stream?action=STOP&id=' + confId;
const reco = global.robotLib.reco(config);
reco.stop(confId);
expect(XMLHttpRequestMock.open).toHaveBeenCalledWith('GET', urlExpected, expect.anything());
expect(XMLHttpRequestMock.send).toHaveBeenCalled();
expect(request[0].method).toBe('GET');
expect(request[0].url).toBe(urlExpected);
});
test('should make correct STOMP call when sending data', () => {
......@@ -116,13 +101,4 @@ describe('client/lib/reco', () => {
reco.send(content);
expect(StompclientMock.send).toHaveBeenCalledWith('/app/chat', {}, JSON.stringify(content));
});
test('should make correct REST call when getting recommendation', done => {
const reco = global.robotLib.reco(config);
reco.getOnlineReco('testconf')
.then(res => {
expect(res).toBe('somedata');
done();
});
});
});
......@@ -208,7 +208,6 @@ describe('client/robot', () => {
const e = {data: 'somedata'};
global.MediaRecorder.instances.testid.ondataavailable(e);
console.log(sttMock.sttDataSent);
expect(sttMock.sttDataSent[sttMock.sttDataSent.length - 1]).toBe(e.data);
});
......
......@@ -18,7 +18,8 @@
},
"devDependencies": {
"jest": "20.0.0",
"xo": "0.18.2"
"xo": "0.18.2",
"sinon": "^4.0.2"
},
"author": "Linagora Folks",
"license": "AGPL-3.0",
......
......@@ -3155,6 +3155,18 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
sinon@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-4.0.2.tgz#c81f62456d37986c84e9f522ddb9ce413bda49d2"
dependencies:
diff "^3.1.0"
formatio "1.2.0"
lodash.get "^4.4.2"
lolex "^2.1.3"
nise "^1.2.0"
supports-color "^4.4.0"
type-detect "^4.0.0"
slash@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
......
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