graph_ctrl.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { GraphCtrl } from '../module';
  2. import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  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.annotationsPromise = Promise.resolve({});
  35. ctx.ctrl.updateTimeRange();
  36. });
  37. describe('when time series are outside range', () => {
  38. beforeEach(() => {
  39. const data = [
  40. {
  41. target: 'test.cpu1',
  42. datapoints: [[45, 1234567890], [60, 1234567899]],
  43. },
  44. ];
  45. ctx.ctrl.range = { from: dateTime().valueOf(), to: dateTime().valueOf() };
  46. ctx.ctrl.onDataReceived(data);
  47. });
  48. it('should set datapointsOutside', () => {
  49. expect(ctx.ctrl.dataWarning.title).toBe('Data points outside time range');
  50. });
  51. });
  52. describe('when time series are inside range', () => {
  53. beforeEach(() => {
  54. const range = {
  55. from: dateTime()
  56. .subtract(1, 'days')
  57. .valueOf(),
  58. to: dateTime().valueOf(),
  59. };
  60. const data = [
  61. {
  62. target: 'test.cpu1',
  63. datapoints: [[45, range.from + 1000], [60, range.from + 10000]],
  64. },
  65. ];
  66. ctx.ctrl.range = range;
  67. ctx.ctrl.onDataReceived(data);
  68. });
  69. it('should set datapointsOutside', () => {
  70. expect(ctx.ctrl.dataWarning).toBe(null);
  71. });
  72. });
  73. describe('datapointsCount given 2 series', () => {
  74. beforeEach(() => {
  75. const data: any = [{ target: 'test.cpu1', datapoints: [] }, { target: 'test.cpu2', datapoints: [] }];
  76. ctx.ctrl.onDataReceived(data);
  77. });
  78. it('should set datapointsCount warning', () => {
  79. expect(ctx.ctrl.dataWarning.title).toBe('No data points');
  80. });
  81. });
  82. });