Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Preserve passed size in SDK question loading and error states (#78906)

Sébastiene1fb469d5773
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
index e1b9ed30324..6e1a42d75b5 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
@@ -157,19 +157,32 @@ export const SdkQuestionDefaultView = ({
const { ref: containerRef, isMobile } = useMobileLayout();
+ // EMB-2177: the loader and the error states have to sit in the same box as
+ // the rendered state, otherwise the component collapses and then jumps to
+ // the caller's height once the question resolves.
+ const sizeProps = { height, width, className, style };
+
if (
!isEditorOpen &&
(isLocaleLoading || isQuestionLoading || isQueryResultLoading)
) {
- return <SdkLoader />;
+ return (
+ <FlexibleSizeComponent {...sizeProps}>
+ <SdkLoader />
+ </FlexibleSizeComponent>
+ );
}
if (!isEditorOpen && !question) {
- if (originalId) {
- return <QuestionNotFoundError id={originalId} />;
- } else {
- return <SdkError message={t`Question not found`} />;
- }
+ return (
+ <FlexibleSizeComponent {...sizeProps}>
+ {originalId ? (
+ <QuestionNotFoundError id={originalId} />
+ ) : (
+ <SdkError message={t`Question not found`} />
+ )}
+ </FlexibleSizeComponent>
+ );
}
const showSaveButton =
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.unit.spec.tsx
new file mode 100644
index 00000000000..59f4d7322ef
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.unit.spec.tsx
@@ -0,0 +1,96 @@
+import { screen } from "__support__/ui";
+import { renderWithSDKProviders } from "embedding-sdk-bundle/test/__support__/ui";
+import { createMockSdkConfig } from "embedding-sdk-bundle/test/mocks/config";
+import { setupSdkState } from "embedding-sdk-bundle/test/server-mocks/sdk-init";
+
+import { useSdkQuestionContext } from "../SdkQuestion/context";
+
+import type { SdkQuestionDefaultViewProps } from "./SdkQuestionDefaultView";
+import { SdkQuestionDefaultView } from "./SdkQuestionDefaultView";
+
+jest.mock("../SdkQuestion/context", () => ({
+ ...jest.requireActual("../SdkQuestion/context"),
+ useSdkQuestionContext: jest.fn(),
+}));
+
+// `jest.mock` above swapped the real hook for a mock, which the import is not
+// typed as.
+const mockUseSdkQuestionContext = useSdkQuestionContext as jest.MockedFunction<
+ typeof useSdkQuestionContext
+>;
+
+// A viewport unit survives Mantine's size handling as-is, whereas pixel values
+// are rewritten to a `calc(... * var(--mantine-scale))` expression.
+const HEIGHT = "50vh";
+
+type QuestionContext = Partial<ReturnType<typeof useSdkQuestionContext>>;
+
+const LOADING_CONTEXT: QuestionContext = { isQuestionLoading: true };
+const NOT_FOUND_CONTEXT: QuestionContext = {
+ isQuestionLoading: false,
+ question: undefined,
+};
+
+const setup = (
+ props: SdkQuestionDefaultViewProps = {},
+ context: QuestionContext = LOADING_CONTEXT,
+) => {
+ mockUseSdkQuestionContext.mockReturnValue(
+ // Each of these states returns before the component reads the rest of the
+ // context, so a partial one is all it ever gets to see.
+ context as ReturnType<typeof useSdkQuestionContext>,
+ );
+
+ const { state } = setupSdkState();
+
+ renderWithSDKProviders(<SdkQuestionDefaultView {...props} />, {
+ componentProviderProps: { authConfig: createMockSdkConfig() },
+ storeInitialState: state,
+ });
+};
+
+describe("SdkQuestionDefaultView", () => {
+ describe("loading state", () => {
+ it("should render the loader inside a box that keeps the passed height", () => {
+ setup({ height: HEIGHT });
+
+ // The sizing box is a plain Box with no accessible role, so the height it
+ // is meant to hold is the only thing to query it by.
+ const sizedBox = screen
+ .getByTestId("loading-indicator")
+ .closest(`[style*="height: ${HEIGHT}"]`);
+
+ expect(sizedBox).toBeInTheDocument();
+ });
+
+ it("should apply the passed className and style to the loading box", () => {
+ setup({ className: "custom-class", style: { padding: "10px" } });
+
+ const styledBox = screen
+ .getByTestId("loading-indicator")
+ .closest(".custom-class");
+
+ expect(styledBox).toHaveStyle({ padding: "10px" });
+ });
+ });
+
+ describe("error state", () => {
+ it.each([
+ { name: "question not found error", originalId: 42 },
+ { name: "generic not found error", originalId: undefined },
+ ])(
+ "should render the $name inside a box that keeps the passed height",
+ async ({ originalId }) => {
+ setup({ height: HEIGHT }, { ...NOT_FOUND_CONTEXT, originalId });
+
+ // The locale resolves asynchronously, and until it does the component
+ // shows the loader rather than the error.
+ const sizedBox = (
+ await screen.findByTestId("sdk-error-container")
+ ).closest(`[style*="height: ${HEIGHT}"]`);
+
+ expect(sizedBox).toBeInTheDocument();
+ },
+ );
+ });
+});