Skip to content

Commit 3d9a5ed

Browse files
test: replace sinon with jest mock (#688)
1 parent bcb187f commit 3d9a5ed

File tree

12 files changed

+46
-352
lines changed

12 files changed

+46
-352
lines changed

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@
5757
"@types/jest": "^26.0.8",
5858
"@types/react": "^16.9.34",
5959
"@types/react-dom": "^16.9.6",
60-
"@types/sinon": "^9.0.0",
6160
"@typescript-eslint/eslint-plugin": "^3.7.0",
6261
"@typescript-eslint/parser": "^3.7.0",
6362
"@ui5/webcomponents-tools": "^1.0.0-rc.8",
64-
"babel-jest": "^26.2.2",
6563
"babel-loader": "^8.1.0",
6664
"chalk": "^4.0.0",
6765
"dedent": "^0.7.0",
@@ -94,10 +92,7 @@
9492
"rimraf": "^3.0.1",
9593
"rollup": "^2.23.0",
9694
"rollup-plugin-terser": "^6.1.0",
97-
"shelljs": "^0.8.3",
98-
"sinon": "^9.0.2",
9995
"targz": "^1.0.1",
100-
"tmp": "^0.1.0",
10196
"typescript": "^3.9.7"
10297
},
10398
"resolutions": {

packages/base/src/Device/EventRegistry.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import * as sinon from 'sinon';
21
import { EventRegistry } from './EventRegistry';
32

43
describe('Device - Event Registry', () => {
54
test('Add, fire and remove event', () => {
6-
const callback = sinon.spy();
5+
const callback = jest.fn();
76
EventRegistry.attachEvent('test', callback);
87
expect(EventRegistry.mEventRegistry.hasOwnProperty('test')).toBe(true);
98
EventRegistry.fireEvent('test');
10-
expect(callback.called).toBe(true);
9+
expect(callback).toBeCalled();
1110
EventRegistry.detachEvent('test', callback);
1211
expect(EventRegistry.mEventRegistry.hasOwnProperty('test')).toBe(false);
1312
});
1413

1514
test('Add and remove events with different listeners', () => {
16-
const callback = sinon.spy();
15+
const callback = jest.fn();
1716
const a = {};
1817
const b = {};
1918
EventRegistry.attachEvent('test', callback, a);
@@ -26,7 +25,7 @@ describe('Device - Event Registry', () => {
2625
});
2726

2827
test('Remove not existing event should not crash', () => {
29-
const callback = sinon.spy();
28+
const callback = jest.fn();
3029
expect(() => {
3130
EventRegistry.detachEvent('test', callback);
3231
}).not.toThrow();

packages/base/src/Device/Media.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as sinon from 'sinon';
21
import { setupMatchMedia } from '../../../../config/jestsetup';
32
import { EventRegistry } from './EventRegistry';
43
import { Media, RANGESETS } from './Media';
@@ -45,12 +44,12 @@ describe('Device - Media', () => {
4544

4645
test('Attach, fire and Detach Event', () => {
4746
const media = new Media(defaultSupportInstance);
48-
const callback = sinon.spy();
47+
const callback = jest.fn();
4948
// @ts-ignore
5049
media.attachHandler(callback, this);
5150
expect(EventRegistry.mEventRegistry.hasOwnProperty('media_Std'));
5251
EventRegistry.fireEvent('media_Std');
53-
expect(callback.called).toBe(true);
52+
expect(callback).toBeCalled();
5453
// @ts-ignore
5554
media.detachHandler(callback, this);
5655
expect(EventRegistry.mEventRegistry.hasOwnProperty('media_Std')).toBe(false);

packages/base/src/Device/Orientation.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import * as sinon from 'sinon';
21
import { EventRegistry } from './EventRegistry';
32
import { Orientation } from './Orientation';
43

54
describe('Device - Orientation', () => {
65
test('Add and remove handler', () => {
76
const instance = new Orientation();
8-
const callback = sinon.spy();
7+
const callback = jest.fn();
98
// @ts-ignore
109
instance.attachHandler(callback, this);
1110
// @ts-ignore

packages/base/src/Device/Resize.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import * as sinon from 'sinon';
21
import { EventRegistry } from './EventRegistry';
32
import { Resize } from './Resize';
43

54
describe('Device - Resize', () => {
65
test('Add and remove handler', () => {
76
const instance = new Resize();
8-
const callback = sinon.spy();
7+
const callback = jest.fn();
98
// @ts-ignore
109
instance.attachHandler(callback, this);
1110
// @ts-ignore

packages/base/src/utils/Logger.test.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
import { modifyObjectProperty } from '@shared/tests/utils';
2-
import * as sinon from 'sinon';
32
import { LOG_LEVEL, Logger } from './Logger';
43

54
describe('Logger', () => {
65
test('fatal', () => {
7-
const spy = sinon.spy(console, 'error');
6+
const spy = jest.spyOn(console, 'error').mockImplementation();
87
Logger.log(LOG_LEVEL.FATAL, 'Test');
9-
expect(spy.called).toBe(true);
10-
spy.restore();
8+
expect(spy).toBeCalled();
9+
spy.mockRestore();
1110
});
1211
test('error', () => {
13-
const spy = sinon.spy(console, 'error');
12+
const spy = jest.spyOn(console, 'error').mockImplementation();
1413
Logger.log(LOG_LEVEL.ERROR, 'Test');
15-
expect(spy.called).toBe(true);
16-
spy.restore();
14+
expect(spy).toBeCalled();
15+
spy.mockRestore();
1716
});
1817
test('warning', () => {
19-
const spy = sinon.spy(console, 'warn');
18+
const spy = jest.spyOn(console, 'warn').mockImplementation();
2019
Logger.log(LOG_LEVEL.WARNING, 'Test');
21-
expect(spy.called).toBe(true);
22-
spy.restore();
20+
expect(spy).toBeCalled();
21+
spy.mockRestore();
2322
});
2423
test('info', () => {
25-
const spy = sinon.spy(console, 'info');
24+
const spy = jest.spyOn(console, 'info').mockImplementation();
2625
Logger.log(LOG_LEVEL.INFO, 'Test');
27-
expect(spy.called).toBe(true);
28-
spy.restore();
26+
expect(spy).toBeCalled();
27+
spy.mockRestore();
2928
});
3029
test('debug', () => {
31-
const spy = sinon.spy(console, 'debug');
30+
const spy = jest.spyOn(console, 'debug').mockImplementation();
3231
Logger.log(LOG_LEVEL.DEBUG, 'Test');
33-
expect(spy.called).toBe(true);
34-
spy.restore();
32+
expect(spy).toBeCalled();
33+
spy.mockRestore();
3534
});
3635
test('trace', () => {
37-
const spy = sinon.spy(console, 'trace');
36+
const spy = jest.spyOn(console, 'trace').mockImplementation();
3837
Logger.log(LOG_LEVEL.TRACE, 'Test');
39-
expect(spy.called).toBe(true);
40-
spy.restore();
38+
expect(spy).toBeCalled();
39+
spy.mockRestore();
4140
});
4241

4342
test('Not to crash if no console', () => {
@@ -51,40 +50,40 @@ describe('Logger', () => {
5150
const { debug, trace } = window.console;
5251
modifyObjectProperty(window.console, 'debug', false);
5352
modifyObjectProperty(window.console, 'trace', false);
54-
const spy = sinon.spy(console, 'log');
53+
const spy = jest.spyOn(console, 'log').mockImplementation();
5554
// @ts-ignore
5655
Logger.trace();
57-
expect(spy.called).toBe(true);
56+
expect(spy).toBeCalled();
5857
Logger.debug('debug');
59-
expect(spy.calledTwice).toBe(true);
60-
spy.restore();
58+
expect(spy).toBeCalledTimes(2);
59+
spy.mockRestore();
6160
modifyObjectProperty(window.console, 'debug', debug);
6261
modifyObjectProperty(window.console, 'trace', trace);
6362
});
6463
test('iOS specific', () => {
6564
const { info } = window.console;
6665
modifyObjectProperty(window.console, 'info', false);
67-
const spy = sinon.spy(console, 'log');
66+
const spy = jest.spyOn(console, 'log').mockImplementation();
6867
Logger.info('info');
69-
expect(spy.called).toBe(true);
70-
spy.restore();
68+
expect(spy).toBeCalled();
69+
spy.mockRestore();
7170
modifyObjectProperty(window.console, 'info', info);
7271
});
7372

7473
test('Public API', () => {
75-
const spy = sinon.spy(Logger, 'log');
74+
const spy = jest.spyOn(Logger, 'log').mockImplementation();
7675
Logger.fatal('fatal', 'test');
77-
expect(spy.calledWith(LOG_LEVEL.FATAL, 'fatal', 'test')).toBe(true);
76+
expect(spy).toBeCalledWith(LOG_LEVEL.FATAL, 'fatal', 'test');
7877
Logger.error('error', 'test');
79-
expect(spy.calledWith(LOG_LEVEL.ERROR, 'error', 'test')).toBe(true);
78+
expect(spy).toBeCalledWith(LOG_LEVEL.ERROR, 'error', 'test');
8079
Logger.warning('warning', 'test');
81-
expect(spy.calledWith(LOG_LEVEL.WARNING, 'warning', 'test')).toBe(true);
80+
expect(spy).toBeCalledWith(LOG_LEVEL.WARNING, 'warning', 'test');
8281
Logger.info('info', 'test');
83-
expect(spy.calledWith(LOG_LEVEL.INFO, 'info', 'test')).toBe(true);
82+
expect(spy).toBeCalledWith(LOG_LEVEL.INFO, 'info', 'test');
8483
Logger.debug('debug', 'test');
85-
expect(spy.calledWith(LOG_LEVEL.DEBUG, 'debug', 'test')).toBe(true);
84+
expect(spy).toBeCalledWith(LOG_LEVEL.DEBUG, 'debug', 'test');
8685
Logger.trace('trace', 'test');
87-
expect(spy.calledWith(LOG_LEVEL.TRACE, 'trace', 'test')).toBe(true);
86+
expect(spy).toBeCalledWith(LOG_LEVEL.TRACE, 'trace', 'test');
8887
});
8988

9089
test('Custom Component', () => {

packages/main/src/components/AutoCloseOnOutsideClick/AutoCloseOnOutsideClick.test.tsx

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages/main/src/components/AutoCloseOnOutsideClick/index.tsx

Lines changed: 0 additions & 53 deletions
This file was deleted.

packages/main/src/components/FlexBox/FlexBox.test.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { mount } from 'enzyme';
33
import { FlexBox } from '@ui5/webcomponents-react/lib/FlexBox';
44
import { FlexBoxJustifyContent } from '@ui5/webcomponents-react/lib/FlexBoxJustifyContent';
55
import * as React from 'react';
6-
import sinon from 'sinon';
76

87
describe('FlexBox', () => {
98
test('JustifyContent: End', () => {
@@ -46,17 +45,5 @@ describe('FlexBox', () => {
4645
expect(wrapper.render()).toMatchSnapshot();
4746
});
4847

49-
test('pass through click handler', () => {
50-
const callback = sinon.spy();
51-
const wrapper = mount(
52-
// @ts-ignore
53-
<FlexBox onClick={callback}>
54-
<span>Test 1</span>
55-
</FlexBox>
56-
);
57-
wrapper.simulate('click');
58-
expect(callback.called).toBe(true);
59-
});
60-
6148
createPassThroughPropsTest(FlexBox);
6249
});

packages/main/src/components/ObjectPage/ObjectPage.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getEventFromCallback } from '@shared/tests/utils';
21
import { mount } from 'enzyme';
32
import { Breadcrumbs } from '@ui5/webcomponents-react/lib/Breadcrumbs';
43
import { Button } from '@ui5/webcomponents-react/lib/Button';
@@ -12,7 +11,6 @@ import { Text } from '@ui5/webcomponents-react/lib/Text';
1211
import { Title } from '@ui5/webcomponents-react/lib/Title';
1312
import { TitleLevel } from '@ui5/webcomponents-react/lib/TitleLevel';
1413
import React from 'react';
15-
import * as sinon from 'sinon';
1614

1715
const headerContent = (
1816
<div style={{ display: 'flex', flexDirection: 'column' }}>
@@ -153,15 +151,15 @@ describe('ObjectPage', () => {
153151
});
154152

155153
test.skip('onSelectedSectionChangedHandler', () => {
156-
const callback = sinon.spy();
154+
const callback = jest.fn();
157155
const wrapper = mount(
158156
<ObjectPage selectedSectionId={'2'} mode={ObjectPageMode.IconTabBar} onSelectedSectionChanged={callback}>
159157
<ObjectPageSection id={'1'}>Test</ObjectPageSection>
160158
<ObjectPageSection id={'2'}>Test 2</ObjectPageSection>
161159
</ObjectPage>
162160
);
163161
wrapper.find('section[role="navigation"] ui5-button').first().simulate('click');
164-
expect(getEventFromCallback(callback).detail.selectedSectionId).toEqual('1');
162+
expect(callback.mock[0][0].detail.selectedSectionId).toEqual('1');
165163
});
166164

167165
test('No Header', () => {

scripts/rollup/configFactory.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const { asyncCopyTo, highlightLog } = require('../utils');
1010
const replace = require('@rollup/plugin-replace');
1111
const glob = require('glob');
1212
const { terser } = require('rollup-plugin-terser');
13-
const sh = require('shelljs');
1413
const { spawnSync } = require('child_process');
1514

1615
process.env.BABEL_ENV = 'production';

0 commit comments

Comments
 (0)