Skip to content

Commit a6a0b7c

Browse files
committed
added jest and basic tets
1 parent 5a1bbbb commit a6a0b7c

File tree

4 files changed

+1651
-80
lines changed

4 files changed

+1651
-80
lines changed

__tests__/index.test.jsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import { act } from 'react-dom/test-utils';
4+
import ReactTimer from '../src';
5+
6+
test('returns correct value after 3 seconds', async () => {
7+
jest.useFakeTimers();
8+
const ModalWrapperComponent = mount(
9+
<ReactTimer
10+
start={30}
11+
end={(value) => value < 25}
12+
onTick={(value) => value - 1}
13+
>
14+
{(time) => <div>{time}</div>}
15+
</ReactTimer>,
16+
);
17+
expect(ModalWrapperComponent.find('div').prop('children')).toEqual(30);
18+
act(() => {
19+
jest.advanceTimersByTime(3000);
20+
ModalWrapperComponent.update();
21+
});
22+
expect(ModalWrapperComponent.find('div').prop('children')).toEqual(27);
23+
});
24+
25+
26+
test('returns correct value after reaching the end of the timer', async () => {
27+
jest.useFakeTimers();
28+
const ModalWrapperComponent = mount(
29+
<ReactTimer
30+
start={30}
31+
end={(value) => value < 25}
32+
onTick={(value) => value - 1}
33+
>
34+
{(time) => <div>{time}</div>}
35+
</ReactTimer>,
36+
);
37+
expect(ModalWrapperComponent.find('div').prop('children')).toEqual(30);
38+
act(() => {
39+
jest.advanceTimersByTime(7000);
40+
ModalWrapperComponent.update();
41+
});
42+
expect(ModalWrapperComponent.find('div').prop('children')).toEqual(24);
43+
});

package.json

+16-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"docz:dev": "docz dev",
4141
"docz:build": "docz build",
4242
"docz:deploy": "yarn docz:build && gh-pages -d .docz/dist",
43-
"lint:test": "yarn eslint ./src"
43+
"lint:test": "yarn eslint ./src",
44+
"jest": "jest"
4445
},
4546
"peerDependencies": {
4647
"prop-types": "^15.5.4",
@@ -54,26 +55,32 @@
5455
"@babel/plugin-proposal-class-properties": "^7.0.0",
5556
"@babel/plugin-proposal-function-bind": "^7.0.0",
5657
"@babel/plugin-transform-runtime": "^7.0.0",
57-
"@babel/preset-env": "^7.0.0",
58+
"@babel/preset-env": "^7.6.3",
5859
"@babel/preset-flow": "^7.0.0",
59-
"@babel/preset-react": "^7.0.0",
60+
"@babel/preset-react": "^7.6.3",
6061
"@babel/preset-stage-0": "^7.0.0",
6162
"@babel/preset-stage-2": "^7.0.0",
6263
"@svgr/rollup": "^4.3.2",
6364
"babel-eslint": "^10.0.1",
65+
"babel-jest": "^24.9.0",
66+
"babel-polyfill": "^6.26.0",
6467
"cross-env": "^5.1.4",
6568
"docz": "^2.0.0-rc.53",
69+
"enzyme": "^3.10.0",
70+
"enzyme-adapter-react-16": "^1.15.1",
6671
"eslint": "^6.2.2",
6772
"eslint-config-airbnb": "^18.0.1",
6873
"eslint-plugin-import": "^2.18.2",
6974
"eslint-plugin-jsx-a11y": "^6.2.3",
7075
"eslint-plugin-react": "^7.16.0",
7176
"eslint-plugin-react-hooks": "^2.1.2",
7277
"gh-pages": "^2.1.1",
78+
"jest": "^24.9.0",
7379
"react": "^16.10.2",
7480
"react-charts": "^2.0.0-beta.4",
7581
"react-dom": "^16.10.2",
7682
"react-live": "^2.2.1",
83+
"react-test-renderer": "^16.10.2",
7784
"rollup": "^1.20.3",
7885
"rollup-plugin-babel": "^4.3.3",
7986
"rollup-plugin-commonjs": "^10.1.0",
@@ -84,5 +91,10 @@
8491
},
8592
"files": [
8693
"dist"
87-
]
94+
],
95+
"jest": {
96+
"setupFilesAfterEnv": [
97+
"./setupTests.js"
98+
]
99+
}
88100
}

setupTests.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Enzyme from 'enzyme';
2+
import Adapter from 'enzyme-adapter-react-16';
3+
import 'babel-polyfill';
4+
5+
Enzyme.configure({ adapter: new Adapter() });

0 commit comments

Comments
 (0)