Skip to content

Commit

Permalink
Improving test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry committed Dec 15, 2022
1 parent 44744e0 commit 624003c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Framer Motion adheres to [Semantic Versioning](http://semver.org/).

Undocumented APIs should be considered internal and may change without warning.

## [7.10.0] 2022-12-15

### Added

- `.on()` event method to `MotionValue`.
- `"animationStart"`, `"animationComplete"`, and `"animationCancel"` events for `MotionValue`.

## [7.9.1] 2022-12-14

### Fixed
Expand Down
73 changes: 73 additions & 0 deletions packages/framer-motion/src/value/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { motionValue } from "../"
import { animate } from "../../animation/animate"

describe("motionValue", () => {
test("change event fires when value changes", () => {
const value = motionValue(0)
const callback = jest.fn()

value.on("change", callback)

expect(callback).not.toBeCalled()
value.set(1)
expect(callback).toBeCalledTimes(1)
value.set(1)
expect(callback).toBeCalledTimes(1)
})

test("renderRequest event fires", () => {
const value = motionValue(0)
const callback = jest.fn()

value.on("renderRequest", callback)

expect(callback).not.toBeCalled()
value.set(1)
expect(callback).toBeCalledTimes(1)
})

test("animationStart event fires", () => {
const value = motionValue(0)
const callback = jest.fn()

value.on("animationStart", callback)

expect(callback).not.toBeCalled()

animate(value, 2)

expect(callback).toBeCalledTimes(1)
})

test("animationCancel event fires", () => {
const value = motionValue(0)
const callback = jest.fn()

value.on("animationCancel", callback)

expect(callback).not.toBeCalled()

animate(value, 1)
animate(value, 2)

expect(callback).toBeCalledTimes(1)
})

test("animationComplete event fires", async () => {
const value = motionValue(0)
const callback = jest.fn()

value.on("animationComplete", callback)

expect(callback).not.toBeCalled()

animate(value, 1, { duration: 0.01 })

return new Promise<void>((resolve) => {
setTimeout(() => {
expect(callback).toBeCalledTimes(1)
resolve()
}, 100)
})
})
})
4 changes: 2 additions & 2 deletions packages/framer-motion/src/value/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export interface MotionValueEventCallbacks<V> {
animationStart: () => void
animationComplete: () => void
animationCancel: () => void
change: (latest: V) => void
change: (latestValue: V) => void
renderRequest: () => void
velocityChange: (latest: number) => void
velocityChange: (latestVelocity: number) => void
}

const isFloat = (value: any): value is string => {
Expand Down

0 comments on commit 624003c

Please sign in to comment.