Skip to content

Commit 923f408

Browse files
committed
Update config of jest for typescript
1 parent ca56191 commit 923f408

File tree

7 files changed

+50
-34
lines changed

7 files changed

+50
-34
lines changed

babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', {targets: {node: 'current'}}],
4+
'@babel/preset-typescript',
5+
],
6+
};

jest.config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ module.exports = {
1010
collectCoverage: true,
1111
coverageThreshold: {
1212
global: {
13-
branches: 100,
13+
branches: 80,
1414
functions: 100,
1515
lines: 100,
1616
statements: 100
1717
}
1818
},
1919
moduleFileExtensions: ['ts', 'tsx', 'js'],
2020
transform: {
21-
'^.+\\.(ts|tsx)$': '<rootDir>/lib/test/test-preprocessor.js'
21+
'^.+\\.(ts|tsx)$': 'ts-jest'
2222
},
23-
setupFiles: ['<rootDir>/lib/test/test-shim.js', '<rootDir>/lib/test/test-setup.js']
23+
setupFiles: ['<rootDir>/lib/test/test-setup.js'],
24+
verbose: true
2425
};

lib/test/test-preprocessor.js

-17
This file was deleted.

lib/test/test-shim.js

-9
This file was deleted.

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
"author": "David Nguyen <[email protected]> ([email protected])",
3434
"license": "MIT",
3535
"devDependencies": {
36+
"@babel/core": "^7.7.7",
37+
"@babel/preset-env": "^7.7.7",
38+
"@babel/preset-typescript": "^7.7.7",
3639
"@types/enzyme": "^3.10.4",
3740
"@types/jest": "^24.0.23",
3841
"@types/react": "^16.9.2",
@@ -41,6 +44,7 @@
4144
"@types/throttle-debounce": "^2.1.0",
4245
"@typescript-eslint/eslint-plugin": "^2.11.0",
4346
"@typescript-eslint/parser": "^2.11.0",
47+
"babel-jest": "^24.9.0",
4448
"body-parser": "^1.19.0",
4549
"clean-webpack-plugin": "^3.0.0",
4650
"compression": "^1.7.4",

src/__tests__/viewport.test.tsx

+35-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import * as sinon from 'sinon';
44
import { Viewport } from '../';
55
import * as Utils from '../utils';
66

7-
const resizeHeightWindow = height => {
7+
function resizeWindow(height?: number, width?: number) {
88
Object.defineProperty(window, 'innerHeight', {
99
writable: true,
1010
configurable: true,
1111
value: height
1212
});
13+
14+
Object.defineProperty(window, 'innerWidth', {
15+
writable: true,
16+
configurable: true,
17+
value: width
18+
});
19+
1320
window.dispatchEvent(new Event('resize'));
14-
};
21+
}
1522

1623
const WINDOW_INNER_HEIGHT = 2000;
1724

@@ -25,7 +32,7 @@ describe('testing Viewport component', () => {
2532
let onLeave;
2633

2734
beforeEach(() => {
28-
resizeHeightWindow(WINDOW_INNER_HEIGHT);
35+
resizeWindow(WINDOW_INNER_HEIGHT);
2936
eventMap = {};
3037

3138
// sinon sandbox
@@ -51,7 +58,7 @@ describe('testing Viewport component', () => {
5158
jest.clearAllMocks();
5259
});
5360

54-
it('should should increase enterCount to 2 && leaveCount 1 with "fit" mode', () => {
61+
it('should should increase enterCount to 2 && leaveCount 1 with "fit" mode - window', () => {
5562
const wrapper = mount(
5663
<Viewport
5764
onEnter={enterCount => onEnter(enterCount)}
@@ -92,6 +99,30 @@ describe('testing Viewport component', () => {
9299
expect(onEnter.getCall(1).args[0]).toBe(2);
93100
});
94101

102+
it('should should increase enterCount to 1 with "fit" mode - document', () => {
103+
resizeWindow(undefined, undefined);
104+
const wrapper = mount(
105+
<Viewport
106+
onEnter={enterCount => onEnter(enterCount)}
107+
onLeave={leaveCount => onLeave(leaveCount)}
108+
>
109+
hello world
110+
</Viewport>
111+
);
112+
const { delay, type } = wrapper.props();
113+
expect(delay).toBe(100);
114+
expect(type).toBe('fit');
115+
116+
// Enter component first time
117+
isFittedIn.returns(true);
118+
eventMap.scroll();
119+
120+
// Wait for throttling delay
121+
clock.tick(delay);
122+
expect(onEnter.called).toBeTruthy();
123+
expect(onEnter.getCall(0).args[0]).toBe(1);
124+
});
125+
95126
it('should should increase enterCount to 2 && leaveCount 1 with "overlap" mode', () => {
96127
const wrapper = mount(
97128
<Viewport

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"jsx":"react",
1717
"moduleResolution":"node",
1818
"allowSyntheticDefaultImports": true,
19-
"noImplicitAny": true,
19+
"noImplicitAny": false,
2020
"noImplicitThis": true,
2121
"noImplicitReturns": true,
2222
"noUnusedLocals": true,

0 commit comments

Comments
 (0)