File size: 4,976 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 |
/* eslint-disable no-var */
import createMockRaf, { MockRaf } from 'mock-raf'
import { flushMicroTasks } from 'flush-microtasks'
import { act } from '@testing-library/react'
import {
isEqual,
is,
colors,
frameLoop,
addFluidObserver,
removeFluidObserver,
getFluidObservers,
} from '@react-spring/shared'
import { __raf as raf } from '@react-spring/rafz'
import { Globals, Controller, FrameValue, SpringValue } from '../src/index'
import { computeGoal } from '../src/helpers'
declare global {
var mockRaf: MockRaf
var advance: (n?: number) => Promise<void>
var advanceByTime: (ms: number) => Promise<void>
var advanceUntil: (test: () => boolean) => Promise<void>
var advanceUntilIdle: () => Promise<void>
var advanceUntilValue: <T>(spring: FrameValue<T>, value: T) => Promise<void>
/** Take an array of values (one per animation frame) from internal test storage */
var getFrames: <T>(
target: FrameValue<T> | Controller<Extract<T, object>>,
preserve?: boolean
) => T[]
/** Count the number of bounces in a spring animation */
var countBounces: (spring: SpringValue<number>) => number
// @ts-ignore
var setTimeout: (handler: Function, ms: number) => number
var setSkipAnimation: (skip: boolean) => void
}
// Allow indefinite tests, since we limit the number of animation frames
// per "advanceUntil" call to 1000. This keeps the "isRunning" variable
// from interfering with the debugger.
jest.setTimeout(6e8)
let isRunning = false
let frameCache: WeakMap<any, any[]>
beforeEach(() => {
isRunning = true
frameCache = new WeakMap()
frameLoop.clear()
raf.clear()
global.mockRaf = createMockRaf()
Globals.assign({
now: global.mockRaf.now,
requestAnimationFrame: global.mockRaf.raf,
colors,
skipAnimation: false,
})
})
afterEach(() => {
isRunning = false
})
// This observes every SpringValue animation when "advanceUntil" is used.
// Any changes between frames are not recorded.
const frameObserver = (event: FrameValue.Event) => {
const spring = event.parent
if (event.type == 'change') {
let frames = frameCache.get(spring)
if (!frames) frameCache.set(spring, (frames = []))
frames.push(event.value)
}
}
global.getFrames = (target, preserve) => {
let frames = frameCache.get(target)!
if (!preserve) {
frameCache.delete(target)
}
if (!frames) {
frames = []
if (target instanceof Controller) {
target.each(spring => {
global.getFrames(spring, preserve).forEach((value, i) => {
const frame = frames[i] || (frames[i] = {})
frame[spring.key!] = value
})
})
if (preserve) {
frameCache.set(target, frames)
}
}
}
return frames
}
global.countBounces = spring => {
const { to, from } = spring.animation
let prev = from
let count = 0
global.getFrames(spring, true).forEach(value => {
if (
value !== to &&
is.num(to) &&
is.num(prev) &&
value > to !== prev > to
) {
count += 1
}
prev = value
})
return count
}
global.advanceUntil = async test => {
let steps = 0
while (isRunning && !test()) {
// Observe animations scheduled for next frame.
const values: FrameValue[] = []
const observe = (value: unknown) => {
if (value instanceof FrameValue && !value.idle) {
getFluidObservers(value)?.forEach(observe)
addFluidObserver(value, frameObserver)
values.push(value)
}
}
Globals.assign({
willAdvance: observe,
})
await act(() => jest.advanceTimersByTimeAsync(1000 / 60))
global.mockRaf.step()
// Stop observing after the frame is processed.
for (const value of values) {
removeFluidObserver(value, frameObserver)
}
// Ensure pending effects are flushed.
await act(() => flushMicroTasks())
// Prevent infinite recursion.
if (++steps > 1e3) {
throw Error('Infinite loop detected')
}
}
}
global.advance = (n = 1) => {
return global.advanceUntil(() => --n < 0)
}
global.advanceByTime = ms => {
let fired = false
setTimeout(() => (fired = true), ms)
return global.advanceUntil(() => fired)
}
global.advanceUntilIdle = () => {
return global.advanceUntil(() => frameLoop.idle && raf.count() == 0)
}
// TODO: support "value" as an array or animatable string
global.advanceUntilValue = (spring, value) => {
const from = computeGoal(spring.get())
const goal = computeGoal(value)
const offset = global.getFrames(spring, true).length
return global.advanceUntil(() => {
const frames = global.getFrames(spring, true)
const value = frames.length - offset > 0 ? frames[frames.length - 1] : from
const stop =
is.num(goal) && is.num(value) && is.num(from)
? goal > from
? goal <= value
: goal >= value
: isEqual(value, goal)
return stop
})
}
global.setSkipAnimation = skip => {
Globals.assign({
skipAnimation: skip,
})
}
|