@@ -2,18 +2,22 @@ import useConditionWatcher from 'vue-condition-watcher'
2
2
import { isRef , isReactive , isReadonly , createApp , nextTick , defineComponent } from 'vue-demi'
3
3
import type { VueWrapper } from '@vue/test-utils'
4
4
import { mount } from '@vue/test-utils'
5
- import { describe , expect , test , vi } from 'vitest'
5
+ import { describe , expect , test , vi , beforeEach } from 'vitest'
6
6
7
7
describe ( 'Basic test of vue-condition-watcher' , ( ) => {
8
- it ( `Check return value type` , ( ) => {
9
- const config = {
8
+ let basicTestConfig = { }
9
+ beforeEach ( ( ) => {
10
+ basicTestConfig = {
10
11
fetcher : ( params ) => new Promise ( ( resolve ) => resolve ( params ) ) ,
11
12
conditions : {
12
13
gender : [ 'male' ] ,
13
14
results : 9 ,
14
15
} ,
15
16
}
16
- const { conditions, data, error, isLoading, execute } = useConditionWatcher ( config )
17
+ } )
18
+
19
+ it ( `Check return value type` , ( ) => {
20
+ const { conditions, data, error, isLoading, execute } = useConditionWatcher ( basicTestConfig )
17
21
18
22
expect ( isReactive ( conditions ) ) . toBeTruthy ( )
19
23
expect ( isRef ( data ) ) . toBeTruthy ( )
@@ -24,30 +28,15 @@ describe('Basic test of vue-condition-watcher', () => {
24
28
} )
25
29
26
30
it ( `Check data, error, isLoading is readonly` , ( ) => {
27
- const config = {
28
- fetcher : ( params ) => new Promise ( ( resolve ) => resolve ( params ) ) ,
29
- conditions : {
30
- gender : [ 'male' ] ,
31
- results : 9 ,
32
- } ,
33
- }
34
-
35
- const { data, error, isLoading } = useConditionWatcher ( config )
31
+ const { data, error, isLoading } = useConditionWatcher ( basicTestConfig )
36
32
37
33
expect ( isReadonly ( data ) ) . toBeTruthy ( )
38
34
expect ( isReadonly ( error ) ) . toBeTruthy ( )
39
35
expect ( isReadonly ( isLoading ) ) . toBeTruthy ( )
40
36
} )
41
37
42
38
it ( `Condition should be change` , ( ) => {
43
- const config = {
44
- fetcher : ( params ) => new Promise ( ( resolve ) => resolve ( params ) ) ,
45
- conditions : {
46
- gender : [ 'male' ] ,
47
- results : 9 ,
48
- } ,
49
- }
50
- const { conditions } = useConditionWatcher ( config )
39
+ const { conditions } = useConditionWatcher ( basicTestConfig )
51
40
52
41
expect ( conditions ) . toMatchObject ( {
53
42
gender : [ 'male' ] ,
@@ -61,6 +50,39 @@ describe('Basic test of vue-condition-watcher', () => {
61
50
results : 10 ,
62
51
} )
63
52
} )
53
+
54
+ it ( 'Reset conditions to initial value' , ( ) => {
55
+ const { conditions, resetConditions } = useConditionWatcher ( basicTestConfig )
56
+
57
+ conditions . results = 10
58
+ conditions . gender = [ 'male' , 'female' ]
59
+
60
+ resetConditions ( )
61
+
62
+ expect ( conditions ) . toMatchObject ( {
63
+ gender : [ 'male' ] ,
64
+ results : 9 ,
65
+ } )
66
+ } )
67
+
68
+ it ( 'Reset conditions to custom value and only assign if property exists.' , ( ) => {
69
+ const { conditions, resetConditions } = useConditionWatcher ( basicTestConfig )
70
+
71
+ conditions . results = 10
72
+ conditions . gender = [ 'male' , 'female' ]
73
+
74
+ resetConditions ( {
75
+ gender : [ 'female' ] ,
76
+ results : 19 ,
77
+ type : '2' ,
78
+ useless : '12312321' ,
79
+ } )
80
+
81
+ expect ( conditions ) . toMatchObject ( {
82
+ gender : [ 'female' ] ,
83
+ results : 19 ,
84
+ } )
85
+ } )
64
86
} )
65
87
66
88
// const tick = async (times: number) => {
0 commit comments