mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
dbec2b02fd
* Variables: move state tree into a keyed state * Update public/app/features/variables/state/transactionReducer.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> * Chore: fix prettier error * Chore: renamed slices and lastUid * Chore: rename toUidAction * Chore: rename dashboardVariableReducer * Chore: rename state prop back to templating * Chore renames variable.dashboardUid * Chore: rename toDashboardVariableIdentifier * Chore: rename getDashboardVariable * Chore: rename getDashboardVariablesState * Chore: rename getDashboardVariables * Chore: some more renames * Chore: small clean up * Chore: small rename * Chore: removes unused function * Chore: rename VariableModel.stateKey * Chore: rename KeyedVariableIdentifier.stateKey * user essentials mob! 🔱 * user essentials mob! 🔱 * user essentials mob! 🔱 * user essentials mob! 🔱 Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> Co-authored-by: kay delaney <kay@grafana.com> Co-authored-by: Alexandra Vargas <alexa1866@gmail.com> Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
117 lines
4.1 KiB
TypeScript
117 lines
4.1 KiB
TypeScript
import React from 'react';
|
|
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { DataSourceApi } from '@grafana/data';
|
|
|
|
import { Props, QueryVariableEditorUnConnected } from './QueryVariableEditor';
|
|
import { initialQueryVariableModelState } from './reducer';
|
|
import { describe, expect } from '../../../../test/lib/common';
|
|
import { LegacyVariableQueryEditor } from '../editor/LegacyVariableQueryEditor';
|
|
import { mockDataSource } from 'app/features/alerting/unified/mocks';
|
|
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
|
|
import { NEW_VARIABLE_ID } from '../constants';
|
|
import { VariableModel } from '../types';
|
|
import { KeyedVariableIdentifier } from '../state/types';
|
|
|
|
const setupTestContext = (options: Partial<Props>) => {
|
|
const variableDefaults: Partial<VariableModel> = { rootStateKey: 'key' };
|
|
const extended = {
|
|
VariableQueryEditor: LegacyVariableQueryEditor,
|
|
dataSource: {} as unknown as DataSourceApi,
|
|
};
|
|
const defaults: Props = {
|
|
variable: { ...initialQueryVariableModelState, ...variableDefaults },
|
|
initQueryVariableEditor: jest.fn(),
|
|
changeQueryVariableDataSource: jest.fn(),
|
|
changeQueryVariableQuery: jest.fn(),
|
|
changeVariableMultiValue: jest.fn(),
|
|
extended,
|
|
onPropChange: jest.fn(),
|
|
};
|
|
|
|
const props: Props & Record<string, any> = { ...defaults, ...options };
|
|
const { rerender } = render(<QueryVariableEditorUnConnected {...props} />);
|
|
|
|
return { rerender, props };
|
|
};
|
|
|
|
const mockDS = mockDataSource({
|
|
name: 'CloudManager',
|
|
type: DataSourceType.Alertmanager,
|
|
});
|
|
|
|
jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => {
|
|
return {
|
|
getDataSourceSrv: () => ({
|
|
get: () => Promise.resolve(mockDS),
|
|
getList: () => [mockDS],
|
|
getInstanceSettings: () => mockDS,
|
|
}),
|
|
};
|
|
});
|
|
|
|
const defaultIdentifier: KeyedVariableIdentifier = { type: 'query', rootStateKey: 'key', id: NEW_VARIABLE_ID };
|
|
|
|
describe('QueryVariableEditor', () => {
|
|
describe('when the component is mounted', () => {
|
|
it('then it should call initQueryVariableEditor', () => {
|
|
const { props } = setupTestContext({});
|
|
|
|
expect(props.initQueryVariableEditor).toHaveBeenCalledTimes(1);
|
|
expect(props.initQueryVariableEditor).toHaveBeenCalledWith(defaultIdentifier);
|
|
});
|
|
});
|
|
|
|
describe('when the user changes', () => {
|
|
it.each`
|
|
fieldName | propName | expectedArgs
|
|
${'query'} | ${'changeQueryVariableQuery'} | ${[defaultIdentifier, 't', 't']}
|
|
${'regex'} | ${'onPropChange'} | ${[{ propName: 'regex', propValue: 't', updateOptions: true }]}
|
|
`(
|
|
'$fieldName field and tabs away then $propName should be called with correct args',
|
|
({ fieldName, propName, expectedArgs }) => {
|
|
const { props } = setupTestContext({});
|
|
const propUnderTest = props[propName];
|
|
const fieldAccessor = fieldAccessors[fieldName];
|
|
|
|
userEvent.type(fieldAccessor(), 't');
|
|
userEvent.tab();
|
|
|
|
expect(propUnderTest).toHaveBeenCalledTimes(1);
|
|
expect(propUnderTest).toHaveBeenCalledWith(...expectedArgs);
|
|
}
|
|
);
|
|
});
|
|
|
|
describe('when the user changes', () => {
|
|
it.each`
|
|
fieldName | propName
|
|
${'query'} | ${'changeQueryVariableQuery'}
|
|
${'regex'} | ${'onPropChange'}
|
|
`(
|
|
'$fieldName field but reverts the change and tabs away then $propName should not be called',
|
|
({ fieldName, propName }) => {
|
|
const { props } = setupTestContext({});
|
|
const propUnderTest = props[propName];
|
|
const fieldAccessor = fieldAccessors[fieldName];
|
|
|
|
userEvent.type(fieldAccessor(), 't');
|
|
userEvent.type(fieldAccessor(), '{backspace}');
|
|
userEvent.tab();
|
|
|
|
expect(propUnderTest).not.toHaveBeenCalled();
|
|
}
|
|
);
|
|
});
|
|
});
|
|
|
|
const getQueryField = () =>
|
|
screen.getByRole('textbox', { name: /variable editor form default variable query editor textarea/i });
|
|
|
|
const getRegExField = () => screen.getByLabelText('Regex');
|
|
|
|
const fieldAccessors: Record<string, () => HTMLElement> = {
|
|
query: getQueryField,
|
|
regex: getRegExField,
|
|
};
|