1
1
import React from 'react'
2
- import { Controller } from './Controller'
3
2
import { render , RenderResult } from '@testing-library/react'
4
3
import { useSprings } from './useSprings'
5
- import { is } from 'shared'
4
+ import { is , each , Indexable } from 'shared'
6
5
import { SpringsUpdateFn , SpringStopFn } from './types/spring'
6
+ import { SpringValue } from './SpringValue'
7
7
8
8
describe ( 'useSprings' , ( ) => {
9
- let springs : Controller [ ]
9
+ let springs : Indexable < SpringValue > [ ]
10
10
let animate : SpringsUpdateFn < any >
11
11
let stop : SpringStopFn < any >
12
12
@@ -26,25 +26,95 @@ describe('useSprings', () => {
26
26
return null
27
27
} )
28
28
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
+
29
85
describe ( 'when the length argument increases' , ( ) => {
30
- it ( 'creates new spring objects ' , ( ) => {
86
+ it ( 'creates new springs ' , ( ) => {
31
87
const getProps = ( i : number ) => ( { x : i * 100 } )
88
+
32
89
update ( 0 , getProps )
33
90
expect ( springs . length ) . toBe ( 0 )
91
+
34
92
update ( 2 , getProps )
35
93
expect ( springs . length ) . toBe ( 2 )
36
94
} )
37
95
} )
38
96
39
97
describe ( 'when the length argument decreases' , ( ) => {
40
- it ( 'removes excess spring objects ' , ( ) => {
98
+ it ( 'removes old springs ' , ( ) => {
41
99
const getProps = ( i : number ) => ( { x : i * 100 } )
100
+
42
101
update ( 3 , getProps )
43
102
expect ( springs . length ) . toBe ( 3 )
103
+
44
104
update ( 1 , getProps )
45
105
expect ( springs . length ) . toBe ( 1 )
46
106
} )
47
107
} )
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
+ }
48
118
} )
49
119
50
120
function createUpdater (
@@ -55,7 +125,8 @@ function createUpdater(
55
125
result = undefined
56
126
} )
57
127
58
- return ( ...args : Parameters < typeof useSprings > ) => {
128
+ type Args = [ number , any [ ] | ( ( i : number ) => any ) , any [ ] ?]
129
+ return ( ...args : Args ) => {
59
130
const elem = < Component args = { args } />
60
131
if ( result ) result . rerender ( elem )
61
132
else result = render ( elem )
0 commit comments