DashboardModel.refresh.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { afterEach, beforeEach } from '../../../../test/lib/common';
  2. import { appEvents } from '../../../core/core';
  3. import { VariablesChanged } from '../../variables/types';
  4. import { getTimeSrv, setTimeSrv } from '../services/TimeSrv';
  5. import { DashboardModel } from './DashboardModel';
  6. import { PanelModel } from './PanelModel';
  7. function getTestContext({
  8. usePanelInEdit,
  9. usePanelInView,
  10. refreshAll = false,
  11. }: { usePanelInEdit?: boolean; usePanelInView?: boolean; refreshAll?: boolean } = {}) {
  12. jest.clearAllMocks();
  13. const dashboard = new DashboardModel({});
  14. const startRefreshMock = jest.fn();
  15. dashboard.startRefresh = startRefreshMock;
  16. const panelInView = new PanelModel({ id: 99 });
  17. const panelInEdit = new PanelModel({ id: 100 });
  18. const panelIds = [1, 2, 3];
  19. if (usePanelInEdit) {
  20. dashboard.panelInEdit = panelInEdit;
  21. panelIds.push(panelInEdit.id);
  22. }
  23. if (usePanelInView) {
  24. dashboard.panelInView = panelInView;
  25. panelIds.push(panelInView.id);
  26. }
  27. appEvents.publish(new VariablesChanged({ panelIds, refreshAll }));
  28. return { dashboard, startRefreshMock, panelInEdit, panelInView };
  29. }
  30. describe('Strict panel refresh', () => {
  31. describe('when there is no panel in full view or panel in panel edit during variable change', () => {
  32. it('then all affected panels should be refreshed', () => {
  33. const { startRefreshMock } = getTestContext();
  34. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  35. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [1, 2, 3], refreshAll: false });
  36. });
  37. });
  38. describe('when there is no panel in full view or panel in panel edit during variable change but refreshAll is true', () => {
  39. it('then all affected panels should be refreshed', () => {
  40. const { startRefreshMock } = getTestContext({ refreshAll: true });
  41. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  42. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [], refreshAll: true });
  43. });
  44. });
  45. describe('testing refresh threshold', () => {
  46. const originalTimeSrv = getTimeSrv();
  47. let isRefreshOutsideThreshold = false;
  48. beforeEach(() => {
  49. setTimeSrv({
  50. isRefreshOutsideThreshold: () => isRefreshOutsideThreshold,
  51. } as any);
  52. });
  53. afterEach(() => {
  54. setTimeSrv(originalTimeSrv);
  55. });
  56. describe('when the dashboard has not been refreshed within the threshold', () => {
  57. it(' then all panels should be refreshed', () => {
  58. isRefreshOutsideThreshold = true;
  59. const { startRefreshMock } = getTestContext();
  60. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  61. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [], refreshAll: true });
  62. });
  63. });
  64. describe('when the dashboard has been refreshed within the threshold', () => {
  65. it('then all affected panels should be refreshed', () => {
  66. isRefreshOutsideThreshold = false;
  67. const { startRefreshMock } = getTestContext();
  68. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  69. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [1, 2, 3], refreshAll: false });
  70. });
  71. });
  72. describe('when the dashboard has been refreshed within the threshold but refreshAll is true', () => {
  73. it('then all affected panels should be refreshed', () => {
  74. isRefreshOutsideThreshold = false;
  75. const { startRefreshMock } = getTestContext({ refreshAll: true });
  76. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  77. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [], refreshAll: true });
  78. });
  79. });
  80. });
  81. describe('when there is a panel in full view during variable change', () => {
  82. it('then all affected panels should be refreshed', () => {
  83. const { panelInView, startRefreshMock } = getTestContext({ usePanelInView: true });
  84. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  85. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [1, 2, 3, panelInView.id], refreshAll: false });
  86. });
  87. describe('and when exitViewPanel is called', () => {
  88. it('then all affected panels except the panel in full view should be refreshed', () => {
  89. const { dashboard, panelInView, startRefreshMock } = getTestContext({ usePanelInView: true });
  90. startRefreshMock.mockClear();
  91. dashboard.exitViewPanel(panelInView);
  92. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  93. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [1, 2, 3], refreshAll: false });
  94. expect(dashboard['panelsAffectedByVariableChange']).toBeNull();
  95. });
  96. });
  97. });
  98. describe('when there is a panel in panel edit during variable change', () => {
  99. it('then all affected panels should be refreshed', () => {
  100. const { panelInEdit, startRefreshMock } = getTestContext({ usePanelInEdit: true });
  101. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  102. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [1, 2, 3, panelInEdit.id], refreshAll: false });
  103. });
  104. describe('and when exitViewPanel is called', () => {
  105. it('then all affected panels except the panel in panel edit should be refreshed', () => {
  106. const { dashboard, startRefreshMock } = getTestContext({ usePanelInEdit: true });
  107. startRefreshMock.mockClear();
  108. dashboard.exitPanelEditor();
  109. expect(startRefreshMock).toHaveBeenCalledTimes(1);
  110. expect(startRefreshMock).toHaveBeenLastCalledWith({ panelIds: [1, 2, 3], refreshAll: false });
  111. expect(dashboard['panelsAffectedByVariableChange']).toBeNull();
  112. });
  113. });
  114. });
  115. });