Skip to content

Commit b4272bd

Browse files
committed
test: the SpringContext component
1 parent f111d0d commit b4272bd

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React from 'react'
2+
import { render, RenderResult } from '@testing-library/react'
3+
import { SpringContext } from './SpringContext'
4+
import { SpringValue } from './SpringValue'
5+
import { useSpring } from './hooks'
6+
7+
describe('SpringContext', () => {
8+
let t: SpringValue<number>
9+
10+
const Child = () => {
11+
t = useSpring({ t: 1, from: { t: 0 } }).t
12+
return null
13+
}
14+
15+
const update = createUpdater(props => (
16+
<SpringContext {...props}>
17+
<Child />
18+
</SpringContext>
19+
))
20+
21+
it('can cancel current animations', () => {
22+
update({})
23+
mockRaf.step()
24+
expect(t.idle).toBeFalsy()
25+
update({ cancel: true })
26+
expect(t.idle).toBeTruthy()
27+
})
28+
it('can cancel future animations', async () => {
29+
update({ cancel: true })
30+
expect(t.idle).toBeTruthy()
31+
const { cancelled } = await t.start(100)
32+
expect(cancelled).toBeTruthy()
33+
expect(t.idle).toBeTruthy()
34+
expect(t.goal).toBe(0)
35+
})
36+
37+
it('can pause current animations', () => {
38+
update({})
39+
mockRaf.step()
40+
expect(t.idle).toBeFalsy()
41+
42+
update({ pause: true })
43+
expect(t.idle).toBeTruthy()
44+
expect(t.goal).toBe(1)
45+
46+
update({ pause: false })
47+
expect(t.idle).toBeFalsy()
48+
expect(t.goal).toBe(1)
49+
})
50+
it('can pause future animations', () => {
51+
// Paused right away.
52+
update({ pause: true })
53+
expect(t.idle).toBeTruthy()
54+
expect(t.goal).toBeUndefined()
55+
56+
// This update is paused too.
57+
t.start(2)
58+
expect(t.idle).toBeTruthy()
59+
expect(t.goal).toBeUndefined()
60+
61+
// Let it roll.
62+
update({ pause: false })
63+
expect(t.idle).toBeFalsy()
64+
// The `goal` is not 2, because the `useSpring` hook is
65+
// executed by the SpringContext update.
66+
expect(t.goal).toBe(1)
67+
})
68+
69+
it('can make current animations immediate', () => {
70+
update({})
71+
mockRaf.step()
72+
expect(t.idle).toBeFalsy()
73+
74+
update({ immediate: true })
75+
mockRaf.step()
76+
77+
expect(t.idle).toBeTruthy()
78+
expect(t.get()).toBe(1)
79+
})
80+
it('can make future animations immediate', () => {
81+
update({ immediate: true })
82+
mockRaf.step()
83+
84+
expect(t.idle).toBeTruthy()
85+
expect(t.get()).toBe(1)
86+
87+
t.start(2)
88+
mockRaf.step()
89+
90+
expect(t.idle).toBeTruthy()
91+
expect(t.get()).toBe(2)
92+
})
93+
})
94+
95+
function createUpdater(Component: React.ComponentType<SpringContext>) {
96+
let result: RenderResult | undefined
97+
afterEach(() => {
98+
result = undefined
99+
})
100+
return (props: SpringContext) => {
101+
const elem = <Component {...props} />
102+
if (result) result.rerender(elem)
103+
else result = render(elem)
104+
return result
105+
}
106+
}

0 commit comments

Comments
 (0)