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

Fix SDK interactive question showing no question error incorrectly #42400

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export const _InteractiveQuestion = ({
const hasQuestionChanges =
card && (!card.id || card.id !== card.original_card_id);

const [loading, setLoading] = useState(true);
const [isQuestionLoading, setIsQuestionLoading] = useState(true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming to make is obvious this means it's loading a question not running a question query


const { isRunning } = uiControls;
const { isRunning: isQueryRunning } = uiControls;

if (question) {
// FIXME: remove "You can also get an alert when there are some results." feature for question
Expand All @@ -78,14 +78,14 @@ export const _InteractiveQuestion = ({
dispatch: ReturnType<typeof useDispatch>,
questionId: CardId,
) => {
setLoading(true);
setIsQuestionLoading(true);

const { location, params } = getQuestionParameters(questionId);
try {
await dispatch(initializeQBRaw(location, params));
} catch (e) {
console.error(`Failed to get question`, e);
setLoading(false);
setIsQuestionLoading(false);
}
};

Expand All @@ -99,11 +99,11 @@ export const _InteractiveQuestion = ({

useEffect(() => {
if (queryResults) {
setLoading(false);
setIsQuestionLoading(false);
}
}, [queryResults]);

if (loading) {
if (isQuestionLoading || isQueryRunning) {
return <Loader data-testid="loading-spinner" />;
}

Expand Down Expand Up @@ -143,7 +143,7 @@ export const _InteractiveQuestion = ({
className={cx(CS.flexFull, CS.fullWidth, CS.fullHeight)}
question={question}
rawSeries={[{ card, data: result && result.data }]}
isRunning={isRunning}
isRunning={isQueryRunning}
isObjectDetail={false}
isResultDirty={false}
isNativeEditorOpen={false}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AnyAction, ThunkDispatch } from "@reduxjs/toolkit";
import { within } from "@testing-library/react";

import {
Expand All @@ -15,6 +16,10 @@ import {
} from "__support__/ui";
import { createMockConfig } from "embedding-sdk/test/mocks/config";
import { setupSdkState } from "embedding-sdk/test/server-mocks/sdk-init";
import {
clearQueryResult,
runQuestionQuery,
} from "metabase/query_builder/actions";
import {
createMockCard,
createMockColumn,
Expand All @@ -24,6 +29,7 @@ import {
createMockTable,
createMockUser,
} from "metabase-types/api/mocks";
import type { State } from "metabase-types/store";

import { InteractiveQuestion } from "./InteractiveQuestion";

Expand Down Expand Up @@ -67,13 +73,16 @@ const setup = ({

setupCardQueryEndpoints(TEST_CARD, TEST_DATASET);

renderWithProviders(<InteractiveQuestion questionId={TEST_CARD.id} />, {
mode: "sdk",
sdkConfig: createMockConfig({
jwtProviderUri: "http://TEST_URI/sso/metabase",
}),
storeInitialState: state,
});
return renderWithProviders(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return the result from renderWithProvider so I could access store and use it to dispatch actions.

<InteractiveQuestion questionId={TEST_CARD.id} />,
{
mode: "sdk",
sdkConfig: createMockConfig({
jwtProviderUri: "http://TEST_URI/sso/metabase",
}),
storeInitialState: state,
},
);
};

describe("InteractiveQuestion", () => {
Expand All @@ -98,6 +107,37 @@ describe("InteractiveQuestion", () => {
).toBeInTheDocument();
});

it("should render loading state when drilling down", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is tested with the previous version and it failed as expected.

const { store } = setup();

await waitForLoaderToBeRemoved();

expect(
within(screen.getByTestId("TableInteractive-root")).getByText(
TEST_COLUMN.display_name,
),
).toBeInTheDocument();
expect(
within(screen.getByRole("gridcell")).getByText("Test Row"),
).toBeInTheDocument();

expect(screen.queryByTestId("loading-spinner")).not.toBeInTheDocument();
// Mimicking drilling down by rerunning the query again
const storeDispatch = store.dispatch as unknown as ThunkDispatch<
State,
void,
AnyAction
>;
Comment on lines +126 to +130
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't manage to find a way to use this without casting, let me know if there's any better way.

storeDispatch(clearQueryResult());
storeDispatch(runQuestionQuery());
Comment on lines +131 to +132
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 actions do:

  1. clear the results so it's as if we're about to query a new question when we're drilling
  2. query again


expect(screen.queryByText("Question not found")).not.toBeInTheDocument();
expect(screen.getByTestId("loading-spinner")).toBeInTheDocument();
expect(
within(await screen.findByRole("gridcell")).getByText("Test Row"),
).toBeInTheDocument();
});

it("should not render an error if a question isn't found before the question loaded", async () => {
setup();

Expand Down