File tree Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,9 @@ export const parseQuery = (queries: MicroCMSQueries): string => {
1414 const queryString = new URLSearchParams (
1515 Object . entries ( queries ) . reduce (
1616 ( acc , [ key , value ] ) => {
17- acc [ key ] = String ( value ) ;
17+ if ( value !== undefined ) {
18+ acc [ key ] = String ( value ) ;
19+ }
1820 return acc ;
1921 } ,
2022 { } as Record < string , string > ,
Original file line number Diff line number Diff line change @@ -7,14 +7,45 @@ describe('parseQuery', () => {
77 limit : 100 ,
88 fields : [ 'id' , 'title' ] ,
99 orders : 'publishedAt' ,
10- } )
10+ } ) ,
1111 ) . toBe ( 'limit=100&fields=id%2Ctitle&orders=publishedAt' ) ;
1212 } ) ;
13+
1314 test ( 'Throws an error if a non-object is specified' , ( ) => {
1415 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1516 // @ts -expect-error
1617 expect ( ( ) => parseQuery ( '' ) ) . toThrowError (
17- new Error ( 'queries is not object' )
18+ new Error ( 'queries is not object' ) ,
1819 ) ;
1920 } ) ;
21+
22+ test ( 'Undefined values are removed from the query string' , ( ) => {
23+ expect (
24+ parseQuery ( {
25+ limit : 100 ,
26+ fields : undefined ,
27+ orders : 'publishedAt' ,
28+ } ) ,
29+ ) . toBe ( 'limit=100&orders=publishedAt' ) ;
30+ } ) ;
31+
32+ test ( 'Multiple undefined values are removed from the query string' , ( ) => {
33+ expect (
34+ parseQuery ( {
35+ limit : undefined ,
36+ fields : undefined ,
37+ orders : 'publishedAt' ,
38+ } ) ,
39+ ) . toBe ( 'orders=publishedAt' ) ;
40+ } ) ;
41+
42+ test ( 'All undefined values results in an empty query string' , ( ) => {
43+ expect (
44+ parseQuery ( {
45+ limit : undefined ,
46+ fields : undefined ,
47+ orders : undefined ,
48+ } ) ,
49+ ) . toBe ( '' ) ;
50+ } ) ;
2051} ) ;
You can’t perform that action at this time.
0 commit comments