Skip to content

Commit dd073ff

Browse files
bilalabbadclaude
andcommitted
test: tighten BranchesProvider coverage
Derive the query mock state from the hook's return type instead of a hand-written shape that could drift from it, and cover two more cases: an explicit default branch in the URL, and a background refetch that must not replace the children with the loading screen. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 1531329 commit dd073ff

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

frontend/app/src/entities/branches/ui/branches-provider.test.tsx

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ vi.mock("@/entities/branches/ui/queries/get-branches.query");
1616
const defaultBranch = generateBranch({ id: "branch-default", name: "primary", is_default: true });
1717
const featureBranch = generateBranch({ id: "branch-feature", name: "feature-1" });
1818

19-
type BranchesQueryState = {
20-
data?: Array<BranchListItem>;
21-
isPending: boolean;
22-
error: Error | null;
23-
};
24-
25-
const mockBranchesQuery = (state: BranchesQueryState) =>
19+
const mockBranchesQuery = (state: Partial<ReturnType<typeof useGetBranches>>) =>
2620
vi.mocked(useGetBranches).mockReturnValue(state as ReturnType<typeof useGetBranches>);
2721

2822
const mockFetchedBranches = () =>
@@ -85,6 +79,23 @@ describe("BranchesProvider", () => {
8579
await expect.element(component.getByText("Current branch: feature-1")).toBeVisible();
8680
});
8781

82+
test("resolves the default branch when the URL names it explicitly", async () => {
83+
// GIVEN
84+
mockFetchedBranches();
85+
seedBranchInUrl(defaultBranch.name);
86+
87+
// WHEN
88+
const component = await render(
89+
<BranchesProvider>
90+
<BranchProbe />
91+
</BranchesProvider>
92+
);
93+
94+
// THEN
95+
await expect.element(component.getByText("Current branch: primary")).toBeVisible();
96+
expect(getBranchInUrl()).toBe("primary");
97+
});
98+
8899
test("hides its children while the branches are being fetched", async () => {
89100
// GIVEN
90101
mockBranchesQuery({ isPending: true, error: null });
@@ -101,6 +112,27 @@ describe("BranchesProvider", () => {
101112
expect(component.getByText(/Current branch/).query()).toBeNull();
102113
});
103114

115+
test("keeps its children mounted while the branches are refetched in the background", async () => {
116+
// GIVEN
117+
mockBranchesQuery({
118+
data: [defaultBranch, featureBranch],
119+
isPending: false,
120+
isFetching: true,
121+
error: null,
122+
});
123+
124+
// WHEN
125+
const component = await render(
126+
<BranchesProvider>
127+
<BranchProbe />
128+
</BranchesProvider>
129+
);
130+
131+
// THEN
132+
await expect.element(component.getByText("Current branch: primary")).toBeVisible();
133+
expect(component.getByText("Loading branches...").query()).toBeNull();
134+
});
135+
104136
test("shows an error screen when the branches cannot be fetched", async () => {
105137
// GIVEN
106138
mockBranchesQuery({ isPending: false, error: new Error("Branches are unreachable") });

0 commit comments

Comments
 (0)