diff --git a/packages/react-sdk/src/index.tsx b/packages/react-sdk/src/index.tsx index d833c090..645f7df5 100644 --- a/packages/react-sdk/src/index.tsx +++ b/packages/react-sdk/src/index.tsx @@ -202,6 +202,8 @@ export function BucketProvider({ void clientRef.current.stop(); } + setFeaturesLoading(true); + const client = newBucketClient({ ...config, user, diff --git a/packages/react-sdk/test/usage.test.tsx b/packages/react-sdk/test/usage.test.tsx index 3bf999f2..85e4d709 100644 --- a/packages/react-sdk/test/usage.test.tsx +++ b/packages/react-sdk/test/usage.test.tsx @@ -231,6 +231,54 @@ describe("", () => { expect(initialize).toHaveBeenCalledOnce(); expect(BucketClient.prototype.stop).not.toHaveBeenCalledOnce(); }); + + test("resets loading state when context changes", async () => { + const { queryByTestId, rerender } = render( + getProvider({ + loadingComponent: Loading..., + }), + ); + + // Loading component should be visible initially + expect(queryByTestId("loading")).not.toBeNull(); + + // Wait for initial loading to complete + await waitFor(() => { + expect(queryByTestId("loading")).toBeNull(); + }); + + // Change user context + rerender( + getProvider({ + loadingComponent: Loading..., + user: { ...user, id: "new-user-id" }, + }), + ); + + // Loading should appear again + expect(queryByTestId("loading")).not.toBeNull(); + + // Wait for loading to complete again + await waitFor(() => { + expect(queryByTestId("loading")).toBeNull(); + }); + + // Change company context + rerender( + getProvider({ + loadingComponent: Loading..., + company: { ...company, id: "new-company-id" }, + }), + ); + + // Loading should appear again + expect(queryByTestId("loading")).not.toBeNull(); + + // Wait for loading to complete again + await waitFor(() => { + expect(queryByTestId("loading")).toBeNull(); + }); + }); }); describe("useFeature", () => {