graph_ctrl.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { GraphCtrl } from '../module';
  2. import { dateTime } from '@grafana/data';
  3. jest.mock('../graph', () => ({}));
  4. describe('GraphCtrl', () => {
  5. const injector = {
  6. get: () => {
  7. return {
  8. timeRange: () => {
  9. return {
  10. from: '',
  11. to: '',
  12. };
  13. },
  14. };
  15. },
  16. };
  17. const scope = {
  18. $on: () => {},
  19. };
  20. GraphCtrl.prototype.panel = {
  21. events: {
  22. on: () => {},
  23. },
  24. gridPos: {
  25. w: 100,
  26. },
  27. };
  28. const ctx = {} as any;
  29. beforeEach(() => {
  30. ctx.ctrl = new GraphCtrl(scope, injector as any, {} as any);
  31. ctx.ctrl.events = {
  32. emit: () => {},
  33. };
  34. ctx.ctrl.annotationsSrv = {
  35. getAnnotations: () => Promise.resolve({}),
  36. };
  37. ctx.ctrl.annotationsPromise = Promise.resolve({});
  38. ctx.ctrl.updateTimeRange();
  39. });
  40. describe('when time series are outside range', () => {
  41. beforeEach(() => {
  42. const data = [
  43. {
  44. target: 'test.cpu1',
  45. datapoints: [[45, 1234567890], [60, 1234567899]],
  46. },
  47. ];
  48. ctx.ctrl.range = { from: dateTime().valueOf(), to: dateTime().valueOf() };
  49. ctx.ctrl.onDataSnapshotLoad(data);
  50. });
  51. it('should set datapointsOutside', () => {
  52. expect(ctx.ctrl.dataWarning.title).toBe('Data points outside time range');
  53. });
  54. });
  55. describe('when time series are inside range', () => {
  56. beforeEach(() => {
  57. const range = {
  58. from: dateTime()
  59. .subtract(1, 'days')
  60. .valueOf(),
  61. to: dateTime().valueOf(),
  62. };
  63. const data = [
  64. {
  65. target: 'test.cpu1',
  66. datapoints: [[45, range.from + 1000], [60, range.from + 10000]],
  67. },
  68. ];
  69. ctx.ctrl.range = range;
  70. ctx.ctrl.onDataSnapshotLoad(data);
  71. });
  72. it('should set datapointsOutside', () => {
  73. expect(ctx.ctrl.dataWarning).toBe(null);
  74. });
  75. });
  76. describe('datapointsCount given 2 series', () => {
  77. beforeEach(() => {
  78. const data: any = [{ target: 'test.cpu1', datapoints: [] }, { target: 'test.cpu2', datapoints: [] }];
  79. ctx.ctrl.onDataSnapshotLoad(data);
  80. });
  81. it('should set datapointsCount warning', () => {
  82. expect(ctx.ctrl.dataWarning.title).toBe('No data points');
  83. });
  84. });
  85. });