File size: 17,259 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
import { Controller } from './Controller'
import { flushMicroTasks } from 'flush-microtasks'
import { getFinishedResult } from './AnimationResult'
const frameLength = 1000 / 60
describe('Controller', () => {
it('can animate a number', async () => {
const ctrl = new Controller({ x: 0 })
ctrl.start({ x: 100 })
await global.advanceUntilIdle()
const frames = global.getFrames(ctrl)
expect(frames).toMatchSnapshot()
// The first frame should *not* be the from value.
expect(frames[0]).not.toEqual({ x: 0 })
// The last frame should be the goal value.
expect(frames.slice(-1)[0]).toEqual({ x: 100 })
})
it('can animate an array of numbers', async () => {
const config = { precision: 0.005 }
const ctrl = new Controller<{ x: [number, number] }>({ x: [1, 2], config })
ctrl.start({ x: [5, 10] })
await global.advanceUntilIdle()
const frames = global.getFrames(ctrl)
expect(frames).toMatchSnapshot()
// The last frame should be the goal value.
expect(frames.slice(-1)[0]).toEqual({ x: [5, 10] })
// The 2nd value is always ~2x the 1st value (within the defined precision).
const factors = frames.map(frame => frame.x[1] / frame.x[0])
expect(
factors.every(factor => Math.abs(2 - factor) < config.precision)
).toBeTruthy()
})
describe('when the "to" prop is an async function', () => {
it('respects the "cancel" prop', async () => {
const ctrl = new Controller({ from: { x: 0 } })
const promise = ctrl.start({
to: async next => {
while (true) {
await next({ x: 1, reset: true })
}
},
})
const { x } = ctrl.springs
await global.advanceUntilValue(x, 0.5)
ctrl.start({ cancel: true })
await flushMicroTasks()
expect(ctrl.idle).toBeTruthy()
expect((await promise).cancelled).toBeTruthy()
})
it('respects the "stop" method', async () => {
const ctrl = new Controller({ from: { x: 0 } })
const promise = ctrl.start({
to: async next => {
while (true) {
await next({ x: 1, reset: true })
}
},
})
const { x } = ctrl.springs
await global.advanceUntilValue(x, 0.5)
ctrl.stop()
expect(ctrl.idle).toBeTruthy()
expect((await promise).finished).toBeFalsy()
})
it('respects the "pause" prop', async () => {
const ctrl = new Controller({ from: { x: 0 } })
ctrl.start({ pause: true })
let n = 0
ctrl.start({
to: async animate => {
while (true) {
n += 1
await animate({ x: 1, reset: true })
}
},
})
await flushMicroTasks()
expect(n).toBe(0)
ctrl.start({ pause: false })
await flushMicroTasks()
expect(n).toBe(1)
})
describe('when the "to" prop is changed', () => {
it('stops the old "to" prop', async () => {
const ctrl = new Controller({ from: { x: 0 } })
let n = 0
const promise = ctrl.start({
to: async next => {
while (++n < 5) {
await next({ x: 1, reset: true })
}
},
})
await global.advance()
expect(n).toBe(1)
ctrl.start({
to: () => {},
})
await global.advanceUntilIdle()
expect(n).toBe(1)
expect(await promise).toMatchObject({
finished: false,
})
})
})
// This function is the "to" prop's 1st argument.
describe('the "animate" function', () => {
it('inherits any default props', async () => {
const ctrl = new Controller({ from: { x: 0 } })
const onStart = jest.fn()
ctrl.start({
onStart,
to: async animate => {
expect(onStart).toBeCalledTimes(0)
await animate({ x: 1 })
expect(onStart).toBeCalledTimes(1)
await animate({ x: 0 })
},
})
await global.advanceUntilIdle()
expect(onStart).toBeCalledTimes(2)
})
it('can start its own async animation', async () => {
const ctrl = new Controller({ from: { x: 0 } })
// Call this from inside the nested "to" prop.
const nestedFn = jest.fn()
// Call this after the nested "to" prop is done.
const afterFn = jest.fn()
ctrl.start({
to: async animate => {
await animate({
to: async animate => {
nestedFn()
await animate({ x: 1 })
},
})
afterFn()
},
})
await global.advanceUntilIdle()
await flushMicroTasks()
expect(nestedFn).toBeCalledTimes(1)
expect(afterFn).toBeCalledTimes(1)
})
})
describe('nested async animation', () => {
it('stops the parent on bail', async () => {
const ctrl = new Controller({ from: { x: 0 } })
const { x } = ctrl.springs
const afterFn = jest.fn()
ctrl.start({
to: async animate => {
await animate({
to: async animate => {
await animate({ x: 1 })
},
})
afterFn()
},
})
await global.advanceUntilValue(x, 0.5)
ctrl.start({ cancel: true })
await flushMicroTasks()
expect(ctrl.idle).toBeTruthy()
expect(afterFn).not.toHaveBeenCalled()
})
})
describe('while paused', () => {
it('stays paused when its values are force-finished', () => {
const ctrl = new Controller<{ t: number }>({ t: 0 })
const { t } = ctrl.springs
const onRest = jest.fn()
ctrl.start({
to: next => next({ t: 1 }),
onRest,
})
global.mockRaf.step()
ctrl.pause()
t.finish()
global.mockRaf.step()
expect(ctrl['_state'].paused).toBeTruthy()
expect(onRest).not.toBeCalled()
})
})
it('acts strangely without the "from" prop', async () => {
const ctrl = new Controller<{ x: number }>()
const { springs } = ctrl
const promise = ctrl.start({
to: async update => {
// The spring does not exist yet!
expect(springs.x).toBeUndefined()
// Any values passed here are treated as "from" values,
// because no "from" prop was ever given.
const p1 = update({ x: 1 })
// Now the spring exists!
expect(springs.x).toBeDefined()
// But the spring is idle!
expect(springs.x.idle).toBeTruthy()
// This call *will* start an animation!
const p2 = update({ x: 2 })
expect(springs.x.idle).toBeFalsy()
await Promise.all([p1, p2])
},
})
await Promise.all([global.advanceUntilIdle(), promise])
expect(ctrl.idle).toBeTruthy()
// Since we call `update` twice, frames are generated!
expect(global.getFrames(ctrl)).toMatchSnapshot()
})
describe('when skipAnimations is true', () => {
it('should not run at all', async () => {
const ctrl = new Controller({ from: { x: 0 } })
let n = 0
global.setSkipAnimation(true)
ctrl.start({
to: async next => {
while (true) {
n += 1
await next({ x: 1, reset: true })
}
},
})
await flushMicroTasks()
expect(n).toBe(0)
})
it('should stop running and push the animation to the finished state when called mid animation', async () => {
const ctrl = new Controller({ from: { x: 0 } })
let n = 0
ctrl.start({
to: async next => {
while (n < 5) {
n++
await next({ x: 10, reset: true })
}
},
})
await global.advance()
expect(n).toBe(1)
global.setSkipAnimation(true)
await global.advanceUntilIdle()
const { x } = ctrl.springs
expect(n).toBe(2)
expect(x.get()).toEqual(10)
})
})
})
describe('when the "onStart" prop is defined', () => {
it('is called once per "start" call maximum', async () => {
const ctrl = new Controller({ x: 0, y: 0 })
const onStart = jest.fn()
ctrl.start({
x: 1,
y: 1,
onStart,
})
await global.advanceUntilIdle()
expect(onStart).toBeCalledTimes(1)
})
it('can be different per key', async () => {
const ctrl = new Controller({ x: 0, y: 0 })
const onStart1 = jest.fn()
ctrl.start({ x: 1, onStart: onStart1 })
const onStart2 = jest.fn()
ctrl.start({ y: 1, onStart: onStart2 })
await global.advanceUntilIdle()
expect(onStart1).toBeCalledTimes(1)
expect(onStart2).toBeCalledTimes(1)
})
})
describe('the "loop" prop', () => {
it('can be combined with the "reverse" prop', async () => {
const ctrl = new Controller({
t: 1,
from: { t: 0 },
config: { duration: frameLength * 3 },
})
const { t } = ctrl.springs
expect(t.get()).toBe(0)
await global.advanceUntilIdle()
expect(t.get()).toBe(1)
ctrl.start({
loop: { reverse: true },
})
await global.advanceUntilValue(t, 0)
await global.advanceUntilValue(t, 1)
expect(global.getFrames(t)).toMatchSnapshot()
})
describe('used with multiple values', () => {
it('loops all values at the same time', async () => {
const ctrl = new Controller()
ctrl.start({
to: { x: 1, y: 1 },
from: { x: 0, y: 0 },
config: key => ({ frequency: key == 'x' ? 0.3 : 1 }),
loop: true,
})
const { x, y } = ctrl.springs
for (let i = 0; i < 2; i++) {
await global.advanceUntilValue(y, 1)
// Both values should equal their "from" value at the same time.
expect(x.get()).toBe(x.animation.from)
expect(y.get()).toBe(y.animation.from)
}
})
})
describe('used when "to" is', () => {
describe('an async function', () => {
it('calls the "to" function repeatedly', async () => {
const ctrl = new Controller({ t: 0 })
const { t } = ctrl.springs
let loop = true
let times = 2
// Note: This example is silly, since you could use a for-loop
// to more easily achieve the same result, but it tests the ability
// to halt a looping script via the "loop" function prop.
ctrl.start({
loop: () => loop,
to: async next => {
await next({ t: 1 })
await next({ t: 0 })
if (times--) return
loop = false
},
})
await global.advanceUntilValue(t, 1)
expect(t.idle).toBeFalsy()
for (let i = 0; i < 2; i++) {
await global.advanceUntilValue(t, 0)
expect(t.idle).toBeFalsy()
await global.advanceUntilValue(t, 1)
expect(t.idle).toBeFalsy()
}
await global.advanceUntilValue(t, 0)
expect(t.idle).toBeTruthy()
})
})
describe('an array', () => {
it('repeats the chain of updates', async () => {
const ctrl = new Controller({ t: 0 })
const { t } = ctrl.springs
let loop = true
const promise = ctrl.start({
loop: () => {
return loop
},
from: { t: 0 },
to: [{ t: 1 }, { t: 2 }],
config: { duration: 3000 / 60 },
})
for (let i = 0; i < 3; i++) {
await global.advanceUntilValue(t, 2)
expect(t.idle).toBeFalsy()
// Run the first frame of the next loop.
global.mockRaf.step()
}
loop = false
await global.advanceUntilValue(t, 2)
expect(t.idle).toBeTruthy()
expect(await promise).toMatchObject({
value: { t: 2 },
finished: true,
})
})
})
})
describe('used on a noop update', () => {
it('does not loop', async () => {
const ctrl = new Controller({ t: 0 })
const loop = jest.fn(() => true)
ctrl.start({ t: 0, loop })
await global.advanceUntilIdle()
expect(loop).toBeCalledTimes(0)
})
})
describe('when "finish" is called while paused', () => {
async function getPausedLoop() {
const ctrl = new Controller<{ t: number }>({
from: { t: 0 }, // FIXME: replace this line with `t: 0,` for a stack overflow
loop: {
async to(start) {
await start({
t: 1,
reset: true,
})
},
},
})
// Ensure `loop.to` has been called.
await flushMicroTasks()
// Apply the first frame.
global.mockRaf.step()
ctrl.pause()
return ctrl
}
it('finishes immediately', async () => {
const ctrl = await getPausedLoop()
const { t } = ctrl.springs
expect(t.get()).toBeLessThan(1)
t.finish()
expect(t.get()).toBe(1)
})
it('does not loop until resumed', async () => {
const ctrl = await getPausedLoop()
const { t } = ctrl.springs
t.finish()
expect(t.idle).toBeTruthy()
expect(t.get()).toBe(1)
// HACK: The internal promise is undefined once resolved.
const expectResolved = (isResolved: boolean) =>
!ctrl['_state'].promise == isResolved
// Resume the paused loop.
ctrl.resume()
// Its promise is not resolved yet..
expectResolved(false)
expect(t.idle).toBeTruthy()
expect(t.get()).toBe(1)
// ..but in the next microtask, it will be..
await flushMicroTasks()
expectResolved(true)
// ..which means the loop restarts!
expect(t.idle).toBeFalsy()
expect(t.get()).toBe(0)
})
})
})
describe('the "stop" method', () => {
it('prevents any updates with pending delays', async () => {
const ctrl = new Controller<{ t: number }>({ t: 0 })
const { t } = ctrl.springs
ctrl.start({ t: 1, delay: 100 })
ctrl.stop()
await global.advanceUntilIdle()
expect(ctrl['_state'].timeouts.size).toBe(0)
expect(t['_state'].timeouts.size).toBe(0)
})
it('stops the active runAsync call', async () => {
const ctrl = new Controller<{ t: number }>({ t: 0 })
ctrl.start({
to: async animate => {
await animate({ t: 1 })
},
})
ctrl.stop()
await global.advanceUntilIdle()
expect(ctrl['_state'].asyncTo).toBeUndefined()
})
})
describe('events', () => {
test('events recieve an AnimationResult and the Controller as the first two args', async () => {
const ctrl = new Controller<{ t: number }>({ t: 0 })
const onRest = jest.fn()
const onStart = jest.fn()
const onChange = jest.fn()
ctrl.start({
to: next => next({ t: 1 }),
onRest,
onStart,
onChange,
})
global.mockRaf.step()
expect(onStart).toBeCalledWith(
getFinishedResult({ t: 0.022634843307857987 }, false),
ctrl,
undefined
)
expect(onChange).toBeCalledWith(
getFinishedResult(ctrl.get(), false),
ctrl,
undefined
)
await global.advanceUntilIdle()
expect(onRest).toBeCalledWith(
getFinishedResult(ctrl.get(), true),
ctrl,
undefined
)
})
test('events recieve also recieve the item if set as the third', async () => {
const ctrl = new Controller<{ t: number }>({ t: 0 })
const item = { msg: 'hello world', key: 1 }
ctrl.item = item
const onRest = jest.fn()
const onStart = jest.fn()
const onChange = jest.fn()
ctrl.start({
to: next => next({ t: 1 }),
onRest,
onStart,
onChange,
})
global.mockRaf.step()
expect(onStart).toBeCalledWith(
getFinishedResult({ t: 0.022634843307857987 }, false),
ctrl,
item
)
expect(onChange).toBeCalledWith(
getFinishedResult(ctrl.get(), false),
ctrl,
item
)
await global.advanceUntilIdle()
expect(onRest).toBeCalledWith(
getFinishedResult(ctrl.get(), true),
ctrl,
item
)
})
test('onStart & onRest are flushed even if the `immediate` prop is true', async () => {
const onRest = jest.fn()
const onStart = jest.fn()
const ctrl = new Controller<{ t: number }>({
t: 0,
onStart,
onRest,
})
ctrl.start({
t: 1,
immediate: true,
})
await global.advanceUntilIdle()
expect(ctrl.get().t).toBe(1)
expect(onStart).toHaveBeenCalled()
expect(onRest).toHaveBeenCalled()
})
})
})
|