@@ -47,8 +47,6 @@ test('clear pagination should reset search and page', () => {
4747 { wrapper }
4848 )
4949
50- console . log ( window . location . search )
51-
5250 act ( ( ) => {
5351 result . current . actions . query ( { search : 'Max' } )
5452 } )
@@ -79,3 +77,77 @@ test('change default parameters', () => {
7977 expect ( result . current . page ) . toBe ( 1 )
8078 expect ( result . current . size ) . toBe ( 10 )
8179} )
80+
81+ test ( 'query multiple different properties, should keep them all' , ( ) => {
82+ const { result } = renderHook (
83+ ( ) =>
84+ useQueryAndPagination ( {
85+ defaultQueryParameters : { search : '' , department : '' } ,
86+ } ) ,
87+ { wrapper }
88+ )
89+
90+ act ( ( ) => {
91+ result . current . actions . query ( { search : 'Max' } )
92+ result . current . actions . query ( { department : 'IT' } )
93+ } )
94+
95+ expect ( result . current . queryParameters . search ) . toBe ( 'Max' )
96+ expect ( result . current . queryParameters . department ) . toBe ( 'IT' )
97+ } )
98+
99+ test ( 'query a property that is not configured, should do nothing' , ( ) => {
100+ const { result } = renderHook (
101+ ( ) =>
102+ useQueryAndPagination ( {
103+ defaultQueryParameters : { search : '' } ,
104+ } ) ,
105+ { wrapper }
106+ )
107+
108+ act ( ( ) => {
109+ result . current . actions . query ( { department : 'IT' } )
110+ } )
111+
112+ expect ( result . current . queryParameters . search ) . toBe ( '' )
113+ expect ( result . current . queryParameters . department ) . toBeUndefined ( )
114+ } )
115+
116+ test ( 'properties in the URL, that are not part of the configuration should be left untouched' , ( ) => {
117+ window . history . pushState ( { } , '' , '/?greeting=hello' )
118+
119+ const { result } = renderHook (
120+ ( ) =>
121+ useQueryAndPagination ( {
122+ defaultQueryParameters : { search : '' } ,
123+ } ) ,
124+ { wrapper }
125+ )
126+
127+ act ( ( ) => {
128+ result . current . actions . query ( { search : 'Max' } )
129+ } )
130+
131+ expect ( result . current . queryParameters . search ) . toBe ( 'Max' )
132+ expect ( result . current . queryParameters . greeting ) . toBeUndefined ( )
133+ expect ( window . location . search ) . toBe ( '?greeting=hello&search=Max' )
134+ } )
135+
136+ test ( 'query property with default value, should remove it from url' , ( ) => {
137+ window . history . pushState ( { } , '' , '/?search=Anton' )
138+
139+ const { result } = renderHook (
140+ ( ) =>
141+ useQueryAndPagination ( {
142+ defaultQueryParameters : { search : '' } ,
143+ } ) ,
144+ { wrapper }
145+ )
146+
147+ act ( ( ) => {
148+ result . current . actions . query ( { search : '' } )
149+ } )
150+
151+ expect ( result . current . queryParameters . search ) . toBe ( '' )
152+ expect ( window . location . search ) . toBe ( '' )
153+ } )
0 commit comments