Skip to content
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

chore(frontend): Fetch settings if user is authenticated #6247

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions frontend/__tests__/components/features/sidebar/sidebar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { screen, within } from "@testing-library/react";
import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "test-utils";
Expand All @@ -8,18 +8,17 @@ import { MULTI_CONVERSATION_UI } from "#/utils/feature-flags";
import OpenHands from "#/api/open-hands";
import { MOCK_USER_PREFERENCES } from "#/mocks/handlers";

const renderSidebar = () => {
describe("Sidebar", () => {
const RouterStub = createRoutesStub([
{
path: "/conversation/:conversationId",
Component: Sidebar,
},
]);

renderWithProviders(<RouterStub initialEntries={["/conversation/123"]} />);
};
const renderSidebar = () =>
renderWithProviders(<RouterStub initialEntries={["/conversation/123"]} />);

describe("Sidebar", () => {
it.skipIf(!MULTI_CONVERSATION_UI)(
"should have the conversation panel open by default",
() => {
Expand Down Expand Up @@ -54,9 +53,16 @@ describe("Sidebar", () => {
vi.clearAllMocks();
});

it("should fetch settings data on mount", () => {
renderSidebar();
expect(getSettingsSpy).toHaveBeenCalledOnce();
it("should fetch settings data on mount and if the user is authenticated", async () => {
const authenticateSpy = vi.spyOn(OpenHands, "authenticate");

const { rerender } = renderSidebar();
expect(getSettingsSpy).not.toHaveBeenCalled();

authenticateSpy.mockResolvedValueOnce(true);
rerender(<RouterStub initialEntries={["/conversation/123"]} />);

await waitFor(() => expect(getSettingsSpy).toHaveBeenCalledOnce());
});

it("should send all settings data when saving AI configuration", async () => {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/hooks/query/use-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import posthog from "posthog-js";
import { AxiosError } from "axios";
import { DEFAULT_SETTINGS, getLocalStorageSettings } from "#/services/settings";
import OpenHands from "#/api/open-hands";
import { useIsAuthed } from "./use-is-authed";

const getSettingsQueryFn = async () => {
try {
Expand Down Expand Up @@ -36,9 +37,12 @@ const getSettingsQueryFn = async () => {
};

export const useSettings = () => {
const { data: isAuthed } = useIsAuthed();

const query = useQuery({
queryKey: ["settings"],
queryFn: getSettingsQueryFn,
enabled: !!isAuthed,
});

React.useEffect(() => {
Expand Down
Loading