Files
Hugo Häggmark dbec2b02fd Variables: move state tree into a keyed state (#44642)
* 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>
2022-02-18 06:06:04 +01:00

124 lines
4.8 KiB
TypeScript

import { cloneDeep } from 'lodash';
import { getVariableTestContext } from '../state/helpers';
import { VariablesState } from '../state/types';
import { createIntervalVariableAdapter } from './adapter';
import { IntervalVariableModel } from '../types';
import { reducerTester } from '../../../../test/core/redux/reducerTester';
import { createIntervalOptions, intervalVariableReducer } from './reducer';
import { toVariablePayload } from '../utils';
describe('intervalVariableReducer', () => {
const adapter = createIntervalVariableAdapter();
describe('when createIntervalOptions is dispatched', () => {
describe('and auto is false', () => {
it('then state should be correct', () => {
const id = '0';
const query = '1s,1m,1h,1d';
const auto = false;
const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
const payload = toVariablePayload({ id: '0', type: 'interval' });
reducerTester<VariablesState>()
.givenReducer(intervalVariableReducer, cloneDeep(initialState))
.whenActionIsDispatched(createIntervalOptions(payload))
.thenStateShouldEqual({
'0': {
...initialState['0'],
id: '0',
query: '1s,1m,1h,1d',
auto: false,
options: [
{ text: '1s', value: '1s', selected: false },
{ text: '1m', value: '1m', selected: false },
{ text: '1h', value: '1h', selected: false },
{ text: '1d', value: '1d', selected: false },
],
} as IntervalVariableModel,
});
});
});
describe('and auto is true', () => {
it('then state should be correct', () => {
const id = '0';
const query = '1s,1m,1h,1d';
const auto = true;
const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
const payload = toVariablePayload({ id: '0', type: 'interval' });
reducerTester<VariablesState>()
.givenReducer(intervalVariableReducer, cloneDeep(initialState))
.whenActionIsDispatched(createIntervalOptions(payload))
.thenStateShouldEqual({
'0': {
...initialState['0'],
id: '0',
query: '1s,1m,1h,1d',
auto: true,
options: [
{ text: 'auto', value: '$__auto_interval_0', selected: false },
{ text: '1s', value: '1s', selected: false },
{ text: '1m', value: '1m', selected: false },
{ text: '1h', value: '1h', selected: false },
{ text: '1d', value: '1d', selected: false },
],
} as IntervalVariableModel,
});
});
});
describe('and query contains "', () => {
it('then state should be correct', () => {
const id = '0';
const query = '"kalle, anka","donald, duck"';
const auto = false;
const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
const payload = toVariablePayload({ id: '0', type: 'interval' });
reducerTester<VariablesState>()
.givenReducer(intervalVariableReducer, cloneDeep(initialState))
.whenActionIsDispatched(createIntervalOptions(payload))
.thenStateShouldEqual({
'0': {
...initialState['0'],
id: '0',
query: '"kalle, anka","donald, duck"',
auto: false,
options: [
{ text: 'kalle, anka', value: 'kalle, anka', selected: false },
{ text: 'donald, duck', value: 'donald, duck', selected: false },
],
} as IntervalVariableModel,
});
});
});
describe("and query contains '", () => {
it('then state should be correct', () => {
const id = '0';
const query = "'kalle, anka','donald, duck'";
const auto = false;
const { initialState } = getVariableTestContext<IntervalVariableModel>(adapter, { id, query, auto });
const payload = toVariablePayload({ id: '0', type: 'interval' });
reducerTester<VariablesState>()
.givenReducer(intervalVariableReducer, cloneDeep(initialState))
.whenActionIsDispatched(createIntervalOptions(payload))
.thenStateShouldEqual({
'0': {
...initialState['0'],
id: '0',
query: "'kalle, anka','donald, duck'",
auto: false,
options: [
{ text: 'kalle, anka', value: 'kalle, anka', selected: false },
{ text: 'donald, duck', value: 'donald, duck', selected: false },
],
} as IntervalVariableModel,
});
});
});
});
});