mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Dashboards: Fix 'Copy' from being appended to new dashboard titles (#41344)
This commit is contained in:
parent
725c113691
commit
cbc00babe4
@ -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…
Reference in New Issue
Block a user