-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from iamhectorsosa/testing
Adding Test Cases for Utilities
- Loading branch information
Showing
4 changed files
with
116 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@search-params/react": patch | ||
--- | ||
|
||
Adding Utility Tests Cases |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ | |
"outputs": [ | ||
".next/**", | ||
"!.next/cache/**" | ||
] | ||
], | ||
"cache": false | ||
}, | ||
"lint": {}, | ||
"test": {}, | ||
|