2020-11-27 03:20:57 -06:00
|
|
|
import { VariableSupportType } from '@grafana/data';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2020-11-27 03:20:57 -06:00
|
|
|
import { thunkTester } from '../../../../test/core/thunk/thunkTester';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { customBuilder, queryBuilder } from '../shared/testing/builders';
|
2022-01-17 05:48:26 -06:00
|
|
|
import { TransactionStatus, VariableModel } from '../types';
|
2022-02-17 23:06:04 -06:00
|
|
|
import { toKeyedVariableIdentifier } from '../utils';
|
2020-11-27 03:20:57 -06:00
|
|
|
|
2022-04-22 08:33:13 -05:00
|
|
|
import { upgradeLegacyQueries } from './actions';
|
|
|
|
import { getPreloadedState } from './helpers';
|
|
|
|
import { toKeyedAction } from './keyedVariablesReducer';
|
|
|
|
import { changeVariableProp } from './sharedReducer';
|
|
|
|
|
2020-11-27 03:20:57 -06:00
|
|
|
interface Args {
|
|
|
|
query?: any;
|
|
|
|
variable?: VariableModel;
|
|
|
|
datasource?: any;
|
2022-01-14 04:19:42 -06:00
|
|
|
transactionStatus?: TransactionStatus;
|
2020-11-27 03:20:57 -06:00
|
|
|
}
|
2022-01-14 04:19:42 -06:00
|
|
|
function getTestContext({
|
|
|
|
query = '',
|
|
|
|
variable,
|
|
|
|
datasource,
|
|
|
|
transactionStatus = TransactionStatus.Fetching,
|
|
|
|
}: Args = {}) {
|
2022-02-17 23:06:04 -06:00
|
|
|
const key = 'key';
|
2020-11-27 03:20:57 -06:00
|
|
|
variable =
|
2021-11-03 07:14:22 -05:00
|
|
|
variable ??
|
|
|
|
queryBuilder()
|
|
|
|
.withId('query')
|
2022-02-17 23:06:04 -06:00
|
|
|
.withRootStateKey(key)
|
2021-11-03 07:14:22 -05:00
|
|
|
.withName('query')
|
|
|
|
.withQuery(query)
|
|
|
|
.withDatasource({ uid: 'test-data', type: 'test-data' })
|
|
|
|
.build();
|
2022-02-17 23:06:04 -06:00
|
|
|
const templatingState = {
|
|
|
|
transaction: { status: transactionStatus, uid: key, isDirty: false },
|
|
|
|
variables: {
|
|
|
|
[variable.id]: variable,
|
2020-11-27 03:20:57 -06:00
|
|
|
},
|
|
|
|
};
|
2022-02-17 23:06:04 -06:00
|
|
|
const state = getPreloadedState(key, templatingState);
|
2020-11-27 03:20:57 -06:00
|
|
|
datasource = datasource ?? {
|
|
|
|
name: 'TestData',
|
|
|
|
metricFindQuery: () => undefined,
|
|
|
|
variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined },
|
|
|
|
};
|
|
|
|
const get = jest.fn().mockResolvedValue(datasource);
|
|
|
|
const getDatasourceSrv = jest.fn().mockReturnValue({ get });
|
2022-02-17 23:06:04 -06:00
|
|
|
const identifier = toKeyedVariableIdentifier(variable);
|
2020-11-27 03:20:57 -06:00
|
|
|
|
2022-02-17 23:06:04 -06:00
|
|
|
return { key, state, get, getDatasourceSrv, identifier };
|
2020-11-27 03:20:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('upgradeLegacyQueries', () => {
|
|
|
|
describe('when called with a query variable for a standard variable supported data source that has not been upgraded', () => {
|
|
|
|
it('then it should dispatch changeVariableProp', async () => {
|
2022-02-17 23:06:04 -06:00
|
|
|
const { key, state, identifier, get, getDatasourceSrv } = getTestContext({ query: '*' });
|
2020-11-27 03:20:57 -06:00
|
|
|
|
|
|
|
const dispatchedActions = await thunkTester(state)
|
|
|
|
.givenThunk(upgradeLegacyQueries)
|
|
|
|
.whenThunkIsDispatched(identifier, getDatasourceSrv);
|
|
|
|
|
|
|
|
expect(dispatchedActions).toEqual([
|
2022-02-17 23:06:04 -06:00
|
|
|
toKeyedAction(
|
|
|
|
key,
|
|
|
|
changeVariableProp({
|
|
|
|
type: 'query',
|
|
|
|
id: 'query',
|
|
|
|
data: {
|
|
|
|
propName: 'query',
|
|
|
|
propValue: {
|
|
|
|
refId: 'TestData-query-Variable-Query',
|
|
|
|
query: '*',
|
|
|
|
},
|
2020-11-27 03:20:57 -06:00
|
|
|
},
|
2022-02-17 23:06:04 -06:00
|
|
|
})
|
|
|
|
),
|
2020-11-27 03:20:57 -06:00
|
|
|
]);
|
|
|
|
expect(get).toHaveBeenCalledTimes(1);
|
2021-11-03 07:14:22 -05:00
|
|
|
expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
|
2020-11-27 03:20:57 -06:00
|
|
|
});
|
2022-01-14 04:19:42 -06:00
|
|
|
|
|
|
|
describe('but there is no ongoing transaction', () => {
|
|
|
|
it('then it should not dispatch changeVariableProp', async () => {
|
|
|
|
const { state, identifier, get, getDatasourceSrv } = getTestContext({
|
|
|
|
query: '*',
|
|
|
|
transactionStatus: TransactionStatus.NotStarted,
|
|
|
|
});
|
|
|
|
|
|
|
|
const dispatchedActions = await thunkTester(state)
|
|
|
|
.givenThunk(upgradeLegacyQueries)
|
|
|
|
.whenThunkIsDispatched(identifier, getDatasourceSrv);
|
|
|
|
|
|
|
|
expect(dispatchedActions).toEqual([]);
|
|
|
|
expect(get).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
});
|
2020-11-27 03:20:57 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with a query variable for a standard variable supported data source that has been upgraded', () => {
|
|
|
|
it('then it should not dispatch any actions', async () => {
|
|
|
|
const { state, identifier, get, getDatasourceSrv } = getTestContext({ query: { refId: 'A' } });
|
|
|
|
|
|
|
|
const dispatchedActions = await thunkTester(state)
|
|
|
|
.givenThunk(upgradeLegacyQueries)
|
|
|
|
.whenThunkIsDispatched(identifier, getDatasourceSrv);
|
|
|
|
|
|
|
|
expect(dispatchedActions).toEqual([]);
|
|
|
|
expect(get).toHaveBeenCalledTimes(1);
|
2021-11-03 07:14:22 -05:00
|
|
|
expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
|
2020-11-27 03:20:57 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with a query variable for a legacy variable supported data source', () => {
|
|
|
|
it('then it should not dispatch any actions', async () => {
|
|
|
|
const datasource = {
|
|
|
|
name: 'TestData',
|
|
|
|
metricFindQuery: () => undefined,
|
|
|
|
};
|
|
|
|
const { state, identifier, get, getDatasourceSrv } = getTestContext({ datasource });
|
|
|
|
|
|
|
|
const dispatchedActions = await thunkTester(state)
|
|
|
|
.givenThunk(upgradeLegacyQueries)
|
|
|
|
.whenThunkIsDispatched(identifier, getDatasourceSrv);
|
|
|
|
|
|
|
|
expect(dispatchedActions).toEqual([]);
|
|
|
|
expect(get).toHaveBeenCalledTimes(1);
|
2021-11-03 07:14:22 -05:00
|
|
|
expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
|
2020-11-27 03:20:57 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with a query variable for a custom variable supported data source', () => {
|
|
|
|
it('then it should not dispatch any actions', async () => {
|
|
|
|
const datasource = {
|
|
|
|
name: 'TestData',
|
|
|
|
metricFindQuery: () => undefined,
|
|
|
|
variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor: {} },
|
|
|
|
};
|
|
|
|
const { state, identifier, get, getDatasourceSrv } = getTestContext({ datasource });
|
|
|
|
|
|
|
|
const dispatchedActions = await thunkTester(state)
|
|
|
|
.givenThunk(upgradeLegacyQueries)
|
|
|
|
.whenThunkIsDispatched(identifier, getDatasourceSrv);
|
|
|
|
|
|
|
|
expect(dispatchedActions).toEqual([]);
|
|
|
|
expect(get).toHaveBeenCalledTimes(1);
|
2021-11-03 07:14:22 -05:00
|
|
|
expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
|
2020-11-27 03:20:57 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with a query variable for a datasource variable supported data source', () => {
|
|
|
|
it('then it should not dispatch any actions', async () => {
|
|
|
|
const datasource = {
|
|
|
|
name: 'TestData',
|
|
|
|
metricFindQuery: () => undefined,
|
|
|
|
variables: { getType: () => VariableSupportType.Datasource },
|
|
|
|
};
|
|
|
|
const { state, identifier, get, getDatasourceSrv } = getTestContext({ datasource });
|
|
|
|
|
|
|
|
const dispatchedActions = await thunkTester(state)
|
|
|
|
.givenThunk(upgradeLegacyQueries)
|
|
|
|
.whenThunkIsDispatched(identifier, getDatasourceSrv);
|
|
|
|
|
|
|
|
expect(dispatchedActions).toEqual([]);
|
|
|
|
expect(get).toHaveBeenCalledTimes(1);
|
2021-11-03 07:14:22 -05:00
|
|
|
expect(get).toHaveBeenCalledWith({ uid: 'test-data', type: 'test-data' });
|
2020-11-27 03:20:57 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when called with a custom variable', () => {
|
|
|
|
it('then it should not dispatch any actions', async () => {
|
2022-02-17 23:06:04 -06:00
|
|
|
const variable = customBuilder().withId('custom').withRootStateKey('key').withName('custom').build();
|
2020-11-27 03:20:57 -06:00
|
|
|
const { state, identifier, get, getDatasourceSrv } = getTestContext({ variable });
|
|
|
|
|
|
|
|
const dispatchedActions = await thunkTester(state)
|
|
|
|
.givenThunk(upgradeLegacyQueries)
|
|
|
|
.whenThunkIsDispatched(identifier, getDatasourceSrv);
|
|
|
|
|
|
|
|
expect(dispatchedActions).toEqual([]);
|
|
|
|
expect(get).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|