File size: 5,103 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
import * as React from 'react'
import '@testing-library/jest-dom'
import { RenderResult, render } from '@testing-library/react'
import { toArray } from '@react-spring/shared'
import { TransitionFn, UseTransitionProps } from '../types'
import { useTransition } from './useTransition'
import { SpringRef } from '../SpringRef'
describe('useTransition', () => {
let transition: TransitionFn
let rendered: any[]
afterEach(() => {
result = undefined
})
// Call the "useTransition" hook and update local variables.
const update = createUpdater(({ args }) => {
transition = toArray(useTransition(...args))[0]
rendered = transition((_, item) => item).props.children
return null
})
it('unmounts after leave', async () => {
const props = {
from: { n: 0 },
enter: { n: 1 },
leave: { n: 0 },
}
update(true, props)
expect(rendered).toEqual([true])
global.mockRaf.step()
update(false, props)
expect(rendered).toEqual([true, false])
await global.advanceUntilIdle()
expect(rendered).toEqual([false])
})
describe('when "leave" is an array', () => {
it('unmounts after leave', async () => {
const props = {
from: { n: 0 },
enter: { n: 1 },
leave: [{ n: 0 }],
}
update(true, props)
expect(rendered).toEqual([true])
global.mockRaf.step()
update(false, props)
expect(rendered).toEqual([true, false])
await global.advanceUntilIdle()
expect(rendered).toEqual([false])
})
})
describe('when "leave" is a function', () => {
it('unmounts after leave', async () => {
const props: UseTransitionProps = {
from: { n: 0 },
enter: { n: 1 },
leave: () => async next => {
await next({ n: 0 })
},
}
update(true, props)
expect(rendered).toEqual([true])
global.mockRaf.step()
update(false, props)
expect(rendered).toEqual([true, false])
await global.advanceUntilIdle()
expect(rendered).toEqual([false])
})
})
describe('when "enter" is a function', () => {
it('still has its "onRest" prop called', async () => {
const onRest = jest.fn()
update(true, {
from: { x: 0 },
enter: () => ({
x: 1,
onRest,
}),
})
await global.advanceUntilIdle()
expect(onRest).toBeCalledTimes(1)
})
})
describe('when "leave" is a no-op update', () => {
it('still unmounts the transition', async () => {
const props = {
from: { t: 0 },
enter: { t: 1 },
leave: { t: 1 },
}
update(true, props)
expect(rendered).toEqual([true])
await global.advanceUntilIdle()
update(false, props)
expect(rendered).toEqual([true, false])
await global.advanceUntilIdle()
expect(rendered).toEqual([false])
})
})
it('assign controllers to provided "ref"', async () => {
const ref = SpringRef()
const props = {
ref,
}
const children = [<div key={1} />, <div key={2} />, <div key={3} />]
update(children, props)
expect(ref.current).toHaveLength(3)
testIsRef(ref)
})
it('returns a ref if the props argument is a function', () => {
let transRef: SpringRef | null = null
const update = createUpdater(({ args }) => {
const [transition, ref] = useTransition(...args)
rendered = transition((_, item) => item).props.children
transRef = ref
return null
})
update(true, () => ({
from: { n: 0 },
enter: { n: 1 },
leave: { n: 0 },
}))
expect(rendered).toEqual([true])
testIsRef(transRef)
})
it('passes immediate through to payload', async () => {
const props = {
from: { n: 0 },
enter: { n: 1 },
leave: { n: 0 },
immediate: true,
}
update(true, props)
transition(style => {
expect(style.n.animation.immediate).toEqual(true)
return null
})
})
it('should allow items to leave before entering new items when exitBeforeEnter is true', async () => {
const props = {
from: { t: 0 },
enter: { t: 1 },
leave: { t: 1 },
exitBeforeEnter: true,
}
update(0, props)
expect(rendered).toEqual([0])
global.mockRaf.step()
update(1, props)
global.mockRaf.step()
expect(rendered).toEqual([0])
await global.advanceUntilIdle()
expect(rendered).toEqual([1])
})
})
let result: RenderResult | undefined
function createUpdater(
Component: React.ComponentType<{ args: [any, any, any?] }>
) {
type Args = [any, UseTransitionProps | (() => UseTransitionProps), any[]?]
return (...args: Args) => {
const elem = <Component args={args} />
if (result) result.rerender(elem)
else result = render(elem)
return result
}
}
function testIsRef(ref: SpringRef | null) {
const props = [
'add',
'delete',
'pause',
'resume',
'set',
'start',
'stop',
'update',
'_getProps',
]
props.forEach(prop => expect(ref).toHaveProperty(prop))
}
|