Newer
Older
Oisin Coveney
committed
import { within } from "@testing-library/react";
import {
setupAlertsEndpoints,
setupCardEndpoints,
setupCardQueryEndpoints,
setupCardQueryMetadataEndpoint,
Oisin Coveney
committed
setupDatabaseEndpoints,
setupTableEndpoints,
setupUnauthorizedCardEndpoints,
} from "__support__/server-mocks";
import {
Oisin Coveney
committed
renderWithProviders,
screen,
waitForLoaderToBeRemoved,
} from "__support__/ui";
Phoomparin Mano
committed
import { InteractiveQuestionResult } from "embedding-sdk/components/private/InteractiveQuestionResult";
import { createMockAuthProviderUriConfig } from "embedding-sdk/test/mocks/config";
Oisin Coveney
committed
import { setupSdkState } from "embedding-sdk/test/server-mocks/sdk-init";
import {
createMockCard,
Oisin Coveney
committed
createMockColumn,
createMockDatabase,
createMockDataset,
createMockDatasetData,
createMockTable,
createMockUser,
} from "metabase-types/api/mocks";
import { useInteractiveQuestionContext } from "../../private/InteractiveQuestion/context";
import { InteractiveQuestion } from "./InteractiveQuestion";
Oisin Coveney
committed
const TEST_USER = createMockUser();
const TEST_DB_ID = 1;
const TEST_DB = createMockDatabase({ id: TEST_DB_ID });
const TEST_TABLE_ID = 1;
const TEST_TABLE = createMockTable({ id: TEST_TABLE_ID, db_id: TEST_DB_ID });
const TEST_COLUMN = createMockColumn({
display_name: "Test Column",
name: "Test Column",
});
Phoomparin Mano
committed
Oisin Coveney
committed
const TEST_DATASET = createMockDataset({
data: createMockDatasetData({
cols: [TEST_COLUMN],
rows: [["Test Row"]],
}),
});
Phoomparin Mano
committed
// Provides a button to re-run the query
function InteractiveQuestionTestResult() {
const { resetQuestion } = useInteractiveQuestionContext();
return (
<div>
<button onClick={resetQuestion}>Run Query</button>
<InteractiveQuestionResult withTitle />
</div>
);
}
Oisin Coveney
committed
const setup = ({
isValidCard = true,
}: {
isValidCard?: boolean;
} = {}) => {
const { state } = setupSdkState({
currentUser: TEST_USER,
});
const TEST_CARD = createMockCard();
if (isValidCard) {
setupCardEndpoints(TEST_CARD);
setupCardQueryMetadataEndpoint(
TEST_CARD,
createMockCardQueryMetadata({
databases: [TEST_DB],
}),
);
Oisin Coveney
committed
} else {
setupUnauthorizedCardEndpoints(TEST_CARD);
}
setupAlertsEndpoints(TEST_CARD, []);
setupDatabaseEndpoints(TEST_DB);
setupTableEndpoints(TEST_TABLE);
setupCardQueryEndpoints(TEST_CARD, TEST_DATASET);
Mahatthana (Kelvin) Nomsawadi
committed
return renderWithProviders(
Phoomparin Mano
committed
<InteractiveQuestion questionId={TEST_CARD.id}>
<InteractiveQuestionTestResult />
</InteractiveQuestion>,
Mahatthana (Kelvin) Nomsawadi
committed
{
mode: "sdk",
sdkProviderProps: {
config: createMockAuthProviderUriConfig({
authProviderUri: "http://TEST_URI/sso/metabase",
Mahatthana (Kelvin) Nomsawadi
committed
storeInitialState: state,
},
);
Oisin Coveney
committed
};
describe("InteractiveQuestion", () => {
it("should initially render with a loader", async () => {
setup();
expect(screen.getByTestId("loading-indicator")).toBeInTheDocument();
Oisin Coveney
committed
});
Phoomparin Mano
committed
it("should render loading state when rerunning the query", async () => {
setup();
Mahatthana (Kelvin) Nomsawadi
committed
await waitForLoaderToBeRemoved();
expect(
await within(screen.getByTestId("TableInteractive-root")).findByText(
Mahatthana (Kelvin) Nomsawadi
committed
TEST_COLUMN.display_name,
),
).toBeInTheDocument();
expect(
await within(screen.getByRole("gridcell")).findByText("Test Row"),
Mahatthana (Kelvin) Nomsawadi
committed
).toBeInTheDocument();
expect(screen.queryByTestId("loading-indicator")).not.toBeInTheDocument();
Phoomparin Mano
committed
// Simulate drilling down by re-running the query again
act(() => screen.getByText("Run Query").click());
Mahatthana (Kelvin) Nomsawadi
committed
expect(screen.queryByText("Question not found")).not.toBeInTheDocument();
expect(screen.getByTestId("loading-indicator")).toBeInTheDocument();
Mahatthana (Kelvin) Nomsawadi
committed
expect(
within(await screen.findByRole("gridcell")).getByText("Test Row"),
).toBeInTheDocument();
});
it("should render when question is valid", async () => {
setup();
await waitForLoaderToBeRemoved();
expect(
within(screen.getByTestId("TableInteractive-root")).getByText(
TEST_COLUMN.display_name,
),
).toBeInTheDocument();
expect(
within(screen.getByRole("gridcell")).getByText("Test Row"),
).toBeInTheDocument();
});
Mahatthana (Kelvin) Nomsawadi
committed
it("should not render an error if a question isn't found before the question loaded", async () => {
setup();
await waitForLoaderToBeRemoved();
expect(screen.queryByText("Error")).not.toBeInTheDocument();
expect(screen.queryByText("Question not found")).not.toBeInTheDocument();
});
Oisin Coveney
committed
it("should render an error if a question isn't found", async () => {
setup({ isValidCard: false });
await waitForLoaderToBeRemoved();
expect(screen.getByText("Question not found")).toBeInTheDocument();
});
});