@@ -2,18 +2,22 @@ import useConditionWatcher from 'vue-condition-watcher'
22import { isRef , isReactive , isReadonly , createApp , nextTick , defineComponent } from 'vue-demi'
33import type { VueWrapper } from '@vue/test-utils'
44import { mount } from '@vue/test-utils'
5- import { describe , expect , test , vi } from 'vitest'
5+ import { describe , expect , test , vi , beforeEach } from 'vitest'
66
77describe ( 'Basic test of vue-condition-watcher' , ( ) => {
8- it ( `Check return value type` , ( ) => {
9- const config = {
8+ let basicTestConfig = { }
9+ beforeEach ( ( ) => {
10+ basicTestConfig = {
1011 fetcher : ( params ) => new Promise ( ( resolve ) => resolve ( params ) ) ,
1112 conditions : {
1213 gender : [ 'male' ] ,
1314 results : 9 ,
1415 } ,
1516 }
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 )
1721
1822 expect ( isReactive ( conditions ) ) . toBeTruthy ( )
1923 expect ( isRef ( data ) ) . toBeTruthy ( )
@@ -24,30 +28,15 @@ describe('Basic test of vue-condition-watcher', () => {
2428 } )
2529
2630 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 )
3632
3733 expect ( isReadonly ( data ) ) . toBeTruthy ( )
3834 expect ( isReadonly ( error ) ) . toBeTruthy ( )
3935 expect ( isReadonly ( isLoading ) ) . toBeTruthy ( )
4036 } )
4137
4238 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 )
5140
5241 expect ( conditions ) . toMatchObject ( {
5342 gender : [ 'male' ] ,
@@ -61,6 +50,39 @@ describe('Basic test of vue-condition-watcher', () => {
6150 results : 10 ,
6251 } )
6352 } )
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+ } )
6486} )
6587
6688// const tick = async (times: number) => {
0 commit comments