BarGaugePanel.test.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { mount, ReactWrapper } from 'enzyme';
  2. import React from 'react';
  3. import {
  4. dateMath,
  5. dateTime,
  6. FieldConfigSource,
  7. LoadingState,
  8. PanelData,
  9. PanelProps,
  10. TimeRange,
  11. toDataFrame,
  12. VizOrientation,
  13. } from '@grafana/data';
  14. import { selectors } from '@grafana/e2e-selectors';
  15. import { BarGaugeDisplayMode } from '@grafana/ui';
  16. import { BarGaugePanel } from './BarGaugePanel';
  17. import { BarGaugeOptions } from './types';
  18. const valueSelector = selectors.components.Panels.Visualization.BarGauge.valueV2;
  19. describe('BarGaugePanel', () => {
  20. describe('when empty result is rendered', () => {
  21. const wrapper = createBarGaugePanelWithData({
  22. series: [],
  23. timeRange: createTimeRange(),
  24. state: LoadingState.Done,
  25. });
  26. it('should render with title "No data"', () => {
  27. const displayValue = wrapper.find(`div[data-testid="${valueSelector}"]`).text();
  28. expect(displayValue).toBe('No data');
  29. });
  30. });
  31. describe('when there is data', () => {
  32. const wrapper = createBarGaugePanelWithData({
  33. series: [
  34. toDataFrame({
  35. target: 'test',
  36. datapoints: [
  37. [100, 1000],
  38. [100, 200],
  39. ],
  40. }),
  41. ],
  42. timeRange: createTimeRange(),
  43. state: LoadingState.Done,
  44. });
  45. it('should render with title "No data"', () => {
  46. const displayValue = wrapper.find(`div[data-testid="${valueSelector}"]`).text();
  47. expect(displayValue).toBe('100');
  48. });
  49. });
  50. });
  51. function createTimeRange(): TimeRange {
  52. return {
  53. from: dateMath.parse('now-6h') || dateTime(),
  54. to: dateMath.parse('now') || dateTime(),
  55. raw: { from: 'now-6h', to: 'now' },
  56. };
  57. }
  58. function createBarGaugePanelWithData(data: PanelData): ReactWrapper<PanelProps<BarGaugeOptions>> {
  59. const timeRange = createTimeRange();
  60. const options: BarGaugeOptions = {
  61. displayMode: BarGaugeDisplayMode.Lcd,
  62. reduceOptions: {
  63. calcs: ['mean'],
  64. values: false,
  65. },
  66. orientation: VizOrientation.Horizontal,
  67. showUnfilled: true,
  68. minVizHeight: 10,
  69. minVizWidth: 0,
  70. };
  71. const fieldConfig: FieldConfigSource = {
  72. defaults: {},
  73. overrides: [],
  74. };
  75. return mount<BarGaugePanel>(
  76. <BarGaugePanel
  77. id={1}
  78. data={data}
  79. timeRange={timeRange}
  80. timeZone={'utc'}
  81. options={options}
  82. title="hello"
  83. fieldConfig={fieldConfig}
  84. onFieldConfigChange={() => {}}
  85. onOptionsChange={() => {}}
  86. onChangeTimeRange={() => {}}
  87. replaceVariables={(s) => s}
  88. renderCounter={0}
  89. width={532}
  90. transparent={false}
  91. height={250}
  92. eventBus={{} as any}
  93. />
  94. );
  95. }