diff --git a/ui/src/pages/Graphs/NewGraph/NewGraph.tsx b/ui/src/pages/Graphs/NewGraph/NewGraph.tsx index 11e5e8ac9..664368d66 100644 --- a/ui/src/pages/Graphs/NewGraph/NewGraph.tsx +++ b/ui/src/pages/Graphs/NewGraph/NewGraph.tsx @@ -154,16 +154,10 @@ const NewGraph: React.FC = () => { * Function that makes an API call for Edit graph */ const editGraph = useCallback(async () => { - const gfc: GraphFlowInfo = { ...graphFlowInfo }; - if (gfc.flow_type === FlowType.DUPLICATE) { - //Update the name if flow is Duplicate - gfc.name = name; - setGraphFlowInfo(gfc); - } setLoading(true); try { const { graphId } = await createGraph( - gfc, + graphFlowInfo, groups, handleError, true @@ -172,7 +166,7 @@ const NewGraph: React.FC = () => { setLoading(false); if (isValid(graphId)) { - navigate(projectPaths.Graphs + gfc.name); + navigate(projectPaths.Graphs + graphFlowInfo.name); } } } catch (error) { @@ -181,7 +175,6 @@ const NewGraph: React.FC = () => { } }, [ graphFlowInfo, - name, createGraph, groups, handleError, @@ -304,10 +297,10 @@ const NewGraph: React.FC = () => { return; } if (isObjectNotEmpty(graphFlowInfo) && groups) { - if (graphFlowInfo.flow_type === FlowType.CREATE) { - onCreateGraph(); - } else { + if (graphFlowInfo.flow_type === FlowType.EDIT) { validateAndEditGraph(); + } else { + onCreateGraph(); } } }, [graphFlowInfo, groups, name, onCreateGraph, validateAndEditGraph]); diff --git a/ui/src/pages/Graphs/__tests__/DuplicateGraph.test.tsx b/ui/src/pages/Graphs/__tests__/DuplicateGraph.test.tsx index ad8dbbfc0..3d9e06899 100644 --- a/ui/src/pages/Graphs/__tests__/DuplicateGraph.test.tsx +++ b/ui/src/pages/Graphs/__tests__/DuplicateGraph.test.tsx @@ -101,6 +101,7 @@ describe("DuplicateGraphPage", () => { validateWithFailure?: boolean; validateWithInvalid?: boolean; validateWithInvalidSuccess?: boolean; + createWithFailure?: boolean; validName?: string; } ) => { @@ -108,12 +109,16 @@ describe("DuplicateGraphPage", () => { const saveGraph = screen.getByText("Save"); await userEvent.click(saveGraph); }; + vi.mocked(mockAxiosInstance.post).mockClear(); + mockUseNavigate.mockClear(); await executeGraphCreationFlow(args, clickSubmitButton, { validateWithFailure: options.validateWithFailure, validateWithInvalid: options.validateWithInvalid, + validateWithInvalidSuccess: options.validateWithInvalidSuccess, + createWithFailure: options.createWithFailure, validName: "ltv_graph_copy", - forceRename: true + forceRename: false }); }; @@ -142,6 +147,52 @@ describe("DuplicateGraphPage", () => { }); //Previous page }); + it("Should send an unchanged duplicate through create without force_rename and stay on the page when creation fails", async () => { + const graphInfo = duplicateGraphInfo(); + const original = expectedGraphDetailResponse().graph; + mockLocationStateValue({ + flow_type: FlowType.DUPLICATE, + name: graphInfo?.name, + jsGraph: graphInfo + }); + await renderDuplicateGraphPage(); + await pageLoadTest(); + await waitFor(() => { + expect(screen.getByPlaceholderText("name")).toHaveValue( + "ltv_graph_copy" + ); + }); + + await findAndClickButtonByRole("Next"); + await executeGraphCreationFlow( + { + col_groups: original?.col_groups?.map(({ columns }) => ({ + columns + })), + tables: original?.tables + } as GraphDefinition, + async () => { + await userEvent.click(screen.getByText("Save")); + }, + { + createWithInvalid: true, + validName: "ltv_graph_copy", + forceRename: false + } + ); + + expect(mockAxiosInstance.post).not.toHaveBeenCalledWith( + "v1/graphs/", + expect.anything(), + expect.objectContaining({ + params: expect.objectContaining({ force_rename: true }) + }) + ); + expect(mockUseNavigate).not.toHaveBeenCalledWith( + `${getProjectPath("project-tests").Graphs}ltv_graph_copy` + ); + }); + it("Should successfully duplicate the graph and handle validation errors (failure, invalid data)", async () => { const graphInfo = duplicateGraphInfo(); mockLocationStateValue({ @@ -193,6 +244,12 @@ describe("DuplicateGraphPage", () => { validateWithInvalidSuccess: true } ); + await duplicateGraphApiCallTest( + mockNewDeuplicateGraphResponse as GraphDefinition, + { + createWithFailure: true + } + ); await duplicateGraphApiCallTest( mockNewDeuplicateGraphResponse as GraphDefinition, {}