mockExploreState.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, UserState } from 'app/types';
  5. import { TimeRange, dateTime } from '@grafana/data';
  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 user: UserState = {
  79. orgId: 1,
  80. timeZone: 'browser',
  81. };
  82. const state: Partial<StoreState> = {
  83. explore,
  84. user,
  85. };
  86. return {
  87. containerWidth,
  88. datasourceId,
  89. datasourceInstance,
  90. datasourceInterval,
  91. eventBridge,
  92. exploreId,
  93. history,
  94. queries,
  95. refreshInterval,
  96. state,
  97. scanner,
  98. range,
  99. };
  100. };