addToDashboard.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { DataQuery, MutableDataFrame } from '@grafana/data';
  2. import { backendSrv } from 'app/core/services/backend_srv';
  3. import * as api from 'app/features/dashboard/state/initDashboard';
  4. import { ExplorePanelData } from 'app/types';
  5. import { createEmptyQueryResponse } from '../state/utils';
  6. import { setDashboardInLocalStorage } from './addToDashboard';
  7. describe('addPanelToDashboard', () => {
  8. let spy: jest.SpyInstance;
  9. beforeAll(() => {
  10. spy = jest.spyOn(api, 'setDashboardToFetchFromLocalStorage');
  11. });
  12. afterEach(() => {
  13. jest.resetAllMocks();
  14. });
  15. it('Correct datasource ref is used', async () => {
  16. await setDashboardInLocalStorage({
  17. queries: [],
  18. queryResponse: createEmptyQueryResponse(),
  19. datasource: { type: 'loki', uid: 'someUid' },
  20. });
  21. expect(spy).toHaveBeenCalledWith(
  22. expect.objectContaining({
  23. dashboard: expect.objectContaining({
  24. panels: expect.arrayContaining([expect.objectContaining({ datasource: { type: 'loki', uid: 'someUid' } })]),
  25. }),
  26. })
  27. );
  28. });
  29. it('All queries are correctly passed through', async () => {
  30. const queries: DataQuery[] = [{ refId: 'A' }, { refId: 'B', hide: true }];
  31. await setDashboardInLocalStorage({
  32. queries,
  33. queryResponse: createEmptyQueryResponse(),
  34. });
  35. expect(spy).toHaveBeenCalledWith(
  36. expect.objectContaining({
  37. dashboard: expect.objectContaining({
  38. panels: expect.arrayContaining([expect.objectContaining({ targets: expect.arrayContaining(queries) })]),
  39. }),
  40. })
  41. );
  42. });
  43. it('Previous panels should not be removed', async () => {
  44. const queries: DataQuery[] = [{ refId: 'A' }];
  45. const existingPanel = { prop: 'this should be kept' };
  46. jest.spyOn(backendSrv, 'getDashboardByUid').mockResolvedValue({
  47. dashboard: {
  48. templating: { list: [] },
  49. title: 'Previous panels should not be removed',
  50. uid: 'someUid',
  51. panels: [existingPanel],
  52. },
  53. meta: {},
  54. });
  55. await setDashboardInLocalStorage({
  56. queries,
  57. queryResponse: createEmptyQueryResponse(),
  58. dashboardUid: 'someUid',
  59. datasource: { type: '' },
  60. });
  61. expect(spy).toHaveBeenCalledWith(
  62. expect.objectContaining({
  63. dashboard: expect.objectContaining({
  64. panels: expect.arrayContaining([
  65. expect.objectContaining({ targets: expect.arrayContaining(queries) }),
  66. existingPanel,
  67. ]),
  68. }),
  69. })
  70. );
  71. });
  72. describe('Setting visualization type', () => {
  73. describe('Defaults to table', () => {
  74. const cases: Array<[string, DataQuery[], ExplorePanelData]> = [
  75. ['If response is empty', [{ refId: 'A' }], createEmptyQueryResponse()],
  76. ['If no query is active', [{ refId: 'A', hide: true }], createEmptyQueryResponse()],
  77. [
  78. 'If no query is active, even when there is a response from a previous execution',
  79. [{ refId: 'A', hide: true }],
  80. { ...createEmptyQueryResponse(), logsFrames: [new MutableDataFrame({ refId: 'A', fields: [] })] },
  81. ],
  82. ];
  83. it.each(cases)('%s', async (_, queries, queryResponse) => {
  84. await setDashboardInLocalStorage({ queries, queryResponse });
  85. expect(spy).toHaveBeenCalledWith(
  86. expect.objectContaining({
  87. dashboard: expect.objectContaining({
  88. panels: expect.arrayContaining([expect.objectContaining({ type: 'table' })]),
  89. }),
  90. })
  91. );
  92. });
  93. });
  94. describe('Correctly set visualization based on response', () => {
  95. type TestArgs = {
  96. framesType: string;
  97. expectedPanel: string;
  98. };
  99. it.each`
  100. framesType | expectedPanel
  101. ${'logsFrames'} | ${'logs'}
  102. ${'graphFrames'} | ${'timeseries'}
  103. ${'nodeGraphFrames'} | ${'nodeGraph'}
  104. ${'traceFrames'} | ${'traces'}
  105. `(
  106. 'Sets visualization to $expectedPanel if there are $frameType frames',
  107. async ({ framesType, expectedPanel }: TestArgs) => {
  108. const queries = [{ refId: 'A' }];
  109. const queryResponse: ExplorePanelData = {
  110. ...createEmptyQueryResponse(),
  111. [framesType]: [new MutableDataFrame({ refId: 'A', fields: [] })],
  112. };
  113. await setDashboardInLocalStorage({ queries, queryResponse });
  114. expect(spy).toHaveBeenCalledWith(
  115. expect.objectContaining({
  116. dashboard: expect.objectContaining({
  117. panels: expect.arrayContaining([expect.objectContaining({ type: expectedPanel })]),
  118. }),
  119. })
  120. );
  121. }
  122. );
  123. });
  124. });
  125. });