Skip to content

Commit

Permalink
Merge pull request #4 from iamhectorsosa/testing
Browse files Browse the repository at this point in the history
Adding Test Cases for Utilities
  • Loading branch information
iamhectorsosa authored Nov 21, 2023
2 parents 92da54d + c5daef3 commit 12f95de
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-scissors-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@search-params/react": patch
---

Adding Utility Tests Cases
108 changes: 104 additions & 4 deletions packages/react/src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,106 @@
// sum.test.js
import { expect, test } from "vitest";
import { describe, expect, test } from "vitest";
import { stringify, validate } from ".";

test("Enabling Test", () => {
expect(true).toBe(true);
const schemaValidatorFn = (search: Record<string, unknown>) => ({
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,
bar: "hello",
baz: false,
qux: undefined,
});
});

test("Runs validator fn when search string is provided", () => {
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,
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,
"?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, {
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");
});
});
13 changes: 5 additions & 8 deletions packages/react/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@ function parseQueryParams(search: string): SearchParams {
export function validate<
TSchemaValidatorFn extends SearchParamsConfig[keyof SearchParamsConfig]
>(
routeValidator: TSchemaValidatorFn,
schemaValidator: TSchemaValidatorFn,
search: string | SearchParams = ""
): ReturnType<TSchemaValidatorFn> {
return (
typeof search === "string"
? routeValidator(parseQueryParams(search))
: routeValidator(search)
? schemaValidator(parseQueryParams(search))
: schemaValidator(search)
) as ReturnType<TSchemaValidatorFn>;
}

export function stringify<
TSchema extends ReturnType<SearchParamsConfig[keyof SearchParamsConfig]>
>(
input: TSchema,
config: { addQueryPrefix: boolean } = { addQueryPrefix: true }
): string {
>(input: TSchema): string {
const filteredInput = Object.fromEntries(
Object.entries({ ...input })
.filter(
Expand All @@ -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 "";
}
Expand Down
3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"outputs": [
".next/**",
"!.next/cache/**"
]
],
"cache": false
},
"lint": {},
"test": {},
Expand Down

0 comments on commit 12f95de

Please sign in to comment.