refactor(frontend): Auth (#8308)

This commit is contained in:
sp.wack
2025-05-07 08:20:23 +04:00
committed by GitHub
parent ac0dab41dd
commit d3f6508e32
39 changed files with 302 additions and 390 deletions

View File

@@ -1,16 +1,16 @@
import { render, screen, waitFor, within } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import userEvent from "@testing-library/user-event";
import { QueryClientProvider, QueryClient } from "@tanstack/react-query";
import { setupStore } from "test-utils";
import { Provider } from "react-redux";
import { createRoutesStub, Outlet } from "react-router";
import OpenHands from "#/api/open-hands";
import { AuthProvider } from "#/context/auth-context";
import { GitRepository } from "#/types/git";
import { RepoConnector } from "#/components/features/home/repo-connector";
import { MOCK_DEFAULT_USER_SETTINGS } from "#/mocks/handlers";
const renderRepoConnector = (initialProvidersAreSet = true) => {
const renderRepoConnector = () => {
const mockRepoSelection = vi.fn();
const RouterStub = createRoutesStub([
{
@@ -40,11 +40,9 @@ const renderRepoConnector = (initialProvidersAreSet = true) => {
return render(<RouterStub />, {
wrapper: ({ children }) => (
<Provider store={setupStore()}>
<AuthProvider initialProvidersAreSet={initialProvidersAreSet}>
<QueryClientProvider client={new QueryClient()}>
{children}
</QueryClientProvider>
</AuthProvider>
<QueryClientProvider client={new QueryClient()}>
{children}
</QueryClientProvider>
</Provider>
),
});
@@ -65,6 +63,17 @@ const MOCK_RESPOSITORIES: GitRepository[] = [
},
];
beforeEach(() => {
const getSettingsSpy = vi.spyOn(OpenHands, "getSettings");
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {
github: "some-token",
gitlab: null,
},
});
});
describe("RepoConnector", () => {
it("should render the repository connector section", () => {
renderRepoConnector();
@@ -99,7 +108,7 @@ describe("RepoConnector", () => {
renderRepoConnector();
const launchButton = screen.getByTestId("repo-launch-button");
const launchButton = await screen.findByTestId("repo-launch-button");
expect(launchButton).toBeDisabled();
// Wait for the loading state to be replaced with the dropdown
@@ -147,7 +156,7 @@ describe("RepoConnector", () => {
const repoConnector = screen.getByTestId("repo-connector");
const launchButton =
within(repoConnector).getByTestId("repo-launch-button");
await within(repoConnector).findByTestId("repo-launch-button");
await userEvent.click(launchButton);
// repo not selected yet
@@ -184,7 +193,7 @@ describe("RepoConnector", () => {
renderRepoConnector();
const launchButton = screen.getByTestId("repo-launch-button");
const launchButton = await screen.findByTestId("repo-launch-button");
// Wait for the loading state to be replaced with the dropdown
const dropdown = await waitFor(() => screen.getByTestId("repo-dropdown"));
@@ -197,14 +206,22 @@ describe("RepoConnector", () => {
});
it("should not display a button to settings if the user is signed in with their git provider", async () => {
renderRepoConnector(true);
expect(
screen.queryByTestId("navigate-to-settings-button"),
).not.toBeInTheDocument();
renderRepoConnector();
await waitFor(() => {
expect(
screen.queryByTestId("navigate-to-settings-button"),
).not.toBeInTheDocument();
});
});
it("should display a button to settings if the user needs to sign in with their git provider", async () => {
renderRepoConnector(false);
const getSettingsSpy = vi.spyOn(OpenHands, "getSettings");
getSettingsSpy.mockResolvedValue({
...MOCK_DEFAULT_USER_SETTINGS,
provider_tokens_set: {},
});
renderRepoConnector();
const goToSettingsButton = await screen.findByTestId(
"navigate-to-settings-button",