-
Notifications
You must be signed in to change notification settings - Fork 55
refactor: derive the current branch from the URL instead of syncing an atom #10128
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f2e40fe
refactor: derive the current branch from the URL instead of syncing a…
bilalabbad 574b9bc
test: cover BranchesProvider branch resolution and switching
bilalabbad 1531329
more
bilalabbad dd073ff
test: tighten BranchesProvider coverage
bilalabbad 0eaf81d
harvesting review
bilalabbad 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
170 changes: 170 additions & 0 deletions
170
frontend/app/src/entities/branches/ui/branches-provider.test.tsx
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,170 @@ | ||
| import { beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| import { QSP } from "@/shared/config/qsp"; | ||
|
|
||
| import type { BranchListItem } from "@/entities/branches/domain/model/branch"; | ||
| import { BranchesProvider, useCurrentBranch } from "@/entities/branches/ui/branches-provider"; | ||
| import { useGetBranches } from "@/entities/branches/ui/queries/get-branches.query"; | ||
|
|
||
| import { render } from "../../../../tests/components/render"; | ||
| import { generateBranch } from "../../../../tests/fake/branch"; | ||
|
|
||
| vi.mock("@/entities/branches/ui/queries/get-branches.query"); | ||
|
|
||
| // Deliberately not named "main": INFRAHUB_INITIAL_DEFAULT_BRANCH can rename the default branch, | ||
| // so the provider has to resolve it from is_default. | ||
| const defaultBranch = generateBranch({ id: "branch-default", name: "primary", is_default: true }); | ||
| const featureBranch = generateBranch({ id: "branch-feature", name: "feature-1" }); | ||
|
|
||
| type BranchesQueryState = { | ||
| data?: Array<BranchListItem>; | ||
| isPending: boolean; | ||
| error: Error | null; | ||
| }; | ||
|
|
||
| const mockBranchesQuery = (state: BranchesQueryState) => | ||
| vi.mocked(useGetBranches).mockReturnValue(state as ReturnType<typeof useGetBranches>); | ||
|
|
||
| const mockFetchedBranches = () => | ||
| mockBranchesQuery({ data: [defaultBranch, featureBranch], isPending: false, error: null }); | ||
|
|
||
| const seedBranchInUrl = (branchName: string) => | ||
| window.history.replaceState(null, "", `${window.location.pathname}?${QSP.BRANCH}=${branchName}`); | ||
|
|
||
| const getBranchInUrl = () => new URLSearchParams(window.location.search).get(QSP.BRANCH); | ||
|
|
||
| function BranchProbe({ switchTo }: { switchTo?: BranchListItem }) { | ||
| const { currentBranch, setCurrentBranch } = useCurrentBranch(); | ||
|
|
||
| return ( | ||
| <> | ||
| <p>Current branch: {currentBranch.name}</p> | ||
|
|
||
| {switchTo && ( | ||
| <button type="button" onClick={() => setCurrentBranch(switchTo)}> | ||
| Switch branch | ||
| </button> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| describe("BranchesProvider", () => { | ||
| beforeEach(() => { | ||
| window.history.replaceState(null, "", window.location.pathname); | ||
| }); | ||
|
|
||
| test("resolves the default branch when the URL has no branch", async () => { | ||
| // GIVEN | ||
| mockFetchedBranches(); | ||
|
|
||
| // WHEN | ||
| const component = await render( | ||
| <BranchesProvider> | ||
| <BranchProbe /> | ||
| </BranchesProvider> | ||
| ); | ||
|
|
||
| // THEN | ||
| await expect.element(component.getByText("Current branch: primary")).toBeVisible(); | ||
| }); | ||
|
|
||
| test("resolves the branch named in the URL", async () => { | ||
| // GIVEN | ||
| mockFetchedBranches(); | ||
| seedBranchInUrl(featureBranch.name); | ||
|
|
||
| // WHEN | ||
| const component = await render( | ||
| <BranchesProvider> | ||
| <BranchProbe /> | ||
| </BranchesProvider> | ||
| ); | ||
|
|
||
| // THEN | ||
| await expect.element(component.getByText("Current branch: feature-1")).toBeVisible(); | ||
| }); | ||
|
|
||
| test("hides its children while the branches are being fetched", async () => { | ||
| // GIVEN | ||
| mockBranchesQuery({ isPending: true, error: null }); | ||
|
|
||
| // WHEN | ||
| const component = await render( | ||
| <BranchesProvider> | ||
| <BranchProbe /> | ||
| </BranchesProvider> | ||
| ); | ||
|
|
||
| // THEN | ||
| await expect.element(component.getByText("Loading branches...")).toBeVisible(); | ||
| expect(component.getByText(/Current branch/).query()).toBeNull(); | ||
| }); | ||
|
|
||
| test("shows an error screen when the branches cannot be fetched", async () => { | ||
| // GIVEN | ||
| mockBranchesQuery({ isPending: false, error: new Error("Branches are unreachable") }); | ||
|
|
||
| // WHEN | ||
| const component = await render( | ||
| <BranchesProvider> | ||
| <BranchProbe /> | ||
| </BranchesProvider> | ||
| ); | ||
|
|
||
| // THEN | ||
| await expect.element(component.getByText("Branches are unreachable")).toBeVisible(); | ||
| }); | ||
|
|
||
| test("drops the branch parameter when switching to the default branch", async () => { | ||
| // GIVEN | ||
| mockFetchedBranches(); | ||
| seedBranchInUrl(featureBranch.name); | ||
| const component = await render( | ||
| <BranchesProvider> | ||
| <BranchProbe switchTo={defaultBranch} /> | ||
| </BranchesProvider> | ||
| ); | ||
|
|
||
| // WHEN | ||
| await component.getByRole("button", { name: "Switch branch" }).click(); | ||
|
|
||
| // THEN | ||
| await expect.poll(getBranchInUrl).toBeNull(); | ||
| }); | ||
|
|
||
| test("writes the branch name when switching to a non-default branch", async () => { | ||
| // GIVEN | ||
| mockFetchedBranches(); | ||
| const component = await render( | ||
| <BranchesProvider> | ||
| <BranchProbe switchTo={featureBranch} /> | ||
| </BranchesProvider> | ||
| ); | ||
|
|
||
| // WHEN | ||
| await component.getByRole("button", { name: "Switch branch" }).click(); | ||
|
|
||
| // THEN | ||
| await expect.poll(getBranchInUrl).toBe("feature-1"); | ||
| }); | ||
|
|
||
| test("falls back to the default branch when the URL names an unknown branch", async () => { | ||
| // GIVEN | ||
| mockFetchedBranches(); | ||
| seedBranchInUrl("does-not-exist"); | ||
|
|
||
| // WHEN | ||
| const component = await render( | ||
| <BranchesProvider> | ||
| <BranchProbe /> | ||
| </BranchesProvider> | ||
| ); | ||
|
|
||
| // THEN | ||
| await expect | ||
| .element(component.getByText(/not found, you have been redirected to the main branch/)) | ||
| .toBeVisible(); | ||
| await expect.poll(getBranchInUrl).toBeNull(); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.