@@ -28,30 +28,79 @@ describe('useFilterValidation', () => {
2828 const schema = result . current ;
2929
3030 const valid = schema . validate ( { filter : { status : true , owners : [ 'a' ] } } ) ;
31- expect ( valid . errors ) . toBeNull ( ) ;
31+ expect ( valid ) . toMatchObject ( {
32+ success : true ,
33+ data : { filter : { status : true , owners : [ 'a' ] } } ,
34+ errors : null ,
35+ } ) ;
3236
3337 const invalid = schema . validate ( {
3438 filter : { status : 'invalid' , owners : [ ] } ,
3539 } ) ;
36- expect ( invalid . errors ) . not . toBeNull ( ) ;
40+ expect ( invalid ) . toMatchObject ( { success : false , data : null } ) ;
3741 } ) ;
3842
3943 it ( 'validates optional search string when enabled' , ( ) => {
4044 const { result } = renderHook ( ( ) => useFilterValidation ( filters , true ) ) ;
4145 const schema = result . current ;
4246
4347 // include valid filter content so only search is under test
44- expect (
45- schema . validate ( { search : 'abc' , filter : { owners : [ 'x' ] } } ) . errors ,
46- ) . toBe ( null ) ;
47- expect (
48- schema . validate ( { search : 123 , filter : { owners : [ 'x' ] } } ) . errors ,
49- ) . not . toBeNull ( ) ;
50- expect (
51- schema . validate ( {
52- search : 123 ,
53- filter : JSON . stringify ( { owners : [ 'x' ] } ) ,
54- } ) . errors ,
55- ) . not . toBeNull ( ) ;
48+ const ok = schema . validate ( { search : 'abc' , filter : { owners : [ 'x' ] } } ) ;
49+ expect ( ok ) . toMatchObject ( {
50+ success : true ,
51+ data : { search : 'abc' , filter : { owners : [ 'x' ] } } ,
52+ errors : null ,
53+ } ) ;
54+
55+ const invalidSearch = schema . validate ( {
56+ search : 123 as unknown as string ,
57+ filter : { owners : [ 'x' ] } ,
58+ } ) ;
59+ expect ( invalidSearch ) . toMatchObject ( { success : false , data : null } ) ;
60+ } ) ;
61+
62+ it ( 'parses stringified JSON filter into object in output data' , ( ) => {
63+ const { result } = renderHook ( ( ) => useFilterValidation ( filters , true ) ) ;
64+ const schema = result . current ;
65+
66+ const jsonFilter = JSON . stringify ( { status : true , owners : [ 'x' ] } ) ;
67+ const validationResult = schema . validate ( {
68+ search : 'ok' ,
69+ filter : jsonFilter ,
70+ } ) ;
71+ expect ( validationResult ) . toMatchObject ( {
72+ success : true ,
73+ data : { search : 'ok' , filter : { status : true , owners : [ 'x' ] } } ,
74+ errors : null ,
75+ } ) ;
76+ } ) ;
77+
78+ it ( 'accepts when filter is null and returns undefined filter data' , ( ) => {
79+ const { result } = renderHook ( ( ) => useFilterValidation ( filters , true ) ) ;
80+ const schema = result . current ;
81+
82+ const validationResult = schema . validate ( { search : 'test' , filter : null } ) ;
83+ expect ( validationResult ) . toMatchObject ( {
84+ success : true ,
85+ // filter should be undefined
86+ data : { filter : undefined , search : 'test' } ,
87+ errors : null ,
88+ } ) ;
89+ } ) ;
90+
91+ it ( 'accepts when filter is omitted and and returns undefined filter data' , ( ) => {
92+ const { result } = renderHook ( ( ) => useFilterValidation ( filters , true ) ) ;
93+ const schema = result . current ;
94+
95+ const validationResult = schema . validate ( {
96+ search : 'test' ,
97+ filter : undefined ,
98+ } ) ;
99+ expect ( validationResult ) . toMatchObject ( {
100+ success : true ,
101+ // filter should be undefined
102+ data : { filter : undefined , search : 'test' } ,
103+ errors : null ,
104+ } ) ;
56105 } ) ;
57106} ) ;
0 commit comments