1- import { qsNoMungeTestCases , qsTestCases } from "./node" ;
1+ import { qsNoMungeTestCases , qsTestCases , qsWeirdObjects } from "./node" ;
22import qs from "../lib" ;
33import { test , assert } from "vitest" ;
44import querystring from "querystring" ;
55
66test ( "should succeed on node.js tests" , ( ) => {
7+ qsWeirdObjects . forEach (
8+ ( t ) =>
9+ assert . deepEqual (
10+ qs . stringify ( t [ 2 ] as Record < string , any > ) ,
11+ t [ 1 ] as string ,
12+ ) ,
13+ ) ;
714 qsNoMungeTestCases . forEach ( ( t ) => assert . deepEqual ( qs . stringify ( t [ 1 ] ) , t [ 0 ] ) ) ;
815 qsTestCases . forEach ( ( t ) => assert . deepEqual ( qs . stringify ( t [ 2 ] ) , t [ 1 ] ) ) ;
916} ) ;
@@ -29,6 +36,8 @@ test("should handle BigInt", () => {
2936 qs . stringify ( { age : BigInt ( 55 ) , name : "John" } ) ,
3037 "age=55&name=John" ,
3138 ) ;
39+ assert . strictEqual ( qs . stringify ( { foo : 2n ** 1023n } ) , "foo=" + 2n ** 1023n ) ;
40+ assert . strictEqual ( qs . stringify ( [ 0n , 1n , 2n ] ) , "0=0&1=1&2=2" ) ;
3241} ) ;
3342
3443test ( "should handle boolean values" , ( ) => {
@@ -52,3 +61,36 @@ test("should omit non-object inputs", () => {
5261test ( "should handle multi-byte characters" , ( ) => {
5362 assert . deepEqual ( qs . stringify ( { multiByte : "𝌆" } ) , "multiByte=%F0%9D%8C%86" ) ;
5463} ) ;
64+
65+ test ( "invalid surrogate pair should throw" , ( ) => {
66+ assert . throws ( ( ) => qs . stringify ( { foo : "\udc00" } ) , "URI malformed" ) ;
67+ } ) ;
68+
69+ test ( "should omit nested values" , ( ) => {
70+ const f = qs . stringify ( {
71+ a : "b" ,
72+ q : qs . stringify ( {
73+ x : "y" ,
74+ y : "z" ,
75+ } ) ,
76+ } ) ;
77+ assert . strictEqual ( f , "a=b&q=x%3Dy%26y%3Dz" ) ;
78+ } ) ;
79+
80+ test ( "should coerce numbers to string" , ( ) => {
81+ assert . strictEqual ( qs . stringify ( { foo : 0 } ) , "foo=0" ) ;
82+ assert . strictEqual ( qs . stringify ( { foo : - 0 } ) , "foo=0" ) ;
83+ assert . strictEqual ( qs . stringify ( { foo : 3 } ) , "foo=3" ) ;
84+ assert . strictEqual ( qs . stringify ( { foo : - 72.42 } ) , "foo=-72.42" ) ;
85+ assert . strictEqual ( qs . stringify ( { foo : NaN } ) , "foo=" ) ;
86+ assert . strictEqual ( qs . stringify ( { foo : 1e21 } ) , "foo=1e%2B21" ) ;
87+ assert . strictEqual ( qs . stringify ( { foo : Infinity } ) , "foo=" ) ;
88+ } ) ;
89+
90+ test ( "should return empty string on certain inputs" , ( ) => {
91+ assert . strictEqual ( qs . stringify ( ) , "" ) ;
92+ assert . strictEqual ( qs . stringify ( 0 ) , "" ) ;
93+ assert . strictEqual ( qs . stringify ( [ ] ) , "" ) ;
94+ assert . strictEqual ( qs . stringify ( null ) , "" ) ;
95+ assert . strictEqual ( qs . stringify ( true ) , "" ) ;
96+ } ) ;
0 commit comments