datasource_srv.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import config from 'app/core/config';
  2. import 'app/features/plugins/datasource_srv';
  3. import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
  4. // Datasource variable $datasource with current value 'BBB'
  5. const templateSrv = {
  6. variables: [
  7. {
  8. type: 'datasource',
  9. name: 'datasource',
  10. current: {
  11. value: 'BBB',
  12. },
  13. },
  14. ],
  15. };
  16. describe('datasource_srv', () => {
  17. const _datasourceSrv = new DatasourceSrv({}, {}, {}, templateSrv);
  18. describe('when loading metric sources', () => {
  19. let metricSources;
  20. const unsortedDatasources = {
  21. mmm: {
  22. type: 'test-db',
  23. meta: { metrics: { m: 1 } },
  24. },
  25. '--Grafana--': {
  26. type: 'grafana',
  27. meta: { builtIn: true, metrics: { m: 1 }, id: 'grafana' },
  28. },
  29. '--Mixed--': {
  30. type: 'test-db',
  31. meta: { builtIn: true, metrics: { m: 1 }, id: 'mixed' },
  32. },
  33. ZZZ: {
  34. type: 'test-db',
  35. meta: { metrics: { m: 1 } },
  36. },
  37. aaa: {
  38. type: 'test-db',
  39. meta: { metrics: { m: 1 } },
  40. },
  41. BBB: {
  42. type: 'test-db',
  43. meta: { metrics: { m: 1 } },
  44. },
  45. };
  46. beforeEach(() => {
  47. config.datasources = unsortedDatasources;
  48. metricSources = _datasourceSrv.getMetricSources({});
  49. config.defaultDatasource = 'BBB';
  50. });
  51. it('should return a list of sources sorted case insensitively with builtin sources last', () => {
  52. expect(metricSources[1].name).toBe('aaa');
  53. expect(metricSources[2].name).toBe('BBB');
  54. expect(metricSources[3].name).toBe('mmm');
  55. expect(metricSources[4].name).toBe('ZZZ');
  56. expect(metricSources[5].name).toBe('--Grafana--');
  57. expect(metricSources[6].name).toBe('--Mixed--');
  58. });
  59. it('should set default data source', () => {
  60. expect(metricSources[3].name).toBe('default');
  61. expect(metricSources[3].sort).toBe('BBB');
  62. });
  63. it('should set default inject the variable datasources', () => {
  64. expect(metricSources[0].name).toBe('$datasource');
  65. expect(metricSources[0].sort).toBe('$datasource');
  66. });
  67. });
  68. });