From ed2c14c3ea9ce3e9fc959b4a2c929836c995f52d Mon Sep 17 00:00:00 2001 From: Hector Sosa Date: Mon, 20 Nov 2023 22:58:00 +0100 Subject: [PATCH 1/3] Adding Testing --- packages/react/src/utils/index.test.ts | 35 +++++++++++++++++++++++--- packages/react/src/utils/index.ts | 6 ++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/react/src/utils/index.test.ts b/packages/react/src/utils/index.test.ts index f5bab0e..81f933c 100644 --- a/packages/react/src/utils/index.test.ts +++ b/packages/react/src/utils/index.test.ts @@ -1,6 +1,33 @@ -// sum.test.js -import { expect, test } from "vitest"; +import { describe, expect, test } from "vitest"; +import { validate } from "."; -test("Enabling Test", () => { - expect(true).toBe(true); +const schemaValidatorFn = (search: Record) => ({ + foo: search.foo || 1, +}); + +describe("validate", () => { + test("Runs validator fn with fallback when no search string is provided", () => { + const validatedParams = validate(schemaValidatorFn); + expect(validatedParams).toStrictEqual({ foo: 1 }); + }); + + test("Runs validator fn when search string is provided", () => { + const validatedParams = validate(schemaValidatorFn, "?foo=100"); + expect(validatedParams).toStrictEqual({ foo: 100 }); + }); + + test("Runs validator fn when search params are provided", () => { + const validatedParams = validate(schemaValidatorFn, { foo: 100 }); + expect(validatedParams).toStrictEqual({ foo: 100 }); + }); + + test("Runs validator fn with fallback when unexpected search string is provided", () => { + const validatedParams = validate(schemaValidatorFn, "?bar=100"); + expect(validatedParams).toStrictEqual({ foo: 1 }); + }); + + test("Runs validator fn with fallback when unexpected search params are provided", () => { + const validatedParams = validate(schemaValidatorFn, { wrong: 100 }); + expect(validatedParams).toStrictEqual({ foo: 1 }); + }); }); diff --git a/packages/react/src/utils/index.ts b/packages/react/src/utils/index.ts index 85fbe4d..6378b52 100644 --- a/packages/react/src/utils/index.ts +++ b/packages/react/src/utils/index.ts @@ -27,13 +27,13 @@ function parseQueryParams(search: string): SearchParams { export function validate< TSchemaValidatorFn extends SearchParamsConfig[keyof SearchParamsConfig] >( - routeValidator: TSchemaValidatorFn, + schemaValidator: TSchemaValidatorFn, search: string | SearchParams = "" ): ReturnType { return ( typeof search === "string" - ? routeValidator(parseQueryParams(search)) - : routeValidator(search) + ? schemaValidator(parseQueryParams(search)) + : schemaValidator(search) ) as ReturnType; } From 86f8d0c5af3e70555bc973af51dbe41fa6508ef0 Mon Sep 17 00:00:00 2001 From: iamhectorsosa Date: Tue, 21 Nov 2023 11:19:35 +0100 Subject: [PATCH 2/3] Updating Pipelines and adding util tests --- packages/react/src/utils/index.test.ts | 95 +++++++++++++++++++++++--- packages/react/src/utils/index.ts | 7 +- turbo.json | 3 +- 3 files changed, 88 insertions(+), 17 deletions(-) diff --git a/packages/react/src/utils/index.test.ts b/packages/react/src/utils/index.test.ts index 81f933c..8e72fee 100644 --- a/packages/react/src/utils/index.test.ts +++ b/packages/react/src/utils/index.test.ts @@ -1,33 +1,106 @@ import { describe, expect, test } from "vitest"; -import { validate } from "."; +import { stringify, validate } from "."; const schemaValidatorFn = (search: Record) => ({ - foo: search.foo || 1, + foo: search?.foo || 1, + bar: search?.bar || "hello", + baz: search?.baz || false, + qux: search?.qux || undefined, }); describe("validate", () => { test("Runs validator fn with fallback when no search string is provided", () => { const validatedParams = validate(schemaValidatorFn); - expect(validatedParams).toStrictEqual({ foo: 1 }); + expect(validatedParams).toStrictEqual({ + foo: 1, + bar: "hello", + baz: false, + qux: undefined, + }); }); test("Runs validator fn when search string is provided", () => { - const validatedParams = validate(schemaValidatorFn, "?foo=100"); - expect(validatedParams).toStrictEqual({ foo: 100 }); + const validatedParams = validate( + schemaValidatorFn, + "?foo=100&bar=goodbye&baz=true&qux=%5B%22one%22%2C%22two%22%5D" + ); + expect(validatedParams).toStrictEqual({ + foo: 100, + bar: "goodbye", + baz: true, + qux: ["one", "two"], + }); }); test("Runs validator fn when search params are provided", () => { - const validatedParams = validate(schemaValidatorFn, { foo: 100 }); - expect(validatedParams).toStrictEqual({ foo: 100 }); + const validatedParams = validate(schemaValidatorFn, { + foo: 100, + bar: "goodbye", + baz: true, + qux: ["one", "two"], + }); + expect(validatedParams).toStrictEqual({ + foo: 100, + bar: "goodbye", + baz: true, + qux: ["one", "two"], + }); }); test("Runs validator fn with fallback when unexpected search string is provided", () => { - const validatedParams = validate(schemaValidatorFn, "?bar=100"); - expect(validatedParams).toStrictEqual({ foo: 1 }); + const validatedParams = validate( + schemaValidatorFn, + "?foo2=100&bar=goodbye&baz=true&qux=%5B%22one%22%2C%22two%22%5D" + ); + expect(validatedParams).toStrictEqual({ + foo: 1, + bar: "goodbye", + baz: true, + qux: ["one", "two"], + }); }); test("Runs validator fn with fallback when unexpected search params are provided", () => { - const validatedParams = validate(schemaValidatorFn, { wrong: 100 }); - expect(validatedParams).toStrictEqual({ foo: 1 }); + const validatedParams = validate(schemaValidatorFn, { + foo2: 100, + bar: "goodbye", + baz: true, + qux: ["one", "two"], + }); + expect(validatedParams).toStrictEqual({ + foo: 1, + bar: "goodbye", + baz: true, + qux: ["one", "two"], + }); + }); +}); + +describe("stringify", () => { + test("Returns an empty string when params are empty", () => { + const stringifiedParams = stringify({}); + expect(stringifiedParams).toBe(""); + }); + + test("Stringifies query params", () => { + const stringifiedParams = stringify({ + foo: 100, + bar: "goodbye", + baz: true, + qux: ["one", "two"], + }); + expect(stringifiedParams).toBe( + "?foo=100&bar=goodbye&baz=true&qux=%5B%22one%22%2C%22two%22%5D" + ); + }); + + test("Stringifies query params filtering out undefined, empty strings, null and empty objects", () => { + const stringifiedParams = stringify({ + foo: 0, + bar: "", + baz: undefined, + qux: [], + }); + expect(stringifiedParams).toBe("?foo=0"); }); }); diff --git a/packages/react/src/utils/index.ts b/packages/react/src/utils/index.ts index 6378b52..71bdbbd 100644 --- a/packages/react/src/utils/index.ts +++ b/packages/react/src/utils/index.ts @@ -39,10 +39,7 @@ export function validate< export function stringify< TSchema extends ReturnType ->( - input: TSchema, - config: { addQueryPrefix: boolean } = { addQueryPrefix: true } -): string { +>(input: TSchema): string { const filteredInput = Object.fromEntries( Object.entries({ ...input }) .filter( @@ -61,7 +58,7 @@ export function stringify< ); if (Object.entries(filteredInput).length > 0) { const queryString = new URLSearchParams(filteredInput).toString(); - return config?.addQueryPrefix ? "?" + queryString : queryString; + return "?" + queryString; } else { return ""; } diff --git a/turbo.json b/turbo.json index a37c537..0cf7c00 100644 --- a/turbo.json +++ b/turbo.json @@ -8,7 +8,8 @@ "outputs": [ ".next/**", "!.next/cache/**" - ] + ], + "cache": false }, "lint": {}, "test": {}, From c5daef3460bf2a8e4d12697efe73ebd12928f77d Mon Sep 17 00:00:00 2001 From: iamhectorsosa Date: Tue, 21 Nov 2023 11:20:20 +0100 Subject: [PATCH 3/3] Adding Changeset --- .changeset/famous-scissors-judge.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/famous-scissors-judge.md diff --git a/.changeset/famous-scissors-judge.md b/.changeset/famous-scissors-judge.md new file mode 100644 index 0000000..53991ba --- /dev/null +++ b/.changeset/famous-scissors-judge.md @@ -0,0 +1,5 @@ +--- +"@search-params/react": patch +--- + +Adding Utility Tests Cases