1
1
import { ref } from 'vue-demi'
2
2
import { useSetup } from '../../_tests'
3
3
import { useTransition , TransitionPresets } from '.'
4
+ import { promiseTimeout } from '../../shared/utils'
4
5
5
6
describe ( 'useTransition' , ( ) => {
6
- it ( 'transitions between values' , ( done ) => {
7
+ it ( 'transitions between values' , async ( ) => {
7
8
const vm = useSetup ( ( ) => {
8
9
const baseValue = ref ( 0 )
9
10
@@ -25,22 +26,19 @@ describe('useTransition', () => {
25
26
// changing the base value should start the transition
26
27
vm . baseValue = 1
27
28
28
- setTimeout ( ( ) => {
29
- // half way through the transition the base value should be 1,
30
- // and the transitioned value should be approximately 0.5
31
- expect ( vm . baseValue ) . toBe ( 1 )
32
- expect ( vm . transitionedValue > 0 && vm . transitionedValue < 1 ) . toBe ( true )
33
-
34
- setTimeout ( ( ) => {
35
- // once the transition is complete, both values should be 1
36
- expect ( vm . baseValue ) . toBe ( 1 )
37
- expect ( vm . transitionedValue ) . toBe ( 1 )
38
- done ( )
39
- } , 100 )
40
- } , 50 )
29
+ // half way through the transition the base value should be 1,
30
+ // and the transitioned value should be approximately 0.5
31
+ await promiseTimeout ( 50 )
32
+ expect ( vm . baseValue ) . toBe ( 1 )
33
+ expect ( vm . transitionedValue > 0 && vm . transitionedValue < 1 ) . toBe ( true )
34
+
35
+ // once the transition is complete, both values should be 1
36
+ await promiseTimeout ( 100 )
37
+ expect ( vm . baseValue ) . toBe ( 1 )
38
+ expect ( vm . transitionedValue ) . toBe ( 1 )
41
39
} )
42
40
43
- it ( 'exposes named presets' , ( done ) => {
41
+ it ( 'exposes named presets' , async ( ) => {
44
42
const vm = useSetup ( ( ) => {
45
43
const baseValue = ref ( 0 )
46
44
@@ -57,17 +55,14 @@ describe('useTransition', () => {
57
55
58
56
vm . baseValue = 1
59
57
60
- setTimeout ( ( ) => {
61
- expect ( vm . transitionedValue > 0 && vm . transitionedValue < 1 ) . toBe ( true )
58
+ await promiseTimeout ( 50 )
59
+ expect ( vm . transitionedValue > 0 && vm . transitionedValue < 1 ) . toBe ( true )
62
60
63
- setTimeout ( ( ) => {
64
- expect ( vm . transitionedValue ) . toBe ( 1 )
65
- done ( )
66
- } , 100 )
67
- } , 50 )
61
+ await promiseTimeout ( 100 )
62
+ expect ( vm . transitionedValue ) . toBe ( 1 )
68
63
} )
69
64
70
- it ( 'supports custom function transitions' , ( done ) => {
65
+ it ( 'supports custom function transitions' , async ( ) => {
71
66
const vm = useSetup ( ( ) => {
72
67
const baseValue = ref ( 0 )
73
68
@@ -84,13 +79,44 @@ describe('useTransition', () => {
84
79
85
80
vm . baseValue = 1
86
81
87
- setTimeout ( ( ) => {
88
- expect ( vm . transitionedValue > 0 && vm . transitionedValue < 1 ) . toBe ( true )
82
+ await promiseTimeout ( 50 )
83
+ expect ( vm . transitionedValue > 0 && vm . transitionedValue < 1 ) . toBe ( true )
84
+
85
+ await promiseTimeout ( 100 )
86
+ expect ( vm . transitionedValue ) . toBe ( 1 )
87
+ } )
88
+
89
+ it ( 'support dynamic transition durations' , async ( ) => {
90
+ const vm = useSetup ( ( ) => {
91
+ const baseValue = ref ( 0 )
92
+ const duration = ref ( 100 )
93
+
94
+ const transitionedValue = useTransition ( baseValue , {
95
+ duration,
96
+ transition : n => n ,
97
+ } )
98
+
99
+ return {
100
+ baseValue,
101
+ duration,
102
+ transitionedValue,
103
+ }
104
+ } )
105
+
106
+ // first transition should take 100ms
107
+ vm . baseValue = 1
108
+
109
+ await promiseTimeout ( 150 )
110
+ expect ( vm . transitionedValue ) . toBe ( 1 )
111
+
112
+ // second transition should take 200ms
113
+ vm . duration = 200
114
+ vm . baseValue = 2
115
+
116
+ await promiseTimeout ( 150 )
117
+ expect ( vm . transitionedValue < 2 ) . toBe ( true )
89
118
90
- setTimeout ( ( ) => {
91
- expect ( vm . transitionedValue ) . toBe ( 1 )
92
- done ( )
93
- } , 100 )
94
- } , 50 )
119
+ await promiseTimeout ( 100 )
120
+ expect ( vm . transitionedValue ) . toBe ( 2 )
95
121
} )
96
122
} )
0 commit comments