2021-06-02 05:24:19 -05:00
|
|
|
import { DashboardModel } from '../../state/DashboardModel';
|
|
|
|
import { PanelModel } from '../../state/PanelModel';
|
|
|
|
import { setContextSrv } from '../../../../core/services/context_srv';
|
|
|
|
import { hasChanges, ignoreChanges } from './DashboardPrompt';
|
2018-04-17 02:41:07 -05:00
|
|
|
|
2021-04-20 02:55:46 -05:00
|
|
|
function getDefaultDashboardModel(): DashboardModel {
|
|
|
|
return new DashboardModel({
|
|
|
|
refresh: false,
|
|
|
|
panels: [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
type: 'graph',
|
|
|
|
gridPos: { x: 0, y: 0, w: 24, h: 6 },
|
|
|
|
legend: { sortDesc: false },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
type: 'row',
|
|
|
|
gridPos: { x: 0, y: 6, w: 24, h: 2 },
|
|
|
|
collapsed: true,
|
|
|
|
panels: [
|
|
|
|
{ id: 3, type: 'graph', gridPos: { x: 0, y: 6, w: 12, h: 2 } },
|
|
|
|
{ id: 4, type: 'graph', gridPos: { x: 12, y: 6, w: 12, h: 2 } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{ id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 1, h: 1 } },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
2018-04-17 02:41:07 -05:00
|
|
|
|
2021-04-20 02:55:46 -05:00
|
|
|
function getTestContext() {
|
|
|
|
const contextSrv: any = { isSignedIn: true, isEditor: true };
|
|
|
|
setContextSrv(contextSrv);
|
|
|
|
const dash: any = getDefaultDashboardModel();
|
|
|
|
const original: any = dash.getSaveModelClone();
|
2018-04-17 02:41:07 -05:00
|
|
|
|
2021-06-02 05:24:19 -05:00
|
|
|
return { dash, original, contextSrv };
|
2021-04-20 02:55:46 -05:00
|
|
|
}
|
2018-04-17 02:41:07 -05:00
|
|
|
|
2021-06-02 05:24:19 -05:00
|
|
|
describe('DashboardPrompt', () => {
|
2018-04-17 02:41:07 -05:00
|
|
|
it('No changes should not have changes', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
|
|
|
expect(hasChanges(dash, original)).toBe(false);
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Simple change should be registered', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2018-04-17 02:41:07 -05:00
|
|
|
dash.title = 'google';
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(hasChanges(dash, original)).toBe(true);
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should ignore a lot of changes', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2018-04-17 02:41:07 -05:00
|
|
|
dash.time = { from: '1h' };
|
|
|
|
dash.refresh = true;
|
|
|
|
dash.schemaVersion = 10;
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(hasChanges(dash, original)).toBe(false);
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should ignore .iteration changes', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2018-04-17 02:41:07 -05:00
|
|
|
dash.iteration = new Date().getTime() + 1;
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(hasChanges(dash, original)).toBe(false);
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should ignore row collapse change', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2018-04-17 02:41:07 -05:00
|
|
|
dash.toggleRow(dash.panels[1]);
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(hasChanges(dash, original)).toBe(false);
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should ignore panel legend changes', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2018-04-17 02:41:07 -05:00
|
|
|
dash.panels[0].legend.sortDesc = true;
|
|
|
|
dash.panels[0].legend.sort = 'avg';
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(hasChanges(dash, original)).toBe(false);
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should ignore panel repeats', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2018-04-17 02:41:07 -05:00
|
|
|
dash.panels.push(new PanelModel({ repeatPanelId: 10 }));
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(hasChanges(dash, original)).toBe(false);
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|
2021-04-20 02:55:46 -05:00
|
|
|
|
|
|
|
describe('ignoreChanges', () => {
|
|
|
|
describe('when called without original dashboard', () => {
|
|
|
|
it('then it should return true', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { dash } = getTestContext();
|
|
|
|
expect(ignoreChanges(dash, null)).toBe(true);
|
2021-04-20 02:55:46 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called without current dashboard', () => {
|
|
|
|
it('then it should return true', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original } = getTestContext();
|
|
|
|
expect(ignoreChanges((null as unknown) as DashboardModel, original)).toBe(true);
|
2021-04-20 02:55:46 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called without meta in current dashboard', () => {
|
|
|
|
it('then it should return true', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
|
|
|
expect(ignoreChanges({ ...dash, meta: undefined }, original)).toBe(true);
|
2021-04-20 02:55:46 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called for a viewer without save permissions', () => {
|
|
|
|
it('then it should return true', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash, contextSrv } = getTestContext();
|
2021-04-20 02:55:46 -05:00
|
|
|
contextSrv.isEditor = false;
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(ignoreChanges({ ...dash, meta: { canSave: false } }, original)).toBe(true);
|
2021-04-20 02:55:46 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called for a viewer with save permissions', () => {
|
|
|
|
it('then it should return undefined', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash, contextSrv } = getTestContext();
|
2021-04-20 02:55:46 -05:00
|
|
|
contextSrv.isEditor = false;
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(ignoreChanges({ ...dash, meta: { canSave: true } }, original)).toBe(undefined);
|
2021-04-20 02:55:46 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called for an user that is not signed in', () => {
|
|
|
|
it('then it should return true', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash, contextSrv } = getTestContext();
|
2021-04-20 02:55:46 -05:00
|
|
|
contextSrv.isSignedIn = false;
|
2021-06-02 05:24:19 -05:00
|
|
|
expect(ignoreChanges({ ...dash, meta: { canSave: true } }, original)).toBe(true);
|
2021-04-20 02:55:46 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with fromScript', () => {
|
|
|
|
it('then it should return true', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2021-04-20 02:55:46 -05:00
|
|
|
expect(
|
2021-06-02 05:24:19 -05:00
|
|
|
ignoreChanges({ ...dash, meta: { canSave: true, fromScript: true, fromFile: undefined } }, original)
|
2021-04-20 02:55:46 -05:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with fromFile', () => {
|
|
|
|
it('then it should return true', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2021-04-20 02:55:46 -05:00
|
|
|
expect(
|
2021-06-02 05:24:19 -05:00
|
|
|
ignoreChanges({ ...dash, meta: { canSave: true, fromScript: undefined, fromFile: true } }, original)
|
2021-04-20 02:55:46 -05:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with canSave but without fromScript and fromFile', () => {
|
|
|
|
it('then it should return false', () => {
|
2021-06-02 05:24:19 -05:00
|
|
|
const { original, dash } = getTestContext();
|
2021-04-20 02:55:46 -05:00
|
|
|
expect(
|
2021-06-02 05:24:19 -05:00
|
|
|
ignoreChanges({ ...dash, meta: { canSave: true, fromScript: undefined, fromFile: undefined } }, original)
|
2021-04-20 02:55:46 -05:00
|
|
|
).toBe(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-04-17 02:41:07 -05:00
|
|
|
});
|