-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(useTransition): cleaning up (#385)
- Loading branch information
1 parent
d57c3cc
commit f4a302b
Showing
6 changed files
with
297 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { promiseTimeout } from '@vueuse/shared' | ||
import { ref } from 'vue-demi' | ||
import { useTransition } from '.' | ||
|
||
const expectBetween = (val: number, floor: number, ceiling: number) => { | ||
expect(val).toBeGreaterThan(floor) | ||
expect(val).toBeLessThan(ceiling) | ||
} | ||
|
||
describe('useTransition', () => { | ||
it('transitions between numbers', async() => { | ||
const source = ref(0) | ||
const transition = useTransition(source, { duration: 100 }) | ||
|
||
expect(transition.value).toBe(0) | ||
|
||
source.value = 1 | ||
|
||
await promiseTimeout(50) | ||
expectBetween(transition.value, 0, 1) | ||
|
||
await promiseTimeout(100) | ||
expect(transition.value).toBe(1) | ||
}) | ||
|
||
it('transitions between vectors', async() => { | ||
const source = ref([0, 0]) | ||
const transition = useTransition(source, { duration: 100 }) | ||
|
||
expect(transition.value).toEqual([0, 0]) | ||
|
||
source.value = [1, 1] | ||
|
||
await promiseTimeout(50) | ||
expectBetween(transition.value[0], 0, 1) | ||
expectBetween(transition.value[1], 0, 1) | ||
|
||
await promiseTimeout(100) | ||
expect(transition.value[0]).toBe(1) | ||
expect(transition.value[1]).toBe(1) | ||
}) | ||
|
||
it('supports cubic bezier curves', async() => { | ||
const source = ref(0) | ||
|
||
// https://cubic-bezier.com/#0,2,0,1 | ||
const easeOutBack = useTransition(source, { | ||
duration: 100, | ||
transition: [0, 2, 0, 1], | ||
}) | ||
|
||
// https://cubic-bezier.com/#1,0,1,-1 | ||
const easeInBack = useTransition(source, { | ||
duration: 100, | ||
transition: [1, 0, 1, -1], | ||
}) | ||
|
||
source.value = 1 | ||
|
||
await promiseTimeout(50) | ||
expectBetween(easeOutBack.value, 1, 2) | ||
expectBetween(easeInBack.value, -1, 0) | ||
|
||
await promiseTimeout(100) | ||
expect(easeOutBack.value).toBe(1) | ||
expect(easeInBack.value).toBe(1) | ||
}) | ||
|
||
it('supports custom easing functions', async() => { | ||
const source = ref(0) | ||
const linear = jest.fn(n => n) | ||
const transition = useTransition(source, { | ||
duration: 100, | ||
transition: linear, | ||
}) | ||
|
||
expect(linear).not.toHaveBeenCalled() | ||
|
||
source.value = 1 | ||
|
||
await promiseTimeout(50) | ||
expect(linear).toHaveBeenCalled() | ||
expectBetween(transition.value, 0, 1) | ||
|
||
await promiseTimeout(100) | ||
expect(transition.value).toBe(1) | ||
}) | ||
|
||
it('supports dynamic transitions', async() => { | ||
const source = ref(0) | ||
const first = jest.fn(n => n) | ||
const second = jest.fn(n => n) | ||
const easingFn = ref(first) | ||
|
||
useTransition(source, { | ||
duration: 100, | ||
transition: easingFn, | ||
}) | ||
|
||
expect(first).not.toHaveBeenCalled() | ||
expect(second).not.toHaveBeenCalled() | ||
|
||
source.value = 1 | ||
|
||
await promiseTimeout(50) | ||
expect(first).toHaveBeenCalled() | ||
expect(second).not.toHaveBeenCalled() | ||
|
||
first.mockReset() | ||
second.mockReset() | ||
|
||
easingFn.value = second | ||
source.value = 2 | ||
|
||
await promiseTimeout(100) | ||
expect(first).not.toHaveBeenCalled() | ||
expect(second).toHaveBeenCalled() | ||
}) | ||
|
||
it('supports dynamic durations', async() => { | ||
const source = ref(0) | ||
const duration = ref(100) | ||
const transition = useTransition(source, { duration }) | ||
|
||
source.value = 1 | ||
|
||
await promiseTimeout(50) | ||
expectBetween(transition.value, 0, 1) | ||
|
||
await promiseTimeout(100) | ||
expect(transition.value).toBe(1) | ||
|
||
duration.value = 200 | ||
source.value = 2 | ||
|
||
await promiseTimeout(150) | ||
expectBetween(transition.value, 1, 2) | ||
|
||
await promiseTimeout(100) | ||
expect(transition.value).toBe(2) | ||
}) | ||
|
||
it('fires onStarted and onFinished callbacks', async() => { | ||
const source = ref(0) | ||
const onStarted = jest.fn() | ||
const onFinished = jest.fn() | ||
|
||
useTransition(source, { | ||
duration: 100, | ||
onStarted, | ||
onFinished, | ||
}) | ||
|
||
expect(onStarted).not.toHaveBeenCalled() | ||
expect(onFinished).not.toHaveBeenCalled() | ||
|
||
source.value = 1 | ||
|
||
await promiseTimeout(50) | ||
expect(onStarted).toHaveBeenCalled() | ||
expect(onFinished).not.toHaveBeenCalled() | ||
|
||
onStarted.mockReset() | ||
onFinished.mockReset() | ||
|
||
await promiseTimeout(100) | ||
expect(onStarted).not.toHaveBeenCalled() | ||
expect(onFinished).toHaveBeenCalled() | ||
}) | ||
}) |
Oops, something went wrong.