grafana/public/app/features/variables/textbox/reducer.test.ts
Hugo Häggmark 1db067396a
Variables: migrates data source variable type to React/Redux (#22770)
* Refactor: moves all the newVariables part to features/variables directory

* Feature: adds datasource type

* Tests: adds reducer tests

* Tests: covers data source actions with tests

* Chore: reduces strict null errors
2020-03-16 06:32:04 +01:00

73 lines
2.4 KiB
TypeScript

import { reducerTester } from '../../../../test/core/redux/reducerTester';
import cloneDeep from 'lodash/cloneDeep';
import { getVariableTestContext } from '../state/helpers';
import { toVariablePayload } from '../state/types';
import { createTextBoxOptions, textBoxVariableReducer } from './reducer';
import { VariablesState } from '../state/variablesReducer';
import { TextBoxVariableModel } from '../../templating/variable';
import { createTextBoxVariableAdapter } from './adapter';
describe('textBoxVariableReducer', () => {
const adapter = createTextBoxVariableAdapter();
describe('when createTextBoxOptions is dispatched', () => {
it('then state should be correct', () => {
const query = 'ABC';
const uuid = '0';
const { initialState } = getVariableTestContext(adapter, { uuid, query });
const payload = toVariablePayload({ uuid: '0', type: 'textbox' });
reducerTester<VariablesState>()
.givenReducer(textBoxVariableReducer, cloneDeep(initialState))
.whenActionIsDispatched(createTextBoxOptions(payload))
.thenStateShouldEqual({
[uuid]: {
...initialState[uuid],
options: [
{
text: query,
value: query,
selected: false,
},
],
current: {
text: query,
value: query,
selected: false,
},
} as TextBoxVariableModel,
});
});
});
describe('when createTextBoxOptions is dispatched and query contains spaces', () => {
it('then state should be correct', () => {
const query = ' ABC ';
const uuid = '0';
const { initialState } = getVariableTestContext(adapter, { uuid, query });
const payload = toVariablePayload({ uuid: '0', type: 'textbox' });
reducerTester<VariablesState>()
.givenReducer(textBoxVariableReducer, cloneDeep(initialState))
.whenActionIsDispatched(createTextBoxOptions(payload))
.thenStateShouldEqual({
[uuid]: {
...initialState[uuid],
options: [
{
text: query.trim(),
value: query.trim(),
selected: false,
},
],
current: {
text: query.trim(),
value: query.trim(),
selected: false,
},
} as TextBoxVariableModel,
});
});
});
});