|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 |
|
3 | | -import { createSearchParams, createURL, parseSearchParams } from "../src/index.js"; |
| 3 | +import { createSearchParams, createURL, getLiteralValue, parseSearchParams } from "../src/index.js"; |
4 | 4 |
|
5 | 5 | describe("createSearchParams", () => { |
6 | 6 | it("should create basic search params", () => { |
@@ -72,6 +72,27 @@ describe("createSearchParams", () => { |
72 | 72 | "limit=10&search=term&offset=undefined&tagIds%5B%5D=123&tagIds%5B%5D=null&tagIds%5B%5D=321&active=Yes&skip=null", |
73 | 73 | ); |
74 | 74 | }); |
| 75 | + |
| 76 | + it("should treat an empty array value as no params", () => { |
| 77 | + const params = createSearchParams({ a: [], b: "test" }); |
| 78 | + expect(params.toString()).toBe("b=test"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should treat a two-element array whose first element is not an object as a regular array", () => { |
| 82 | + const params = createSearchParams({ key: ["a", "b"] }); |
| 83 | + expect(params.toString()).toBe("key=a&key=b"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should treat a two-element array whose second element is not a string as a regular array", () => { |
| 87 | + const params = createSearchParams({ key: [{ val: "x" }, 42] }); |
| 88 | + expect(params.toString()).toBe("key=%5Bobject+Object%5D&key=42"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should serialize as 'undefined' for an Option tuple key that does not exist in the record", () => { |
| 92 | + const options = { eager: "EagerValue" }; |
| 93 | + const params = createSearchParams({ mode: [options, "missing"] }); |
| 94 | + expect(params.get("mode")).toBe("undefined"); |
| 95 | + }); |
75 | 96 | }); |
76 | 97 |
|
77 | 98 | describe("createURL", () => { |
@@ -241,4 +262,33 @@ describe("parseSearchParams", () => { |
241 | 262 | const result = parseSearchParams(searchParams); |
242 | 263 | expect(result).toEqual({ key: ["value1", "value2"] }); |
243 | 264 | }); |
| 265 | + |
| 266 | + it("should skip null/undefined array entries by default", () => { |
| 267 | + const searchParams = new URLSearchParams("ids[]=1&ids[]=null&ids[]=2"); |
| 268 | + const result = parseSearchParams(searchParams); |
| 269 | + expect(result).toEqual({ "ids[]": ["1", null, "2"] }); |
| 270 | + }); |
| 271 | + |
| 272 | + it("should accumulate more than two values for the same non-[] key into an array", () => { |
| 273 | + const searchParams = new URLSearchParams("tag=a&tag=b&tag=c"); |
| 274 | + const result = parseSearchParams(searchParams); |
| 275 | + expect(result).toEqual({ tag: ["a", "b", "c"] }); |
| 276 | + }); |
| 277 | +}); |
| 278 | + |
| 279 | +describe("getLiteralValue", () => { |
| 280 | + it("should convert the string 'null' to null", () => { |
| 281 | + expect(getLiteralValue("null")).toBeNull(); |
| 282 | + }); |
| 283 | + |
| 284 | + it("should convert the string 'undefined' to undefined", () => { |
| 285 | + expect(getLiteralValue("undefined")).toBeUndefined(); |
| 286 | + }); |
| 287 | + |
| 288 | + it("should return any other string unchanged", () => { |
| 289 | + expect(getLiteralValue("hello")).toBe("hello"); |
| 290 | + expect(getLiteralValue("")).toBe(""); |
| 291 | + expect(getLiteralValue("0")).toBe("0"); |
| 292 | + expect(getLiteralValue("false")).toBe("false"); |
| 293 | + }); |
244 | 294 | }); |
0 commit comments