Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Test Cases for Utilities #4

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading