File size: 5,615 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import * as React from 'react'
import { render, RenderResult } from '@testing-library/react'
import { is, eachProp } from '@react-spring/shared'
import { Lookup } from '@react-spring/types'
import { SpringRef } from '../SpringRef'
import { SpringValue } from '../SpringValue'
import { useSprings } from './useSprings'
describe('useSprings', () => {
const isStrictMode = true
const strictModeFunctionCallMultiplier = isStrictMode ? 2 : 1
let springs: Lookup<SpringValue>[]
let ref: SpringRef
// Call the "useSprings" hook and update local variables.
const update = createUpdater(({ args }) => {
const result = useSprings(...args)
if (is.fun(args[1]) || args.length == 3) {
springs = result[0] as any
ref = result[1]
} else {
springs = result as any
ref = undefined as any
}
return null
}, isStrictMode)
describe('when only a props function is passed', () => {
it('should reach final value in strict mode', async () => {
update(1, () => ({
from: { x: 0 },
to: { x: 1 },
}))
expect(mapSprings(s => s.goal)).toEqual([{ x: 1 }])
})
it('calls the props function once per new spring', () => {
const getProps = jest.fn((i: number) => ({ x: i * 100 }))
// Create two springs.
update(2, getProps)
expect(getProps).toHaveBeenCalledTimes(
2 * strictModeFunctionCallMultiplier
)
expect(springs.length).toBe(2)
expect(ref.current.length).toBe(2)
// Do nothing.
update(2, getProps)
expect(getProps).toHaveBeenCalledTimes(
2 * strictModeFunctionCallMultiplier
)
expect(springs.length).toBe(2)
expect(ref.current.length).toBe(2)
// Create a spring.
update(3, getProps)
expect(getProps).toHaveBeenCalledTimes(
3 * strictModeFunctionCallMultiplier
)
expect(springs.length).toBe(3)
expect(ref.current.length).toBe(3)
// Remove a spring.
update(2, getProps)
expect(getProps).toHaveBeenCalledTimes(
3 * strictModeFunctionCallMultiplier
)
expect(springs.length).toBe(2)
expect(ref.current.length).toBe(2)
// Create two springs.
update(4, getProps)
expect(getProps).toHaveBeenCalledTimes(
5 * strictModeFunctionCallMultiplier
)
expect(springs.length).toBe(4)
expect(ref.current.length).toBe(4)
})
})
describe('when both a props function and a deps array are passed', () => {
it('updates each spring when the deps have changed', () => {
const getProps = jest.fn((i: number) => ({ x: i * 100 }))
update(2, getProps, [1])
expect(getProps).toHaveBeenCalledTimes(
2 * strictModeFunctionCallMultiplier
)
update(2, getProps, [1])
expect(getProps).toHaveBeenCalledTimes(
2 * strictModeFunctionCallMultiplier
)
update(2, getProps, [2])
expect(getProps).toHaveBeenCalledTimes(
4 * strictModeFunctionCallMultiplier
)
})
})
describe('when only a props array is passed', () => {
it('updates each spring on every render', () => {
update(2, [{ x: 0 }, { x: 0 }])
expect(mapSprings(s => s.goal)).toEqual([{ x: 0 }, { x: 0 }])
update(3, [{ x: 1 }, { x: 2 }, { x: 3 }])
expect(mapSprings(s => s.goal)).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }])
})
})
describe('when the length argument increases', () => {
it('creates new springs', () => {
const getProps = (i: number) => ({ x: i * 100 })
update(0, getProps, [[]])
expect(springs.length).toBe(0)
expect(ref.current.length).toBe(0)
update(2, getProps, [[1, 2]])
expect(springs.length).toBe(2)
expect(ref.current.length).toBe(2)
update(0, getProps, [[]])
expect(springs.length).toBe(0)
expect(ref.current.length).toBe(0)
update(2, getProps, [[1, 2]])
expect(springs.length).toBe(2)
expect(ref.current.length).toBe(2)
update(1, getProps, [[1]])
expect(springs.length).toBe(1)
expect(ref.current.length).toBe(1)
update(2, getProps, [[1, 2]])
expect(springs.length).toBe(2)
expect(ref.current.length).toBe(2)
})
})
describe('when the length argument decreases', () => {
it('removes old springs', () => {
const getProps = (i: number) => ({ x: i * 100 })
update(3, getProps)
expect(springs.length).toBe(3)
expect(ref.current.length).toBe(3)
update(1, getProps)
expect(springs.length).toBe(1)
expect(ref.current.length).toBe(1)
update(3, getProps)
expect(springs.length).toBe(3)
expect(ref.current.length).toBe(3)
update(1, getProps)
expect(springs.length).toBe(1)
expect(ref.current.length).toBe(1)
})
})
function mapSprings<T>(fn: (spring: SpringValue) => T) {
return springs.map(values => {
const result: any = {}
eachProp(values, spring => {
result[spring.key!] = fn(spring)
})
return result
})
}
})
function createUpdater(
Component: React.ComponentType<{ args: [any, any, any?] }>,
isStrictMode: boolean
) {
let result: RenderResult | undefined
afterEach(() => {
result = undefined
})
type Args = [number, any[] | ((i: number) => any), any[]?]
return (...args: Args) => {
const component = <Component args={args} />
const elem = isStrictMode ? (
<React.StrictMode>{component}</React.StrictMode>
) : (
component
)
if (result) result.rerender(elem)
else result = render(elem)
return result
}
}
|