Commit 04b20ff7 authored by samy's avatar samy

Check mocking framework

add sinon to 'live-reco-test.js' to mock XMLHttpRequest
delete console.logs from tests
parent 7beb48d1
Pipeline #4500 failed with stage
in 11 seconds
...@@ -67,7 +67,6 @@ describe('client/controller/external', () => { ...@@ -67,7 +67,6 @@ describe('client/controller/external', () => {
global.robotController.external.load({ global.robotController.external.load({
externalLibs: ['dep1', 'dep2', 'dep3'] externalLibs: ['dep1', 'dep2', 'dep3']
}); });
console.log(document);
expect(document.appendedElements).toHaveLength(3); expect(document.appendedElements).toHaveLength(3);
expect(document.appendedElements[0]).toEqual(expect.objectContaining({ expect(document.appendedElements[0]).toEqual(expect.objectContaining({
type: 'text/javascript', type: 'text/javascript',
......
...@@ -56,14 +56,13 @@ describe('client/lib/archive', () => { ...@@ -56,14 +56,13 @@ describe('client/lib/archive', () => {
it('should make init REST request', () => { it('should make init REST request', () => {
const transcript = 'transcript_test'; const transcript = 'transcript_test';
const result = global.archive.store(transcript); const result = global.archive.store(transcript);
expect(result).toBeTruthy(); expect(result).toBe(true);
expect(request[0]).not.toBeNull(); expect(request[0]).not.toBeNull();
}); });
it('should make correct REST call on store', () => { it('should make correct REST call on store', () => {
const transcript = 'transcript_test'; const transcript = 'transcript_test';
global.archive.store(transcript); global.archive.store(transcript);
console.log(request[0]);
expect(request[0].method).toBe('POST'); expect(request[0].method).toBe('POST');
expect(request[0].url).toBe(config.archive); expect(request[0].url).toBe(config.archive);
expect(request[0].requestBody).toBe(JSON.stringify(transcript)); expect(request[0].requestBody).toBe(JSON.stringify(transcript));
...@@ -72,7 +71,7 @@ describe('client/lib/archive', () => { ...@@ -72,7 +71,7 @@ describe('client/lib/archive', () => {
it('should not make a REST call on store without transcript', () => { it('should not make a REST call on store without transcript', () => {
const transcript = undefined; const transcript = undefined;
const result = global.archive.store(transcript); const result = global.archive.store(transcript);
expect(result).toBeFalsy(); expect(result).toBe(false);
expect(request[0]).toBe(undefined); expect(request[0]).toBe(undefined);
}); });
}); });
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
'use strict'; 'use strict';
const sinon = require('sinon');
const robotLib = {}; const robotLib = {};
function SockJS() { function SockJS() {
...@@ -39,28 +41,6 @@ const StompMock = { ...@@ -39,28 +41,6 @@ const StompMock = {
over: () => StompclientMock 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 = { const config = {
reco: { reco: {
host: 'localhost', host: 'localhost',
...@@ -68,13 +48,19 @@ const config = { ...@@ -68,13 +48,19 @@ const config = {
reconnectInterval: 1000 reconnectInterval: 1000
} }
}; };
let xhr;
let request;
describe('client/lib/reco', () => { describe('client/lib/reco', () => {
beforeEach(() => { beforeEach(() => {
request = [];
xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (req) {
request.push(req);
};
global.robotLib = robotLib; global.robotLib = robotLib;
global.Stomp = StompMock; global.Stomp = StompMock;
global.SockJS = SockJS; global.SockJS = SockJS;
global.XMLHttpRequest = XMLHttpRequest;
/* eslint-disable import/no-unassigned-import */ /* eslint-disable import/no-unassigned-import */
require('./live-reco.js'); require('./live-reco.js');
...@@ -88,24 +74,29 @@ describe('client/lib/reco', () => { ...@@ -88,24 +74,29 @@ describe('client/lib/reco', () => {
expect(global.robotLib.reco).toBeDefined(); expect(global.robotLib.reco).toBeDefined();
}); });
test('should make correct REST call on start', () => { test('should make init ', () => {
const confId = 'testConf'; const confId = 'testConf';
const urlExpected = 'http://localhost:8080/stream?action=START&id=' + confId; const reco = global.robotLib.reco(config);
expect(reco.start(confId)).toBe(true);
expect(request[0]).not.toBeNull();
});
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); const reco = global.robotLib.reco(config);
reco.start(confId); reco.start(confId);
expect(XMLHttpRequestMock.open).toHaveBeenCalledWith('GET', urlExpected, expect.anything()); expect(request[0].method).toBe('GET');
expect(XMLHttpRequestMock.send).toHaveBeenCalled(); 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 confId = 'testConf';
const urlExpected = 'http://localhost:8080/stream?action=STOP&id=' + confId; const urlExpected = 'http://localhost:8080/stream?action=STOP&id=' + confId;
const reco = global.robotLib.reco(config); const reco = global.robotLib.reco(config);
reco.stop(confId); reco.stop(confId);
expect(XMLHttpRequestMock.open).toHaveBeenCalledWith('GET', urlExpected, expect.anything()); expect(request[0].method).toBe('GET');
expect(XMLHttpRequestMock.send).toHaveBeenCalled(); expect(request[0].url).toBe(urlExpected);
}); });
test('should make correct STOMP call when sending data', () => { test('should make correct STOMP call when sending data', () => {
...@@ -116,13 +107,4 @@ describe('client/lib/reco', () => { ...@@ -116,13 +107,4 @@ describe('client/lib/reco', () => {
reco.send(content); reco.send(content);
expect(StompclientMock.send).toHaveBeenCalledWith('/app/chat', {}, JSON.stringify(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', () => { ...@@ -208,7 +208,6 @@ describe('client/robot', () => {
const e = {data: 'somedata'}; const e = {data: 'somedata'};
global.MediaRecorder.instances.testid.ondataavailable(e); global.MediaRecorder.instances.testid.ondataavailable(e);
console.log(sttMock.sttDataSent);
expect(sttMock.sttDataSent[sttMock.sttDataSent.length - 1]).toBe(e.data); expect(sttMock.sttDataSent[sttMock.sttDataSent.length - 1]).toBe(e.data);
}); });
......
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