grafana/public/app/features/variables/editor/selectors.test.ts
Josh Hunt 1c6eca09bb
Variables: Explicitly type variable editor extended state (#44749)
* Narrow Variable editor state using selector functions

 - Explicitly type "extended" editor state in editor/reducter.ts using a union
 - Create selectors to narrow the types, using unique properties from each extended state to discriminate the union
 - Update DataSourceVariableEditor to use new style of redux connector
 - Update variable editor components to use new selectors

* fix tests

* Make adhoc variable infoText optional, because it is!

* Add AdHocVariableEditor tests

* DataSourceVariableEditor tests

* comment

* reset

* Wrote tests for selectors \(that actually caught a bug, whodathunkit)

* fix stray types and lint issues

* Rename selector functions
2022-02-08 09:31:42 +11:00

90 lines
2.6 KiB
TypeScript

import {
getAdhocVariableEditorState,
getDatasourceVariableEditorState,
getQueryVariableEditorState,
} from './selectors';
import {
AdHocVariableEditorState,
DataSourceVariableEditorState,
initialVariableEditorState,
QueryVariableEditorState,
} from './reducer';
import { LegacyVariableQueryEditor } from './LegacyVariableQueryEditor';
import { DataSourceApi } from '@grafana/data';
const adhocExtended: AdHocVariableEditorState = {
dataSources: [
{ text: 'Prometheus', value: null }, // default datasource
{ text: 'Loki', value: { type: 'loki-ds', uid: 'abc' } },
],
};
const datasourceExtended: DataSourceVariableEditorState = {
dataSourceTypes: [
{ text: 'Prometheus', value: 'ds-prom' },
{ text: 'Loki', value: 'ds-loki' },
],
};
const queryExtended: QueryVariableEditorState = {
VariableQueryEditor: LegacyVariableQueryEditor,
dataSource: {} as unknown as DataSourceApi,
};
const adhocVariableState = {
...initialVariableEditorState,
extended: adhocExtended,
};
const datasourceVariableState = {
...initialVariableEditorState,
extended: datasourceExtended,
};
const queryVariableState = {
...initialVariableEditorState,
extended: queryExtended,
};
describe('getAdhocVariableEditorState', () => {
it('returns the extended properties for adhoc variable state', () => {
expect(getAdhocVariableEditorState(adhocVariableState)).toBe(adhocExtended);
});
it('returns null for datasource variable state', () => {
expect(getAdhocVariableEditorState(datasourceVariableState)).toBeNull();
});
it('returns null for query variable state', () => {
expect(getAdhocVariableEditorState(queryVariableState)).toBeNull();
});
});
describe('getDatasourceVariableEditorState', () => {
it('returns the extended properties for datasource variable state', () => {
expect(getDatasourceVariableEditorState(datasourceVariableState)).toBe(datasourceExtended);
});
it('returns null for adhoc variable state', () => {
expect(getDatasourceVariableEditorState(adhocVariableState)).toBeNull();
});
it('returns null for query variable state', () => {
expect(getDatasourceVariableEditorState(queryVariableState)).toBeNull();
});
});
describe('getQueryVariableEditorState', () => {
it('returns the extended properties for query variable state', () => {
expect(getQueryVariableEditorState(queryVariableState)).toBe(queryExtended);
});
it('returns null for adhoc variable state', () => {
expect(getQueryVariableEditorState(adhocVariableState)).toBeNull();
});
it('returns null for datasource variable state', () => {
expect(getQueryVariableEditorState(datasourceVariableState)).toBeNull();
});
});