First replacements of sinon mocks with jest mocks

This commit is contained in:
Alejandro Celaya
2019-04-19 10:29:49 +02:00
parent 2cd6e52e9c
commit f8de069567
11 changed files with 666 additions and 127 deletions

View File

@@ -1,26 +1,25 @@
import sinon from 'sinon';
import ServersExporter from '../../../src/servers/services/ServersExporter';
describe('ServersExporter', () => {
const createLinkMock = () => ({
setAttribute: sinon.fake(),
click: sinon.fake(),
setAttribute: jest.fn(),
click: jest.fn(),
style: {},
});
const createWindowMock = (isIe10 = true) => ({
navigator: {
msSaveBlob: isIe10 ? sinon.fake() : undefined,
msSaveBlob: isIe10 ? jest.fn() : undefined,
},
document: {
createElement: sinon.fake.returns(createLinkMock()),
createElement: jest.fn(() => createLinkMock()),
body: {
appendChild: sinon.fake(),
removeChild: sinon.fake(),
appendChild: jest.fn(),
removeChild: jest.fn(),
},
},
});
const serversServiceMock = {
listServers: sinon.fake.returns({
listServers: jest.fn(() => ({
abc123: {
id: 'abc123',
name: 'foo',
@@ -29,10 +28,16 @@ describe('ServersExporter', () => {
id: 'def456',
name: 'bar',
},
}),
})),
};
const createCsvjsonMock = (throwError = false) => ({
toCSV: throwError ? sinon.fake.throws('') : sinon.fake.returns(''),
toCSV: jest.fn(() => {
if (throwError) {
throw new Error('');
}
return '';
}),
});
describe('exportServers', () => {
@@ -40,10 +45,10 @@ describe('ServersExporter', () => {
beforeEach(() => {
originalConsole = global.console;
global.console = { error: sinon.fake() };
global.console = { error: jest.fn() };
global.Blob = class Blob {};
global.URL = { createObjectURL: () => '' };
serversServiceMock.listServers.resetHistory();
serversServiceMock.listServers.mockReset();
});
afterEach(() => {
global.console = originalConsole;
@@ -59,8 +64,8 @@ describe('ServersExporter', () => {
exporter.exportServers();
expect(global.console.error.callCount).toEqual(1);
expect(csvjsonMock.toCSV.callCount).toEqual(1);
expect(global.console.error).toHaveBeenCalledTimes(1);
expect(csvjsonMock.toCSV).toHaveBeenCalledTimes(1);
});
it('makes use of msSaveBlob API when available', () => {
@@ -73,9 +78,9 @@ describe('ServersExporter', () => {
exporter.exportServers();
expect(serversServiceMock.listServers.callCount).toEqual(1);
expect(windowMock.navigator.msSaveBlob.callCount).toEqual(1);
expect(windowMock.document.createElement.callCount).toEqual(0);
expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(windowMock.navigator.msSaveBlob).toHaveBeenCalledTimes(1);
expect(windowMock.document.createElement).not.toHaveBeenCalled();
});
it('makes use of download link API when available', () => {
@@ -88,10 +93,10 @@ describe('ServersExporter', () => {
exporter.exportServers();
expect(serversServiceMock.listServers.callCount).toEqual(1);
expect(windowMock.document.createElement.callCount).toEqual(1);
expect(windowMock.document.body.appendChild.callCount).toEqual(1);
expect(windowMock.document.body.removeChild.callCount).toEqual(1);
expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(windowMock.document.createElement).toHaveBeenCalledTimes(1);
expect(windowMock.document.body.appendChild).toHaveBeenCalledTimes(1);
expect(windowMock.document.body.removeChild).toHaveBeenCalledTimes(1);
});
});
});

View File

@@ -1,14 +1,13 @@
import sinon from 'sinon';
import ServersImporter from '../../../src/servers/services/ServersImporter';
describe('ServersImporter', () => {
const servers = [{ name: 'foo' }, { name: 'bar' }];
const csvjsonMock = {
toObject: sinon.fake.returns(servers),
toObject: jest.fn(() => servers),
};
const importer = new ServersImporter(csvjsonMock);
beforeEach(() => csvjsonMock.toObject.resetHistory());
beforeEach(() => csvjsonMock.toObject.mockClear());
describe('importServersFromFile', () => {
it('rejects with error if no file was provided', async () => {
@@ -28,7 +27,7 @@ describe('ServersImporter', () => {
});
it('reads file when a CSV is provided', async () => {
const readAsText = sinon.fake.returns('');
const readAsText = jest.fn(() => '');
global.FileReader = class FileReader {
constructor() {
@@ -40,8 +39,8 @@ describe('ServersImporter', () => {
await importer.importServersFromFile({ type: 'text/csv' });
expect(readAsText.callCount).toEqual(1);
expect(csvjsonMock.toObject.callCount).toEqual(1);
expect(readAsText).toHaveBeenCalledTimes(1);
expect(csvjsonMock.toObject).toHaveBeenCalledTimes(1);
});
});
});

View File

@@ -1,4 +1,3 @@
import sinon from 'sinon';
import { last } from 'ramda';
import ServersService from '../../../src/servers/services/ServersService';
@@ -8,8 +7,8 @@ describe('ServersService', () => {
def456: { id: 'def456' },
};
const createStorageMock = (returnValue) => ({
set: sinon.fake(),
get: sinon.fake.returns(returnValue),
set: jest.fn(),
get: jest.fn(() => returnValue),
});
describe('listServers', () => {
@@ -20,8 +19,8 @@ describe('ServersService', () => {
const result = service.listServers();
expect(result).toEqual({});
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).not.toHaveBeenCalled();
});
it('returns value from storage when found', () => {
@@ -31,8 +30,8 @@ describe('ServersService', () => {
const result = service.listServers();
expect(result).toEqual(servers);
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).not.toHaveBeenCalled();
});
});
@@ -44,8 +43,8 @@ describe('ServersService', () => {
const result = service.findServerById('ghi789');
expect(result).toBeUndefined();
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).not.toHaveBeenCalled();
});
it('returns server from list when found', () => {
@@ -55,8 +54,8 @@ describe('ServersService', () => {
const result = service.findServerById('abc123');
expect(result).toEqual({ id: 'abc123' });
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(0);
expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).not.toHaveBeenCalled();
});
});
@@ -67,9 +66,12 @@ describe('ServersService', () => {
service.createServer({ id: 'ghi789' });
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(1);
expect(last(storageMock.set.lastCall.args)).toEqual({
expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledTimes(1);
const setLastCallLastArg = last(last(storageMock.set.mock.calls));
expect(setLastCallLastArg).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
ghi789: { id: 'ghi789' },
@@ -84,9 +86,12 @@ describe('ServersService', () => {
service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]);
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(1);
expect(last(storageMock.set.lastCall.args)).toEqual({
expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledTimes(1);
const setLastCallLastArg = last(last(storageMock.set.mock.calls));
expect(setLastCallLastArg).toEqual({
abc123: { id: 'abc123' },
def456: { id: 'def456' },
ghi789: { id: 'ghi789' },
@@ -102,9 +107,12 @@ describe('ServersService', () => {
service.deleteServer({ id: 'abc123' });
expect(storageMock.get.callCount).toEqual(1);
expect(storageMock.set.callCount).toEqual(1);
expect(last(storageMock.set.lastCall.args)).toEqual({
expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledTimes(1);
const setLastCallLastArg = last(last(storageMock.set.mock.calls));
expect(setLastCallLastArg).toEqual({
def456: { id: 'def456' },
});
});