-
Notifications
You must be signed in to change notification settings - Fork 0
feat: useRouter().pathname → usePathname() (#928) #938
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
Merged
+148
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7b408b1
feat: useRouter().pathname → usePathname() (#928)
frano-m 5dc00b5
test: cover wasPop and friends in UseStateSync/utils (#928)
frano-m c9db476
fix: keep useRouter().pathname in useStateSync; correct test contract…
frano-m e365cb2
chore: use type-only import for NextHistoryState in tests (#928)
frano-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,138 @@ | ||
| import { | ||
| hasParams, | ||
| isSynced, | ||
| stringifyQuery, | ||
| wasPop, | ||
| } from "../src/hooks/stateSyncManager/hooks/UseStateSync/utils"; | ||
| import type { NextHistoryState } from "../src/services/beforePopState/types"; | ||
|
|
||
| /** | ||
| * Builds a minimal NextHistoryState for tests. | ||
| * @param url - The href stored by Next.js in history state (the route | ||
| * pattern on dynamic routes, e.g. `/[entityListType]/[entityId]`; the same | ||
| * string as the URL on static routes). | ||
| * @param as - The resolved URL shown to the user. Defaults to `url` for | ||
| * static-route cases where the two are identical. | ||
| * @returns NextHistoryState with the given fields and no-op options. | ||
| */ | ||
| function buildHistoryState(url: string, as: string = url): NextHistoryState { | ||
| return { as, options: {}, url }; | ||
| } | ||
|
|
||
| describe("wasPop", () => { | ||
| it("returns false when nextHistoryState is undefined", () => { | ||
| expect(wasPop("", "/projects", undefined)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns true when pathname matches the path component of the history URL", () => { | ||
| expect(wasPop("", "/projects", buildHistoryState("/projects"))).toBe(true); | ||
| }); | ||
|
|
||
| it("strips the query string off the history URL before comparing", () => { | ||
| expect( | ||
| wasPop("", "/projects", buildHistoryState("/projects?filter=foo")), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when pathname does not match", () => { | ||
| expect(wasPop("", "/projects", buildHistoryState("/files"))).toBe(false); | ||
| }); | ||
|
|
||
| it("defaults basePath to empty string when not provided", () => { | ||
| expect(wasPop(undefined, "/projects", buildHistoryState("/projects"))).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it("prepends basePath to pathname before comparing", () => { | ||
| expect( | ||
| wasPop("/data", "/projects", buildHistoryState("/data/projects")), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when basePath is set but missing from the history URL", () => { | ||
| expect(wasPop("/data", "/projects", buildHistoryState("/projects"))).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| // Documents the contract: nextHistoryState.url is the href stored by | ||
| // Next.js — the route pattern on dynamic routes, the static URL on static | ||
| // routes. `pathname` must be in the same form for the comparison to match. | ||
| it("matches on a dynamic route when both sides are the route pattern", () => { | ||
| expect( | ||
| wasPop( | ||
| "", | ||
| "/[entityListType]/[entityId]", | ||
| buildHistoryState( | ||
| "/[entityListType]/[entityId]?filter=foo", | ||
| "/anvil-cmg/abc-123?filter=foo", | ||
| ), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("does not match when pathname is the resolved URL but history url is the route pattern", () => { | ||
| expect( | ||
| wasPop( | ||
| "", | ||
| "/anvil-cmg/abc-123", | ||
| buildHistoryState("/[entityListType]/[entityId]", "/anvil-cmg/abc-123"), | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("hasParams", () => { | ||
| it("returns true when any param key has a defined value in the query", () => { | ||
| expect(hasParams({ filter: "foo" }, ["filter"])).toBe(true); | ||
| }); | ||
|
|
||
| it("returns true when at least one of multiple param keys is present", () => { | ||
| expect(hasParams({ sort: "asc" }, ["filter", "sort"])).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when none of the param keys are in the query", () => { | ||
| expect(hasParams({ other: "x" }, ["filter", "sort"])).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false for an empty paramKeys list", () => { | ||
| expect(hasParams({ filter: "foo" }, [])).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when a param key is present but undefined", () => { | ||
| expect(hasParams({ filter: undefined }, ["filter"])).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isSynced", () => { | ||
| it("returns true for two empty queries", () => { | ||
| expect(isSynced({}, {})).toBe(true); | ||
| }); | ||
|
|
||
| it("returns true when queries have the same keys/values in different order", () => { | ||
| // eslint-disable-next-line sort-keys -- intentionally unsorted to exercise insertion-order independence. | ||
| expect(isSynced({ a: "1", b: "2" }, { b: "2", a: "1" })).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when queries differ in value", () => { | ||
| expect(isSynced({ a: "1" }, { a: "2" })).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when one query has extra keys", () => { | ||
| expect(isSynced({ a: "1" }, { a: "1", b: "2" })).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("stringifyQuery", () => { | ||
| it("produces identical output regardless of insertion order", () => { | ||
| expect(stringifyQuery({ a: "1", b: "2" })).toBe( | ||
| // eslint-disable-next-line sort-keys -- intentionally unsorted to exercise insertion-order independence. | ||
| stringifyQuery({ b: "2", a: "1" }), | ||
| ); | ||
| }); | ||
|
|
||
| it("produces empty-object JSON for an empty query", () => { | ||
| expect(stringifyQuery({})).toBe("{}"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.