Skip to content

Commit e7ca958

Browse files
committed
add tests
1 parent 1960845 commit e7ca958

File tree

5 files changed

+123
-8
lines changed

5 files changed

+123
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@
4545
"cross-env": "^5.1.4",
4646
"ease-component": "^1.0.0",
4747
"gh-pages": "^1.2.0",
48+
"mock-raf": "^1.0.0",
4849
"raf": "^3.4.0",
4950
"react": "^16.4.1",
5051
"react-dom": "^16.4.1",
5152
"react-scripts-ts": "^2.16.0",
53+
"react-testing-library": "^5.2.0",
5254
"rollup": "^0.62.0",
5355
"rollup-plugin-babel": "^3.0.7",
5456
"rollup-plugin-commonjs": "^9.1.3",

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const easeOutCubic: Easing = n => --n * n * n + 1
4747

4848
class Counter extends React.Component<Props, State> {
4949
public state = {
50-
value: 0,
50+
value: this.props.from || 0,
5151
start: Date.now(),
5252
isAnimating: true,
5353
}
@@ -63,6 +63,7 @@ class Counter extends React.Component<Props, State> {
6363
const duration = this.props.duration || 1000
6464
const easing = this.props.easing || easeOutCubic
6565

66+
6667
if (now - start >= duration) {
6768
this.setState({isAnimating: false})
6869
}

src/test.ts

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

src/test.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {Counter} from './'
2+
import * as T from 'react-testing-library'
3+
import * as React from 'react'
4+
5+
jest.useFakeTimers();
6+
7+
(global as any).requestAnimationFrame = function(callback: () => void) {
8+
setTimeout(callback, 0);
9+
};
10+
11+
afterEach(T.cleanup)
12+
13+
describe('ExampleComponent', () => {
14+
it('is truthy', () => {
15+
expect(Counter).toBeTruthy()
16+
})
17+
18+
it('renders the number', () => {
19+
const to = 1000
20+
const res = T.render(<Counter to={to}/>)
21+
jest.runAllTimers()
22+
expect(res.container.innerHTML).toBeTruthy()
23+
expect(res.container.firstChild).toBeTruthy();
24+
expect(res.container.firstChild && Number(res.container.firstChild.textContent)).toBe(to)
25+
})
26+
27+
it ('renders from first', () => {
28+
const from = 42
29+
const to = 84
30+
const res = T.render(<Counter from={from} to={to} />)
31+
const node = res.container.firstChild
32+
33+
expect(node && Number(node.textContent)).toBe(from)
34+
35+
jest.runAllTimers()
36+
37+
expect(node && Number(node.textContent)).toBe(to)
38+
})
39+
40+
it('works with render prop', () => {
41+
const from = 42
42+
const to = 84
43+
const res = T.render(
44+
<Counter from={from} to={to}>
45+
{value => <div data-testid="test">{value}</div>}
46+
</Counter>
47+
)
48+
49+
let actual = Number(res.getByTestId("test").innerHTML)
50+
expect(actual).toBeTruthy()
51+
expect(actual).toBe(from)
52+
53+
jest.runAllTimers()
54+
55+
actual = Number(res.getByTestId("test").innerHTML)
56+
expect(actual).toBe(to)
57+
58+
})
59+
60+
it('works with a custom easing function prop', () => {
61+
const from = 42
62+
const to = 84
63+
const res = T.render(
64+
<Counter from={from} to={to} easing={n => n}>
65+
{value => <div data-testid="test">{value}</div>}
66+
</Counter>
67+
)
68+
69+
let actual = Number(res.getByTestId("test").innerHTML)
70+
expect(actual).toBeTruthy()
71+
expect(actual).toBe(from)
72+
73+
jest.runAllTimers()
74+
75+
actual = Number(res.getByTestId("test").innerHTML)
76+
expect(actual).toBe(to)
77+
78+
})
79+
})

yarn.lock

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
esutils "^2.0.2"
1717
js-tokens "^4.0.0"
1818

19+
"@sheerun/mutationobserver-shim@^0.3.2":
20+
version "0.3.2"
21+
resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.2.tgz#8013f2af54a2b7d735f71560ff360d3a8176a87b"
22+
1923
2024
version "0.0.39"
2125
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
@@ -2152,6 +2156,14 @@ dom-serializer@0:
21522156
domelementtype "~1.1.1"
21532157
entities "~1.1.1"
21542158

2159+
dom-testing-library@^3.8.1:
2160+
version "3.11.1"
2161+
resolved "https://registry.yarnpkg.com/dom-testing-library/-/dom-testing-library-3.11.1.tgz#a2a1b0fe04700f585da9f3539ca626ab17ccf050"
2162+
dependencies:
2163+
"@sheerun/mutationobserver-shim" "^0.3.2"
2164+
pretty-format "^23.6.0"
2165+
wait-for-expect "^1.0.0"
2166+
21552167
dom-urls@^1.1.0:
21562168
version "1.1.0"
21572169
resolved "https://registry.yarnpkg.com/dom-urls/-/dom-urls-1.1.0.tgz#001ddf81628cd1e706125c7176f53ccec55d918e"
@@ -4672,6 +4684,12 @@ [email protected], mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1:
46724684
dependencies:
46734685
minimist "0.0.8"
46744686

4687+
mock-raf@^1.0.0:
4688+
version "1.0.0"
4689+
resolved "https://registry.yarnpkg.com/mock-raf/-/mock-raf-1.0.0.tgz#a288145178893e2040b230f2182fee2049f16f25"
4690+
dependencies:
4691+
object-assign "^3.0.0"
4692+
46754693
move-concurrently@^1.0.1:
46764694
version "1.0.1"
46774695
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
@@ -4922,6 +4940,10 @@ [email protected], object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^
49224940
version "4.1.1"
49234941
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
49244942

4943+
object-assign@^3.0.0:
4944+
version "3.0.0"
4945+
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-3.0.0.tgz#9bedd5ca0897949bca47e7ff408062d549f587f2"
4946+
49254947
object-copy@^0.1.0:
49264948
version "0.1.0"
49274949
resolved "https://registry.yarnpkg.com/object-copy/-/object-copy-0.1.0.tgz#7e7d858b781bd7c991a41ba975ed3812754e998c"
@@ -5589,6 +5611,13 @@ pretty-format@^22.4.0, pretty-format@^22.4.3:
55895611
ansi-regex "^3.0.0"
55905612
ansi-styles "^3.2.0"
55915613

5614+
pretty-format@^23.6.0:
5615+
version "23.6.0"
5616+
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-23.6.0.tgz#5eaac8eeb6b33b987b7fe6097ea6a8a146ab5760"
5617+
dependencies:
5618+
ansi-regex "^3.0.0"
5619+
ansi-styles "^3.2.0"
5620+
55925621
private@^0.1.6, private@^0.1.8:
55935622
version "0.1.8"
55945623
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
@@ -5837,6 +5866,13 @@ react-scripts-ts@^2.16.0:
58375866
optionalDependencies:
58385867
fsevents "^1.1.3"
58395868

5869+
react-testing-library@^5.2.0:
5870+
version "5.2.0"
5871+
resolved "https://registry.yarnpkg.com/react-testing-library/-/react-testing-library-5.2.0.tgz#d7af9b0e6d1b653287d8c06cd0eab9e6d5d645ba"
5872+
dependencies:
5873+
dom-testing-library "^3.8.1"
5874+
wait-for-expect "^1.0.0"
5875+
58405876
react@^16.4.1:
58415877
version "16.5.2"
58425878
resolved "https://registry.yarnpkg.com/react/-/react-16.5.2.tgz#19f6b444ed139baa45609eee6dc3d318b3895d42"
@@ -7385,6 +7421,10 @@ w3c-hr-time@^1.0.1:
73857421
dependencies:
73867422
browser-process-hrtime "^0.1.2"
73877423

7424+
wait-for-expect@^1.0.0:
7425+
version "1.0.1"
7426+
resolved "https://registry.yarnpkg.com/wait-for-expect/-/wait-for-expect-1.0.1.tgz#73ab346ed56ed2ef66c380a59fd623755ceac0ce"
7427+
73887428
walker@~1.0.5:
73897429
version "1.0.7"
73907430
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb"

0 commit comments

Comments
 (0)