-
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
Merged
Changes from 2 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
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,133 @@ | ||
| import { | ||
| hasParams, | ||
| isSynced, | ||
| stringifyQuery, | ||
| wasPop, | ||
| } from "../src/hooks/stateSyncManager/hooks/UseStateSync/utils"; | ||
| import { NextHistoryState } from "../src/services/beforePopState/types"; | ||
|
frano-m marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Builds a minimal NextHistoryState for tests. | ||
| * @param url - The resolved URL the browser is navigating to (the form | ||
| * Next.js stores in history state — always the actual URL, never a route | ||
| * pattern). | ||
| * @returns NextHistoryState with the given url and no-op as/options. | ||
| */ | ||
| function buildHistoryState(url: string): NextHistoryState { | ||
| return { as: url, options: {}, url }; | ||
| } | ||
|
frano-m marked this conversation as resolved.
|
||
|
|
||
| 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 the usePathname() migration relies on: pathname is | ||
| // the resolved URL (e.g. /anvil-cmg/abc-123), not the route pattern | ||
| // (/[entityListType]/[entityId]). The first matches; the second does not. | ||
| it("matches when pathname is the resolved URL (post-migration form)", () => { | ||
| expect( | ||
| wasPop( | ||
| "", | ||
| "/anvil-cmg/abc-123", | ||
| buildHistoryState("/anvil-cmg/abc-123?filter=foo"), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("does not match when pathname is a route pattern (pre-migration form on dynamic routes)", () => { | ||
| expect( | ||
| wasPop( | ||
| "", | ||
| "/[entityListType]/[entityId]", | ||
| buildHistoryState("/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.