QueryEditorRow.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { DataQueryRequest, dateTime, LoadingState, PanelData, toDataFrame } from '@grafana/data';
  2. import { filterPanelDataToQuery } from './QueryEditorRow';
  3. function makePretendRequest(requestId: string, subRequests?: DataQueryRequest[]): DataQueryRequest {
  4. return {
  5. requestId,
  6. // subRequests,
  7. } as DataQueryRequest;
  8. }
  9. describe('filterPanelDataToQuery', () => {
  10. const data: PanelData = {
  11. state: LoadingState.Done,
  12. series: [
  13. toDataFrame({ refId: 'A', fields: [{ name: 'AAA' }], meta: {} }),
  14. toDataFrame({ refId: 'B', fields: [{ name: 'B111' }], meta: {} }),
  15. toDataFrame({ refId: 'B', fields: [{ name: 'B222' }], meta: {} }),
  16. toDataFrame({ refId: 'B', fields: [{ name: 'B333' }], meta: {} }),
  17. toDataFrame({ refId: 'C', fields: [{ name: 'CCCC' }], meta: { requestId: 'sub3' } }),
  18. ],
  19. error: {
  20. refId: 'B',
  21. message: 'Error!!',
  22. },
  23. request: makePretendRequest('111', [
  24. makePretendRequest('sub1'),
  25. makePretendRequest('sub2'),
  26. makePretendRequest('sub3'),
  27. ]),
  28. timeRange: { from: dateTime(), to: dateTime(), raw: { from: 'now-1d', to: 'now' } },
  29. };
  30. it('should not have an error unless the refId matches', () => {
  31. const panelData = filterPanelDataToQuery(data, 'A');
  32. expect(panelData?.series.length).toBe(1);
  33. expect(panelData?.series[0].refId).toBe('A');
  34. expect(panelData?.error).toBeUndefined();
  35. });
  36. it('should match the error to the query', () => {
  37. const panelData = filterPanelDataToQuery(data, 'B');
  38. expect(panelData?.series.length).toBe(3);
  39. expect(panelData?.series[0].refId).toBe('B');
  40. expect(panelData?.error!.refId).toBe('B');
  41. });
  42. it('should include errors when missing data', () => {
  43. const withError = {
  44. series: [],
  45. error: {
  46. message: 'Error!!',
  47. },
  48. } as unknown as PanelData;
  49. const panelData = filterPanelDataToQuery(withError, 'B');
  50. expect(panelData).toBeDefined();
  51. expect(panelData?.state).toBe(LoadingState.Error);
  52. expect(panelData?.error).toBe(withError.error);
  53. });
  54. it('should set the state to done if the frame has no errors', () => {
  55. const withError = {
  56. ...data,
  57. };
  58. withError.state = LoadingState.Error;
  59. const panelDataB = filterPanelDataToQuery(withError, 'B');
  60. expect(panelDataB?.series.length).toBe(3);
  61. expect(panelDataB?.series[0].refId).toBe('B');
  62. expect(panelDataB?.state).toBe(LoadingState.Error);
  63. const panelDataA = filterPanelDataToQuery(withError, 'A');
  64. expect(panelDataA?.series.length).toBe(1);
  65. expect(panelDataA?.series[0].refId).toBe('A');
  66. expect(panelDataA?.state).toBe(LoadingState.Done);
  67. });
  68. it('should return error for query that returns no data, but another query does return data', () => {
  69. const withError = {
  70. ...data,
  71. state: LoadingState.Error,
  72. error: {
  73. message: 'Sad',
  74. refId: 'Q',
  75. },
  76. };
  77. const panelDataB = filterPanelDataToQuery(withError, 'Q');
  78. expect(panelDataB?.series.length).toBe(0);
  79. expect(panelDataB?.error?.refId).toBe('Q');
  80. });
  81. it('should not set the state to done if the frame is loading and has no errors', () => {
  82. const loadingData: PanelData = {
  83. state: LoadingState.Loading,
  84. series: [
  85. toDataFrame({ refId: 'A', fields: [{ name: 'AAA' }], meta: {} }),
  86. toDataFrame({ refId: 'B', fields: [{ name: 'B111' }], meta: {} }),
  87. ],
  88. timeRange: { from: dateTime(), to: dateTime(), raw: { from: 'now-1d', to: 'now' } },
  89. };
  90. const panelDataB = filterPanelDataToQuery(loadingData, 'B');
  91. expect(panelDataB?.state).toBe(LoadingState.Loading);
  92. const panelDataA = filterPanelDataToQuery(loadingData, 'A');
  93. expect(panelDataA?.state).toBe(LoadingState.Loading);
  94. });
  95. });