DashboardPrompt.test.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { getPanelPlugin } from 'app/features/plugins/__mocks__/pluginMocks';
  2. import { setContextSrv } from '../../../../core/services/context_srv';
  3. import { DashboardModel } from '../../state/DashboardModel';
  4. import { PanelModel } from '../../state/PanelModel';
  5. import { hasChanges, ignoreChanges } from './DashboardPrompt';
  6. function getDefaultDashboardModel(): DashboardModel {
  7. return new DashboardModel({
  8. refresh: false,
  9. panels: [
  10. {
  11. id: 1,
  12. type: 'graph',
  13. gridPos: { x: 0, y: 0, w: 24, h: 6 },
  14. legend: { sortDesc: false },
  15. },
  16. {
  17. id: 2,
  18. type: 'row',
  19. gridPos: { x: 0, y: 6, w: 24, h: 2 },
  20. collapsed: true,
  21. panels: [
  22. { id: 3, type: 'graph', gridPos: { x: 0, y: 6, w: 12, h: 2 } },
  23. { id: 4, type: 'graph', gridPos: { x: 12, y: 6, w: 12, h: 2 } },
  24. ],
  25. },
  26. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 1, h: 1 } },
  27. ],
  28. });
  29. }
  30. function getTestContext() {
  31. const contextSrv: any = { isSignedIn: true, isEditor: true };
  32. setContextSrv(contextSrv);
  33. const dash: any = getDefaultDashboardModel();
  34. const original: any = dash.getSaveModelClone();
  35. return { dash, original, contextSrv };
  36. }
  37. describe('DashboardPrompt', () => {
  38. it('No changes should not have changes', () => {
  39. const { original, dash } = getTestContext();
  40. expect(hasChanges(dash, original)).toBe(false);
  41. });
  42. it('Simple change should be registered', () => {
  43. const { original, dash } = getTestContext();
  44. dash.title = 'google';
  45. expect(hasChanges(dash, original)).toBe(true);
  46. });
  47. it('Should ignore a lot of changes', () => {
  48. const { original, dash } = getTestContext();
  49. dash.time = { from: '1h' };
  50. dash.refresh = true;
  51. dash.schemaVersion = 10;
  52. expect(hasChanges(dash, original)).toBe(false);
  53. });
  54. it('Should ignore row collapse change', () => {
  55. const { original, dash } = getTestContext();
  56. dash.toggleRow(dash.panels[1]);
  57. expect(hasChanges(dash, original)).toBe(false);
  58. });
  59. it('Should ignore panel legend changes', () => {
  60. const { original, dash } = getTestContext();
  61. dash.panels[0].legend.sortDesc = true;
  62. dash.panels[0].legend.sort = 'avg';
  63. expect(hasChanges(dash, original)).toBe(false);
  64. });
  65. it('Should ignore panel repeats', () => {
  66. const { original, dash } = getTestContext();
  67. dash.panels.push(new PanelModel({ repeatPanelId: 10 }));
  68. expect(hasChanges(dash, original)).toBe(false);
  69. });
  70. describe('ignoreChanges', () => {
  71. describe('when called without original dashboard', () => {
  72. it('then it should return true', () => {
  73. const { dash } = getTestContext();
  74. expect(ignoreChanges(dash, null)).toBe(true);
  75. });
  76. });
  77. describe('when called without current dashboard', () => {
  78. it('then it should return true', () => {
  79. const { original } = getTestContext();
  80. expect(ignoreChanges(null as unknown as DashboardModel, original)).toBe(true);
  81. });
  82. });
  83. describe('when called without meta in current dashboard', () => {
  84. it('then it should return true', () => {
  85. const { original, dash } = getTestContext();
  86. expect(ignoreChanges({ ...dash, meta: undefined }, original)).toBe(true);
  87. });
  88. });
  89. describe('when called for a viewer without save permissions', () => {
  90. it('then it should return true', () => {
  91. const { original, dash, contextSrv } = getTestContext();
  92. contextSrv.isEditor = false;
  93. expect(ignoreChanges({ ...dash, meta: { canSave: false } }, original)).toBe(true);
  94. });
  95. });
  96. describe('when called for a viewer with save permissions', () => {
  97. it('then it should return undefined', () => {
  98. const { original, dash, contextSrv } = getTestContext();
  99. contextSrv.isEditor = false;
  100. expect(ignoreChanges({ ...dash, meta: { canSave: true } }, original)).toBe(undefined);
  101. });
  102. });
  103. describe('when called for an user that is not signed in', () => {
  104. it('then it should return true', () => {
  105. const { original, dash, contextSrv } = getTestContext();
  106. contextSrv.isSignedIn = false;
  107. expect(ignoreChanges({ ...dash, meta: { canSave: true } }, original)).toBe(true);
  108. });
  109. });
  110. describe('when called with fromScript', () => {
  111. it('then it should return true', () => {
  112. const { original, dash } = getTestContext();
  113. expect(
  114. ignoreChanges({ ...dash, meta: { canSave: true, fromScript: true, fromFile: undefined } }, original)
  115. ).toBe(true);
  116. });
  117. });
  118. it('Should ignore panel schema migrations', () => {
  119. const { original, dash } = getTestContext();
  120. const plugin = getPanelPlugin({}).setMigrationHandler((panel) => {
  121. delete (panel as any).legend;
  122. return { option1: 'Aasd' };
  123. });
  124. dash.panels[0].pluginLoaded(plugin);
  125. expect(hasChanges(dash, original)).toBe(false);
  126. });
  127. describe('when called with fromFile', () => {
  128. it('then it should return true', () => {
  129. const { original, dash } = getTestContext();
  130. expect(
  131. ignoreChanges({ ...dash, meta: { canSave: true, fromScript: undefined, fromFile: true } }, original)
  132. ).toBe(true);
  133. });
  134. });
  135. describe('when called with canSave but without fromScript and fromFile', () => {
  136. it('then it should return false', () => {
  137. const { original, dash } = getTestContext();
  138. expect(
  139. ignoreChanges({ ...dash, meta: { canSave: true, fromScript: undefined, fromFile: undefined } }, original)
  140. ).toBe(undefined);
  141. });
  142. });
  143. });
  144. });