|
| 1 | +import { createEffect, frame, motionValue } from "motion-dom" |
| 2 | +import { animate } from ".." |
| 3 | + |
| 4 | +async function nextFrame() { |
| 5 | + return new Promise<void>((resolve) => { |
| 6 | + frame.postRender(() => resolve()) |
| 7 | + }) |
| 8 | +} |
| 9 | + |
| 10 | +interface Subject { |
| 11 | + isSubject: true |
| 12 | + values: Record<string, number | string> |
| 13 | +} |
| 14 | + |
| 15 | +const createSubject = (values: Subject["values"] = {}): Subject => ({ |
| 16 | + isSubject: true, |
| 17 | + values, |
| 18 | +}) |
| 19 | + |
| 20 | +const subjectEffect = createEffect<Subject>( |
| 21 | + (subject, state, key, value) => |
| 22 | + state.set( |
| 23 | + key, |
| 24 | + value, |
| 25 | + () => { |
| 26 | + subject.values[key] = state.latest[key] |
| 27 | + }, |
| 28 | + undefined, |
| 29 | + false |
| 30 | + ), |
| 31 | + { |
| 32 | + test: (subject): subject is Subject => |
| 33 | + Boolean((subject as Subject)?.isSubject), |
| 34 | + read: (subject, key) => subject.values[key], |
| 35 | + } |
| 36 | +) |
| 37 | + |
| 38 | +describe("animate.addEffect", () => { |
| 39 | + beforeEach(() => animate.addEffect(subjectEffect)) |
| 40 | + afterEach(() => animate.removeEffect(subjectEffect)) |
| 41 | + |
| 42 | + test("animates subjects claimed by a registered effect", async () => { |
| 43 | + const subject = createSubject({ x: 0 }) |
| 44 | + |
| 45 | + await animate(subject, { x: 100 }, { duration: 0.01 }) |
| 46 | + await nextFrame() |
| 47 | + |
| 48 | + expect(subject.values.x).toBe(100) |
| 49 | + }) |
| 50 | + |
| 51 | + test("falls back to plain objects when no effect claims the subject", async () => { |
| 52 | + const subject = { x: 0 } |
| 53 | + |
| 54 | + await animate(subject, { x: 100 }, { duration: 0.01 }) |
| 55 | + |
| 56 | + expect(subject.x).toBe(100) |
| 57 | + }) |
| 58 | + |
| 59 | + test("animates DOM elements even when an effect claims everything", async () => { |
| 60 | + const greedyEffect = createEffect<any>(() => () => {}, { |
| 61 | + test: (_subject: unknown): _subject is any => true, |
| 62 | + read: () => 0, |
| 63 | + }) |
| 64 | + animate.addEffect(greedyEffect) |
| 65 | + |
| 66 | + const element = document.createElement("div") |
| 67 | + await animate(element, { opacity: 0.5 }, { duration: 0.01 }) |
| 68 | + await nextFrame() |
| 69 | + |
| 70 | + animate.removeEffect(greedyEffect) |
| 71 | + |
| 72 | + expect(element.style.opacity).toBe("0.5") |
| 73 | + }) |
| 74 | + |
| 75 | + test("animates arrays of effect subjects with stagger", async () => { |
| 76 | + const a = createSubject({ x: 0 }) |
| 77 | + const b = createSubject({ x: 0 }) |
| 78 | + |
| 79 | + await animate( |
| 80 | + [a, b], |
| 81 | + { x: 100 }, |
| 82 | + { |
| 83 | + duration: 0.01, |
| 84 | + delay: (i: number) => i * 0.01, |
| 85 | + } |
| 86 | + ) |
| 87 | + await nextFrame() |
| 88 | + |
| 89 | + expect(a.values.x).toBe(100) |
| 90 | + expect(b.values.x).toBe(100) |
| 91 | + }) |
| 92 | + |
| 93 | + test("animates mixed DOM and effect subjects in a sequence", async () => { |
| 94 | + const element = document.createElement("div") |
| 95 | + const subject = createSubject({ x: 0 }) |
| 96 | + |
| 97 | + await animate( |
| 98 | + [ |
| 99 | + [element, { opacity: 0.5 }, { duration: 0.01 }], |
| 100 | + [subject, { x: 100 }, { duration: 0.01 }], |
| 101 | + ], |
| 102 | + { duration: 0.01 } |
| 103 | + ) |
| 104 | + await nextFrame() |
| 105 | + |
| 106 | + expect(element.style.opacity).toBe("0.5") |
| 107 | + expect(subject.values.x).toBe(100) |
| 108 | + }) |
| 109 | + |
| 110 | + test("reuses motion values bound via the effect", async () => { |
| 111 | + const subject = createSubject() |
| 112 | + const x = motionValue(0) |
| 113 | + subjectEffect(subject, { x }) |
| 114 | + |
| 115 | + await animate(subject, { x: 100 }, { duration: 0.01 }) |
| 116 | + |
| 117 | + expect(x.get()).toBe(100) |
| 118 | + }) |
| 119 | +}) |
0 commit comments