selectors.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { DataSourceApi } from '@grafana/data';
  2. import { LegacyVariableQueryEditor } from './LegacyVariableQueryEditor';
  3. import {
  4. AdHocVariableEditorState,
  5. DataSourceVariableEditorState,
  6. initialVariableEditorState,
  7. QueryVariableEditorState,
  8. } from './reducer';
  9. import {
  10. getAdhocVariableEditorState,
  11. getDatasourceVariableEditorState,
  12. getQueryVariableEditorState,
  13. } from './selectors';
  14. const adhocExtended: AdHocVariableEditorState = {
  15. dataSources: [
  16. { text: 'Prometheus', value: null }, // default datasource
  17. { text: 'Loki', value: { type: 'loki-ds', uid: 'abc' } },
  18. ],
  19. };
  20. const datasourceExtended: DataSourceVariableEditorState = {
  21. dataSourceTypes: [
  22. { text: 'Prometheus', value: 'ds-prom' },
  23. { text: 'Loki', value: 'ds-loki' },
  24. ],
  25. };
  26. const queryExtended: QueryVariableEditorState = {
  27. VariableQueryEditor: LegacyVariableQueryEditor,
  28. dataSource: {} as unknown as DataSourceApi,
  29. };
  30. const adhocVariableState = {
  31. ...initialVariableEditorState,
  32. extended: adhocExtended,
  33. };
  34. const datasourceVariableState = {
  35. ...initialVariableEditorState,
  36. extended: datasourceExtended,
  37. };
  38. const queryVariableState = {
  39. ...initialVariableEditorState,
  40. extended: queryExtended,
  41. };
  42. describe('getAdhocVariableEditorState', () => {
  43. it('returns the extended properties for adhoc variable state', () => {
  44. expect(getAdhocVariableEditorState(adhocVariableState)).toBe(adhocExtended);
  45. });
  46. it('returns null for datasource variable state', () => {
  47. expect(getAdhocVariableEditorState(datasourceVariableState)).toBeNull();
  48. });
  49. it('returns null for query variable state', () => {
  50. expect(getAdhocVariableEditorState(queryVariableState)).toBeNull();
  51. });
  52. });
  53. describe('getDatasourceVariableEditorState', () => {
  54. it('returns the extended properties for datasource variable state', () => {
  55. expect(getDatasourceVariableEditorState(datasourceVariableState)).toBe(datasourceExtended);
  56. });
  57. it('returns null for adhoc variable state', () => {
  58. expect(getDatasourceVariableEditorState(adhocVariableState)).toBeNull();
  59. });
  60. it('returns null for query variable state', () => {
  61. expect(getDatasourceVariableEditorState(queryVariableState)).toBeNull();
  62. });
  63. });
  64. describe('getQueryVariableEditorState', () => {
  65. it('returns the extended properties for query variable state', () => {
  66. expect(getQueryVariableEditorState(queryVariableState)).toBe(queryExtended);
  67. });
  68. it('returns null for adhoc variable state', () => {
  69. expect(getQueryVariableEditorState(adhocVariableState)).toBeNull();
  70. });
  71. it('returns null for datasource variable state', () => {
  72. expect(getQueryVariableEditorState(datasourceVariableState)).toBeNull();
  73. });
  74. });