Commit 7beb48d1 authored by samy's avatar samy

Add sinon framework

parent 22b45ff3
......@@ -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,34 @@ describe('client/lib/archive', () => {
global.archive = global.robotLib.archive(config);
});
test('should define robotLib.archive', () => {
afterEach(() => {
xhr.restore();
});
it('should define robotLib.archive', () => {
expect(global.robotLib.archive).toBeDefined();
});
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 make init REST request', () => {
const transcript = 'transcript_test';
const result = global.archive.store(transcript);
expect(result).toBeTruthy();
expect(request[0]).not.toBeNull();
});
test('should make a REST call with valid transcript', () => {
const transcript = {};
it('should make correct REST call on store', () => {
const transcript = 'transcript_test';
global.archive.store(transcript);
expect(XMLHttpRequestMock.send).toHaveBeenCalledWith(JSON.stringify(transcript));
console.log(request[0]);
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).toBeFalsy();
expect(request[0]).toBe(undefined);
});
});
......@@ -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