mockExploreState.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { DataSourceApi } from '@grafana/ui/src/types/datasource';
  2. import { ExploreId, ExploreItemState, ExploreState } from 'app/types/explore';
  3. import { makeExploreItemState } from 'app/features/explore/state/reducers';
  4. import { StoreState } from 'app/types';
  5. import { TimeRange, dateTime } from '@grafana/ui';
  6. export const mockExploreState = (options: any = {}) => {
  7. const isLive = options.isLive || false;
  8. const history: any[] = [];
  9. const eventBridge = {
  10. emit: jest.fn(),
  11. };
  12. const streaming = options.streaming || undefined;
  13. const datasourceInterval = options.datasourceInterval || '';
  14. const refreshInterval = options.refreshInterval || '';
  15. const containerWidth = options.containerWidth || 1980;
  16. const queries = options.queries || [];
  17. const datasourceError = options.datasourceError || null;
  18. const scanner = options.scanner || jest.fn();
  19. const scanning = options.scanning || false;
  20. const datasourceId = options.datasourceId || '1337';
  21. const exploreId = ExploreId.left;
  22. const datasourceInstance: DataSourceApi<any> = options.datasourceInstance || {
  23. id: 1337,
  24. query: jest.fn(),
  25. name: 'test',
  26. testDatasource: jest.fn(),
  27. meta: {
  28. id: datasourceId,
  29. streaming,
  30. },
  31. interval: datasourceInterval,
  32. };
  33. const range: TimeRange = options.range || {
  34. from: dateTime('2019-01-01 10:00:00.000Z'),
  35. to: dateTime('2019-01-01 16:00:00.000Z'),
  36. raw: {
  37. from: 'now-6h',
  38. to: 'now',
  39. },
  40. };
  41. const urlReplaced = options.urlReplaced || false;
  42. const left: ExploreItemState = options.left || {
  43. ...makeExploreItemState(),
  44. containerWidth,
  45. datasourceError,
  46. datasourceInstance,
  47. eventBridge,
  48. history,
  49. isLive,
  50. queries,
  51. refreshInterval,
  52. scanner,
  53. scanning,
  54. urlReplaced,
  55. range,
  56. };
  57. const right: ExploreItemState = options.right || {
  58. ...makeExploreItemState(),
  59. containerWidth,
  60. datasourceError,
  61. datasourceInstance,
  62. eventBridge,
  63. history,
  64. isLive,
  65. queries,
  66. refreshInterval,
  67. scanner,
  68. scanning,
  69. urlReplaced,
  70. range,
  71. };
  72. const split: boolean = options.split || false;
  73. const explore: ExploreState = {
  74. left,
  75. right,
  76. split,
  77. };
  78. const state: Partial<StoreState> = {
  79. explore,
  80. };
  81. return {
  82. containerWidth,
  83. datasourceId,
  84. datasourceInstance,
  85. datasourceInterval,
  86. eventBridge,
  87. exploreId,
  88. history,
  89. queries,
  90. refreshInterval,
  91. state,
  92. scanner,
  93. range,
  94. };
  95. };