change_tracker.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { ChangeTracker } from 'app/features/dashboard/change_tracker';
  2. import { contextSrv } from 'app/core/services/context_srv';
  3. import { DashboardModel } from '../dashboard_model';
  4. import { PanelModel } from '../panel_model';
  5. jest.mock('app/core/services/context_srv', () => ({
  6. contextSrv: {
  7. user: { orgId: 1 },
  8. },
  9. }));
  10. describe('ChangeTracker', () => {
  11. let rootScope;
  12. let location;
  13. const timeout = () => {};
  14. let tracker: ChangeTracker;
  15. let dash;
  16. let scope;
  17. beforeEach(() => {
  18. dash = new DashboardModel({
  19. refresh: false,
  20. panels: [
  21. {
  22. id: 1,
  23. type: 'graph',
  24. gridPos: { x: 0, y: 0, w: 24, h: 6 },
  25. legend: { sortDesc: false },
  26. },
  27. {
  28. id: 2,
  29. type: 'row',
  30. gridPos: { x: 0, y: 6, w: 24, h: 2 },
  31. collapsed: true,
  32. panels: [
  33. { id: 3, type: 'graph', gridPos: { x: 0, y: 6, w: 12, h: 2 } },
  34. { id: 4, type: 'graph', gridPos: { x: 12, y: 6, w: 12, h: 2 } },
  35. ],
  36. },
  37. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 1, h: 1 } },
  38. ],
  39. });
  40. scope = {
  41. appEvent: jest.fn(),
  42. onAppEvent: jest.fn(),
  43. $on: jest.fn(),
  44. };
  45. rootScope = {
  46. appEvent: jest.fn(),
  47. onAppEvent: jest.fn(),
  48. $on: jest.fn(),
  49. };
  50. location = {
  51. path: jest.fn(),
  52. };
  53. tracker = new ChangeTracker(dash, scope, undefined, location, window, timeout, contextSrv, rootScope);
  54. });
  55. it('No changes should not have changes', () => {
  56. expect(tracker.hasChanges()).toBe(false);
  57. });
  58. it('Simple change should be registered', () => {
  59. dash.title = 'google';
  60. expect(tracker.hasChanges()).toBe(true);
  61. });
  62. it('Should ignore a lot of changes', () => {
  63. dash.time = { from: '1h' };
  64. dash.refresh = true;
  65. dash.schemaVersion = 10;
  66. expect(tracker.hasChanges()).toBe(false);
  67. });
  68. it('Should ignore .iteration changes', () => {
  69. dash.iteration = new Date().getTime() + 1;
  70. expect(tracker.hasChanges()).toBe(false);
  71. });
  72. it('Should ignore row collapse change', () => {
  73. dash.toggleRow(dash.panels[1]);
  74. expect(tracker.hasChanges()).toBe(false);
  75. });
  76. it('Should ignore panel legend changes', () => {
  77. dash.panels[0].legend.sortDesc = true;
  78. dash.panels[0].legend.sort = 'avg';
  79. expect(tracker.hasChanges()).toBe(false);
  80. });
  81. it('Should ignore panel repeats', () => {
  82. dash.panels.push(new PanelModel({ repeatPanelId: 10 }));
  83. expect(tracker.hasChanges()).toBe(false);
  84. });
  85. });