Skip to content

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

Keep question names stable while saving duplicates from the query builder (#85248)

Anderson44f8378caf9b
diff --git a/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx b/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx
index 0afafc311af..b21f8ee0c9f 100644
--- a/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx
+++ b/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.tsx
@@ -1,4 +1,5 @@
-import { useCallback } from "react";
+import { useCallback, useMemo } from "react";
+import { useLatest } from "react-use";
import { t } from "ttag";
import _ from "underscore";
@@ -32,7 +33,7 @@ import { NewDatasetModal } from "../NewDatasetModal";
import { PreviewQueryModal } from "../view/PreviewQueryModal";
interface QueryModalsProps {
- modal: QueryModalType;
+ modal: QueryModalType | null;
question: Question;
setQueryBuilderMode: (mode: QueryBuilderMode) => void;
originalQuestion: Question;
@@ -69,6 +70,11 @@ export function QueryModals({
const initialCollectionId = useGetDefaultCollectionId();
const underlyingQuestion = useSelector(getQuestionWithoutComposing);
const visualizationSettings = useSelector(getVisualizationSettings);
+ const questionRef = useLatest(question);
+ const questionToCopy = useMemo(
+ () => (modal === MODAL_TYPES.CLONE ? questionRef.current : undefined),
+ [modal, questionRef],
+ );
const handleAlertCreatedOrUpdated = useCallback(() => {
dispatch(updateUrl(question, { dirty: false }));
@@ -244,13 +250,17 @@ export function QueryModals({
/>
);
case MODAL_TYPES.CLONE:
+ if (!questionToCopy) {
+ return null;
+ }
+
return (
<CopyModal
entityType="cards"
entityObject={{
- ...question.card(),
- collection_id: question.canWrite()
- ? question.collectionId()
+ ...questionToCopy.card(),
+ collection_id: questionToCopy.canWrite()
+ ? questionToCopy.collectionId()
: initialCollectionId,
}}
copy={async (formValues) => {
diff --git a/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.unit.spec.tsx b/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.unit.spec.tsx
new file mode 100644
index 00000000000..2f345d1e7f5
--- /dev/null
+++ b/frontend/src/metabase/query_builder/components/QueryModals/QueryModals.unit.spec.tsx
@@ -0,0 +1,215 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+import type { ComponentProps } from "react";
+
+import { createMockMetadataFromState } from "__support__/metadata";
+import {
+ setupCollectionsEndpoints,
+ setupGetCollectionEndpoint,
+} from "__support__/server-mocks";
+import {
+ createMockQueryBuilderState,
+ createMockState,
+} from "__support__/state";
+import { createMockEntitiesState } from "__support__/store";
+import { act, renderWithProviders, screen, waitFor } from "__support__/ui";
+import { MODAL_TYPES } from "metabase/querying/constants";
+import { defer } from "metabase/utils/promise";
+import Question from "metabase-lib/v1/Question";
+import type { Collection } from "metabase-types/api";
+import {
+ createMockCard,
+ createMockCollection,
+ createMockUser,
+} from "metabase-types/api/mocks";
+import { createSampleDatabase } from "metabase-types/api/mocks/presets";
+
+import { QueryModals } from "./QueryModals";
+
+function setup({
+ deferCollection = false,
+ deferRootCollection = false,
+ canWrite = true,
+} = {}) {
+ const collection = createMockCollection({ id: 1, name: "Reports" });
+ const personalCollection = createMockCollection({ id: 2, name: "Personal" });
+ const rootCollection = createMockCollection({
+ id: "root",
+ can_write: deferRootCollection,
+ });
+ const card = createMockCard({
+ name: "Orders",
+ description: "Original description",
+ collection: canWrite ? collection : null,
+ collection_id: collection.id,
+ can_write: canWrite,
+ });
+ const createRequest = defer<Question>();
+ const collectionRequest = defer<Collection>();
+ const rootCollectionRequest = defer<Collection>();
+ const onCreate = jest.fn((_question: Question) => createRequest.promise);
+ const onCloseModal = jest.fn();
+ const state = createMockState({
+ currentUser: createMockUser({ personal_collection_id: 2 }),
+ qb: createMockQueryBuilderState({ card }),
+ entities: createMockEntitiesState({
+ databases: [createSampleDatabase()],
+ questions: [card],
+ }),
+ });
+ const metadata = createMockMetadataFromState(state);
+ const question = new Question(card, metadata);
+
+ setupCollectionsEndpoints({
+ collections: [collection, personalCollection],
+ rootCollection,
+ });
+ setupGetCollectionEndpoint(collection);
+ setupGetCollectionEndpoint(personalCollection);
+ if (deferCollection) {
+ fetchMock.modifyRoute(`get-collection-${collection.id}`, {
+ response: () => collectionRequest.promise,
+ });
+ }
+ if (deferRootCollection) {
+ fetchMock.modifyRoute("collection-root", {
+ response: () => rootCollectionRequest.promise,
+ });
+ }
+
+ const props: ComponentProps<typeof QueryModals> = {
+ modal: MODAL_TYPES.CLONE,
+ question,
+ originalQuestion: question,
+ card,
+ onCreate,
+ onSave: jest.fn(),
+ onCloseModal,
+ onOpenModal: jest.fn(),
+ onChangeLocation: jest.fn(),
+ setQueryBuilderMode: jest.fn(),
+ };
+ const { rerender } = renderWithProviders(<QueryModals {...props} />, {
+ storeInitialState: state,
+ });
+
+ return {
+ card,
+ collection,
+ collectionRequest,
+ createRequest,
+ metadata,
+ onCreate,
+ onCloseModal,
+ rootCollection,
+ rootCollectionRequest,
+ rerender: (updates: Partial<ComponentProps<typeof QueryModals>>) =>
+ rerender(<QueryModals {...props} {...updates} />),
+ };
+}
+
+describe("QueryModals", () => {
+ it("should use the current question when the duplicate dialog is reopened", async () => {
+ const { card, metadata, rerender } = setup();
+
+ await screen.findByText("Reports");
+ expect(screen.getByLabelText("Name")).toHaveValue("Orders - Duplicate");
+
+ const updatedCard = createMockCard({
+ ...card,
+ id: 2,
+ name: "Monthly orders",
+ });
+ const updatedQuestion = new Question(updatedCard, metadata);
+ rerender({ modal: null, card: updatedCard, question: updatedQuestion });
+
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+
+ rerender({
+ modal: MODAL_TYPES.CLONE,
+ card: updatedCard,
+ question: updatedQuestion,
+ });
+
+ expect(screen.getByLabelText("Name")).toHaveValue(
+ "Monthly orders - Duplicate",
+ );
+ expect(
+ screen.getByRole("dialog", { name: 'Duplicate "Monthly orders"' }),
+ ).toBeInTheDocument();
+ });
+
+ it("should update the default collection when it loads", async () => {
+ const { collection, collectionRequest } = setup({ deferCollection: true });
+
+ expect(await screen.findByText("Personal")).toBeInTheDocument();
+
+ await act(async () => collectionRequest.resolve(collection));
+
+ expect(await screen.findByText("Reports")).toBeInTheDocument();
+ });
+
+ it("should update the default destination for a read-only question when permissions load", async () => {
+ const { rootCollection, rootCollectionRequest } = setup({
+ canWrite: false,
+ deferRootCollection: true,
+ });
+
+ expect(await screen.findByText("Personal")).toBeInTheDocument();
+
+ await act(async () => rootCollectionRequest.resolve(rootCollection));
+
+ expect(await screen.findByText("Our analytics")).toBeInTheDocument();
+ });
+
+ it.each(["Monthly orders", "Orders - Duplicate"])(
+ "should preserve the name %s while saving a duplicate (#83252)",
+ async (name) => {
+ const {
+ card,
+ createRequest,
+ metadata,
+ onCreate,
+ onCloseModal,
+ rerender,
+ } = setup();
+
+ await screen.findByText("Reports");
+ await userEvent.clear(screen.getByLabelText("Name"));
+ await userEvent.type(screen.getByLabelText("Name"), name);
+ await userEvent.clear(screen.getByLabelText("Description"));
+ await userEvent.type(
+ screen.getByLabelText("Description"),
+ "Copied description",
+ );
+ await userEvent.click(screen.getByRole("button", { name: "Duplicate" }));
+
+ await waitFor(() => expect(onCreate).toHaveBeenCalledTimes(1));
+ const [submittedQuestion] = onCreate.mock.calls[0];
+ expect(submittedQuestion.displayName()).toBe(name);
+ expect(submittedQuestion.description()).toBe("Copied description");
+
+ const savedCard = createMockCard({
+ ...card,
+ id: 2,
+ name,
+ description: "Copied description",
+ });
+ const savedQuestion = new Question(savedCard, metadata);
+ rerender({ question: savedQuestion, card: savedCard });
+
+ expect(screen.getByLabelText("Name")).toHaveValue(name);
+ expect(screen.getByLabelText("Description")).toHaveValue(
+ "Copied description",
+ );
+ expect(
+ screen.getByRole("dialog", { name: 'Duplicate "Orders"' }),
+ ).toBeInTheDocument();
+ expect(onCloseModal).not.toHaveBeenCalled();
+
+ await act(async () => createRequest.resolve(savedQuestion));
+
+ expect(onCloseModal).toHaveBeenCalled();
+ },
+ );
+});