query_ctrl.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { OpenTsQueryCtrl } from '../query_ctrl';
  2. describe('OpenTsQueryCtrl', () => {
  3. const ctx = {
  4. target: { target: '' },
  5. datasource: {
  6. tsdbVersion: '',
  7. getAggregators: () => Promise.resolve([]),
  8. getFilterTypes: () => Promise.resolve([]),
  9. },
  10. } as any;
  11. ctx.panelCtrl = {
  12. panel: {
  13. targets: [ctx.target],
  14. },
  15. refresh: () => {},
  16. };
  17. Object.assign(OpenTsQueryCtrl.prototype, ctx);
  18. beforeEach(() => {
  19. ctx.ctrl = new OpenTsQueryCtrl({}, {} as any);
  20. });
  21. describe('init query_ctrl variables', () => {
  22. it('filter types should be initialized', () => {
  23. expect(ctx.ctrl.filterTypes.length).toBe(7);
  24. });
  25. it('aggregators should be initialized', () => {
  26. expect(ctx.ctrl.aggregators.length).toBe(8);
  27. });
  28. it('fill policy options should be initialized', () => {
  29. expect(ctx.ctrl.fillPolicies.length).toBe(4);
  30. });
  31. });
  32. describe('when adding filters and tags', () => {
  33. it('addTagMode should be false when closed', () => {
  34. ctx.ctrl.addTagMode = true;
  35. ctx.ctrl.closeAddTagMode();
  36. expect(ctx.ctrl.addTagMode).toBe(false);
  37. });
  38. it('addFilterMode should be false when closed', () => {
  39. ctx.ctrl.addFilterMode = true;
  40. ctx.ctrl.closeAddFilterMode();
  41. expect(ctx.ctrl.addFilterMode).toBe(false);
  42. });
  43. it('removing a tag from the tags list', () => {
  44. ctx.ctrl.target.tags = { tagk: 'tag_key', tagk2: 'tag_value2' };
  45. ctx.ctrl.removeTag('tagk');
  46. expect(Object.keys(ctx.ctrl.target.tags).length).toBe(1);
  47. });
  48. it('removing a filter from the filters list', () => {
  49. ctx.ctrl.target.filters = [
  50. {
  51. tagk: 'tag_key',
  52. filter: 'tag_value2',
  53. type: 'wildcard',
  54. groupBy: true,
  55. },
  56. ];
  57. ctx.ctrl.removeFilter(0);
  58. expect(ctx.ctrl.target.filters.length).toBe(0);
  59. });
  60. it('adding a filter when tags exist should generate error', () => {
  61. ctx.ctrl.target.tags = { tagk: 'tag_key', tagk2: 'tag_value2' };
  62. ctx.ctrl.addFilter();
  63. expect(ctx.ctrl.errors.filters).toBe(
  64. 'Please remove tags to use filters, tags and filters are mutually exclusive.'
  65. );
  66. });
  67. it('adding a tag when filters exist should generate error', () => {
  68. ctx.ctrl.target.filters = [
  69. {
  70. tagk: 'tag_key',
  71. filter: 'tag_value2',
  72. type: 'wildcard',
  73. groupBy: true,
  74. },
  75. ];
  76. ctx.ctrl.addTag();
  77. expect(ctx.ctrl.errors.tags).toBe('Please remove filters to use tags, tags and filters are mutually exclusive.');
  78. });
  79. });
  80. });