MCPcopy
hub / github.com/pmndrs/react-spring / describeTarget

Function describeTarget

packages/core/src/SpringValue.test.ts:1059–1191  ·  view source on GitHub ↗
(name: string, create: (from: number) => OpaqueTarget)

Source from the content-addressed store, hash-verified

1057}
1058
1059function describeTarget(name: string, create: (from: number) => OpaqueTarget) {
1060 describe('when our target is ' + name, () => {
1061 let target: OpaqueTarget
1062 let spring: SpringValue
1063 let observer: FluidObserver
1064 beforeEach(() => {
1065 target = create(1)
1066 spring = new SpringValue(0)
1067 // The target is not attached until the spring is observed.
1068 addFluidObserver(spring, (observer = () => {}))
1069 })
1070
1071 it('animates toward the current value', async () => {
1072 spring.start({ to: target.node })
1073 expect(spring.priority).toBeGreaterThan(target.node.priority)
1074 expect(spring.animation).toMatchObject({
1075 to: target.node,
1076 toValues: null,
1077 })
1078
1079 await global.advanceUntilIdle()
1080 expect(spring.get()).toBe(target.node.get())
1081 })
1082
1083 it('preserves its "onRest" prop between animations', async () => {
1084 const onRest = vi.fn()
1085 spring.start({ to: target.node, onRest })
1086 await global.advanceUntilIdle()
1087 expect(onRest).toBeCalledTimes(1)
1088
1089 // When the fluid target moves, the spring re-animates without a
1090 // fresh start() call. The "onRest" handler should still fire when
1091 // the new animation settles.
1092 target.start(2)
1093 await global.advanceUntilIdle()
1094 expect(onRest).toBeCalledTimes(2)
1095 })
1096
1097 it('can change its target while animating', async () => {
1098 spring.start({ to: target.node })
1099 await global.advanceUntilValue(spring, target.node.get() / 2)
1100
1101 spring.start(0)
1102 expect(spring.priority).toBe(0)
1103 expect(spring.animation).toMatchObject({
1104 to: 0,
1105 toValues: [0],
1106 })
1107
1108 await global.advanceUntilIdle()
1109 expect(spring.get()).toBe(0)
1110 })
1111
1112 describe('when target is done animating', () => {
1113 it('keeps animating until the target is reached', async () => {
1114 spring.start({ to: target.node })
1115 target.start(1.1)
1116

Callers 1

Calls 7

addFluidObserverFunction · 0.90
removeFluidObserverFunction · 0.90
hasFluidObserverFunction · 0.85
startMethod · 0.65
getMethod · 0.65
setMethod · 0.65
resetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…