Dashboards: Fix 'Copy' from being appended to new dashboard titles (#41344)

pull/41352/head
Josh Hunt 4 years ago committed by GitHub
parent 725c113691
commit cbc00babe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      public/app/features/dashboard/components/SaveDashboard/forms/SaveDashboardAsForm.test.tsx
  2. 9
      public/app/features/dashboard/components/SaveDashboard/forms/SaveDashboardAsForm.tsx

@ -1,6 +1,6 @@
import React from 'react';
import { mount } from 'enzyme';
import { SaveDashboardAsForm } from './SaveDashboardAsForm';
import { SaveDashboardAsForm, SaveDashboardAsFormProps } from './SaveDashboardAsForm';
import { DashboardModel } from 'app/features/dashboard/state';
import { act } from 'react-dom/test-utils';
import * as api from 'app/features/manage-dashboards/state/actions';
@ -26,7 +26,11 @@ const prepareDashboardMock = (panel: any) => {
getSaveModelClone: () => json,
};
};
const renderAndSubmitForm = async (dashboard: any, submitSpy: any) => {
const renderAndSubmitForm = async (
dashboard: unknown,
submitSpy: jest.Mock,
otherProps: Partial<SaveDashboardAsFormProps> = {}
) => {
const container = mount(
<SaveDashboardAsForm
dashboard={dashboard as DashboardModel}
@ -36,6 +40,7 @@ const renderAndSubmitForm = async (dashboard: any, submitSpy: any) => {
submitSpy(jsonModel);
return {};
}}
{...otherProps}
/>
);
@ -51,15 +56,30 @@ describe('SaveDashboardAsForm', () => {
jest.spyOn(api, 'searchFolders').mockResolvedValue([]);
const spy = jest.fn();
await renderAndSubmitForm(prepareDashboardMock({}), spy);
await renderAndSubmitForm(prepareDashboardMock({}), spy, {
isNew: true,
});
expect(spy).toBeCalledTimes(1);
const savedDashboardModel = spy.mock.calls[0][0];
expect(savedDashboardModel.id).toBe(null);
expect(savedDashboardModel.title).toBe('name Copy');
expect(savedDashboardModel.title).toBe('name');
expect(savedDashboardModel.editable).toBe(true);
expect(savedDashboardModel.hideControls).toBe(false);
});
it("appends 'Copy' to the name when the dashboard isnt new", async () => {
jest.spyOn(api, 'searchFolders').mockResolvedValue([]);
const spy = jest.fn();
await renderAndSubmitForm(prepareDashboardMock({}), spy, {
isNew: false,
});
expect(spy).toBeCalledTimes(1);
const savedDashboardModel = spy.mock.calls[0][0];
expect(savedDashboardModel.title).toBe('name Copy');
});
});
describe('graph panel', () => {

@ -34,14 +34,19 @@ const getSaveAsDashboardClone = (dashboard: DashboardModel) => {
return clone;
};
export const SaveDashboardAsForm: React.FC<SaveDashboardFormProps & { isNew?: boolean }> = ({
export interface SaveDashboardAsFormProps extends SaveDashboardFormProps {
isNew?: boolean;
}
export const SaveDashboardAsForm: React.FC<SaveDashboardAsFormProps> = ({
dashboard,
isNew,
onSubmit,
onCancel,
onSuccess,
}) => {
const defaultValues: SaveDashboardAsFormDTO = {
title: `${dashboard.title} Copy`,
title: isNew ? dashboard.title : `${dashboard.title} Copy`,
$folder: {
id: dashboard.meta.folderId,
title: dashboard.meta.folderTitle,

Loading…
Cancel
Save