Skip to content

Commit 8059861

Browse files
committed
test: more tests for useSprings
1 parent 8e9b94b commit 8059861

File tree

1 file changed

+77
-6
lines changed

1 file changed

+77
-6
lines changed

packages/core/src/useSprings.test.tsx

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react'
2-
import { Controller } from './Controller'
32
import { render, RenderResult } from '@testing-library/react'
43
import { useSprings } from './useSprings'
5-
import { is } from 'shared'
4+
import { is, each, Indexable } from 'shared'
65
import { SpringsUpdateFn, SpringStopFn } from './types/spring'
6+
import { SpringValue } from './SpringValue'
77

88
describe('useSprings', () => {
9-
let springs: Controller[]
9+
let springs: Indexable<SpringValue>[]
1010
let animate: SpringsUpdateFn<any>
1111
let stop: SpringStopFn<any>
1212

@@ -26,25 +26,95 @@ describe('useSprings', () => {
2626
return null
2727
})
2828

29+
describe('when only a props function is passed', () => {
30+
it('calls the props function once per new spring', () => {
31+
const getProps = jest.fn((i: number) => ({ x: i * 100 }))
32+
33+
// Create two springs.
34+
update(2, getProps)
35+
expect(getProps).toHaveBeenCalledTimes(2)
36+
expect(springs.length).toBe(2)
37+
38+
// Do nothing.
39+
update(2, getProps)
40+
expect(getProps).toHaveBeenCalledTimes(2)
41+
expect(springs.length).toBe(2)
42+
43+
// Create a spring.
44+
update(3, getProps)
45+
expect(getProps).toHaveBeenCalledTimes(3)
46+
expect(springs.length).toBe(3)
47+
48+
// Remove a spring.
49+
update(2, getProps)
50+
expect(getProps).toHaveBeenCalledTimes(3)
51+
expect(springs.length).toBe(2)
52+
53+
// Create two springs.
54+
update(4, getProps)
55+
expect(getProps).toHaveBeenCalledTimes(5)
56+
expect(springs.length).toBe(4)
57+
})
58+
})
59+
60+
describe('when both a props function and a deps array are passed', () => {
61+
it('updates each spring when the deps have changed', () => {
62+
const getProps = jest.fn((i: number) => ({ x: i * 100 }))
63+
64+
update(2, getProps, [1])
65+
expect(getProps).toHaveBeenCalledTimes(2)
66+
67+
update(2, getProps, [1])
68+
expect(getProps).toHaveBeenCalledTimes(2)
69+
70+
update(2, getProps, [2])
71+
expect(getProps).toHaveBeenCalledTimes(4)
72+
})
73+
})
74+
75+
describe('when only a props array is passed', () => {
76+
it('updates each spring on every render', () => {
77+
update(2, [{ x: 0 }, { x: 0 }])
78+
expect(mapSprings(s => s.goal)).toEqual([{ x: 0 }, { x: 0 }])
79+
80+
update(3, [{ x: 1 }, { x: 2 }, { x: 3 }])
81+
expect(mapSprings(s => s.goal)).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }])
82+
})
83+
})
84+
2985
describe('when the length argument increases', () => {
30-
it('creates new spring objects', () => {
86+
it('creates new springs', () => {
3187
const getProps = (i: number) => ({ x: i * 100 })
88+
3289
update(0, getProps)
3390
expect(springs.length).toBe(0)
91+
3492
update(2, getProps)
3593
expect(springs.length).toBe(2)
3694
})
3795
})
3896

3997
describe('when the length argument decreases', () => {
40-
it('removes excess spring objects', () => {
98+
it('removes old springs', () => {
4199
const getProps = (i: number) => ({ x: i * 100 })
100+
42101
update(3, getProps)
43102
expect(springs.length).toBe(3)
103+
44104
update(1, getProps)
45105
expect(springs.length).toBe(1)
46106
})
47107
})
108+
109+
function mapSprings<T>(fn: (spring: SpringValue) => T) {
110+
return springs.map(values => {
111+
const result: any = {}
112+
each(values, spring => {
113+
result[spring.key!] = fn(spring)
114+
})
115+
return result
116+
})
117+
}
48118
})
49119

50120
function createUpdater(
@@ -55,7 +125,8 @@ function createUpdater(
55125
result = undefined
56126
})
57127

58-
return (...args: Parameters<typeof useSprings>) => {
128+
type Args = [number, any[] | ((i: number) => any), any[]?]
129+
return (...args: Args) => {
59130
const elem = <Component args={args} />
60131
if (result) result.rerender(elem)
61132
else result = render(elem)

0 commit comments

Comments
 (0)