1
- import { z } from 'zod' ;
2
- import { describe , it , expect , vi } from 'vitest' ;
3
- import { schema } from 'valtio-zod' ;
1
+ import { z } from 'zod'
2
+ import { describe , it , expect , vi } from 'vitest'
3
+ import { schema } from 'valtio-zod'
4
4
5
5
describe ( 'valtio-zod schema' , ( ) => {
6
6
it ( 'should create a proxy and set synchronous values correctly' , ( ) => {
7
7
const userSchema = z . object ( {
8
8
username : z . string ( ) ,
9
- age : z . number ( ) . int ( ) ,
10
- } ) ;
9
+ age : z . number ( ) . int ( )
10
+ } )
11
+
12
+ const { proxy } = schema ( userSchema )
13
+ const user = proxy ( { username : 'Alice' , age : 30 } )
11
14
12
- const { proxy } = schema ( userSchema ) ;
13
- const user = proxy ( { username : 'Alice' , age : 30 } ) ;
15
+ expect ( proxy ) . toThrowError ( )
14
16
15
- user . username = 'Bob' ;
16
- expect ( user . username ) . toBe ( 'Bob' ) ;
17
+ user . username = 'Bob'
18
+ expect ( user . username ) . toBe ( 'Bob' )
17
19
18
- user . age = 42 ;
19
- expect ( user . age ) . toBe ( 42 ) ;
20
- } ) ;
20
+ user . age = 42
21
+ expect ( user . age ) . toBe ( 42 )
22
+ } )
21
23
22
24
it ( 'should handle parseSafe correctly' , ( ) => {
23
25
const userSchema = z . object ( {
24
26
username : z . string ( ) ,
25
- age : z . number ( ) . int ( ) ,
26
- } ) ;
27
+ age : z . number ( ) . int ( )
28
+ } )
27
29
28
- const { proxy } = schema ( userSchema ) ;
30
+ const { proxy } = schema ( userSchema )
29
31
const user = proxy (
30
32
{ username : 'Alice' , age : 30 } ,
31
- { safeParse : true , errorHandler : vi . fn ( ) } ,
32
- ) ;
33
+ { safeParse : true , errorHandler : vi . fn ( ) }
34
+ )
33
35
34
- const errorHandler = vi . fn ( ) ;
36
+ const errorHandler = vi . fn ( )
35
37
36
38
// @ts -expect-error Invalid age for testing
37
- user . age = 'invalidAge' ;
39
+ user . age = 'invalidAge'
38
40
39
41
setTimeout ( ( ) => {
40
- expect ( errorHandler ) . toHaveBeenCalled ( ) ;
41
- expect ( user . age ) . toBe ( 30 ) ; // Ensure the value hasn't changed
42
- } , 100 ) ;
43
- } ) ;
42
+ expect ( errorHandler ) . toHaveBeenCalled ( )
43
+ expect ( user . age ) . toBe ( 30 ) // Ensure the value hasn't changed
44
+ } , 100 )
45
+ } )
44
46
45
47
it ( 'should use custom error handler' , ( ) => {
46
48
const userSchema = z . object ( {
47
49
username : z . string ( ) ,
48
- age : z . number ( ) . int ( ) ,
49
- } ) ;
50
+ age : z . number ( ) . int ( )
51
+ } )
50
52
51
- const errorHandler = vi . fn ( ) ;
53
+ const errorHandler = vi . fn ( )
52
54
53
- const { proxy } = schema ( userSchema ) ;
54
- const user = proxy ( { username : 'Alice' , age : 30 } , { errorHandler } ) ;
55
+ const { proxy } = schema ( userSchema )
56
+ const user = proxy ( { username : 'Alice' , age : 30 } , { errorHandler } )
55
57
56
58
try {
57
59
// Invalid age
58
60
// eslint-disable-next-line @typescript-eslint/no-explicit-any
59
- user . age = 'invalidAge' as any ;
61
+ user . age = 'invalidAge' as any
60
62
} catch ( _e ) {
61
63
// Since parseSafe is false, the error should be caught here
62
64
}
63
65
64
- expect ( errorHandler ) . toHaveBeenCalled ( ) ;
65
- } ) ;
66
+ expect ( errorHandler ) . toHaveBeenCalled ( )
67
+ } )
66
68
67
69
it ( 'should handle multi-level objects correctly' , async ( ) => {
68
70
const userSchema = z . object ( {
@@ -73,12 +75,12 @@ describe('valtio-zod schema', () => {
73
75
lastName : z . string ( ) ,
74
76
address : z . object ( {
75
77
city : z . string ( ) ,
76
- country : z . string ( ) ,
77
- } ) ,
78
- } ) ,
79
- } ) ;
78
+ country : z . string ( )
79
+ } )
80
+ } )
81
+ } )
80
82
81
- const { proxy } = schema ( userSchema ) ;
83
+ const { proxy } = schema ( userSchema )
82
84
const user = proxy ( {
83
85
username : 'Alice' ,
84
86
age : 30 ,
@@ -87,53 +89,53 @@ describe('valtio-zod schema', () => {
87
89
lastName : 'Smith' ,
88
90
address : {
89
91
city : 'Wonderland' ,
90
- country : 'Fantasy' ,
91
- } ,
92
- } ,
93
- } ) ;
92
+ country : 'Fantasy'
93
+ }
94
+ }
95
+ } )
94
96
95
97
// Ensure nested fields maintain object structure and types
96
- user . profile . address . city = 'New City' ; // Ensure the proxy update handling completes
97
- expect ( user . profile . address . city ) . toBe ( 'New City' ) ;
98
- } ) ;
99
-
100
- it ( 'should error by updating a value in a nested object' , ( ) => {
101
- const userSchema = z . object ( {
102
- username : z . string ( ) ,
103
- age : z . number ( ) . int ( ) ,
104
- profile : z . object ( {
105
- firstName : z . string ( ) ,
106
- lastName : z . string ( ) ,
107
- address : z . object ( {
108
- city : z . string ( ) ,
109
- country : z . string ( ) ,
110
- } ) ,
111
- } ) ,
112
- } ) ;
113
-
114
- const { proxy } = schema ( userSchema ) ;
115
- const user = proxy (
116
- {
117
- username : 'Alice' ,
118
- age : 30 ,
119
- profile : {
120
- firstName : 'Alice' ,
121
- lastName : 'Smith' ,
122
- address : {
123
- city : 'Wonderland' ,
124
- country : 'Fantasy' ,
125
- } ,
126
- } ,
127
- } ,
128
- { safeParse : true } ,
129
- ) ;
130
-
131
- // @ts -expect-error Invalid country type
132
- user . profile . address . country = 123 ;
133
-
134
- setTimeout ( ( ) => {
135
- // Ensure the value hasn't changed from the initial valid value
136
- expect ( user . profile . address . country ) . toBe ( 'Fantasy' ) ;
137
- } , 0 ) ;
138
- } ) ;
139
- } ) ;
98
+ user . profile . address . city = 'New City' // Ensure the proxy update handling completes
99
+ expect ( user . profile . address . city ) . toBe ( 'New City' )
100
+ } )
101
+
102
+ // it('should error by updating a value in a nested object', () => {
103
+ // const userSchema = z.object({
104
+ // username: z.string(),
105
+ // age: z.number().int(),
106
+ // profile: z.object({
107
+ // firstName: z.string(),
108
+ // lastName: z.string(),
109
+ // address: z.object({
110
+ // city: z.string(),
111
+ // country: z.string()
112
+ // })
113
+ // })
114
+ // })
115
+
116
+ // const { proxy } = schema(userSchema)
117
+ // const user = proxy(
118
+ // {
119
+ // username: 'Alice',
120
+ // age: 30,
121
+ // profile: {
122
+ // firstName: 'Alice',
123
+ // lastName: 'Smith',
124
+ // address: {
125
+ // city: 'Wonderland',
126
+ // country: 'Fantasy'
127
+ // }
128
+ // }
129
+ // },
130
+ // { safeParse: true }
131
+ // )
132
+
133
+ // // @ts -expect-error Invalid country type
134
+ // user.profile.address.country = 123)
135
+
136
+ // setTimeout(() => {
137
+ // // Ensure the value hasn't changed from the initial valid value
138
+ // expect(user.profile.address.country).toBe('Fantasy')
139
+ // }, 0)
140
+ // })
141
+ } )
0 commit comments