123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- import { cloneDeep } from 'lodash';
- import { LoadingState, VariableType } from '@grafana/data';
- import { reducerTester } from '../../../../test/core/redux/reducerTester';
- import { variableAdapters } from '../adapters';
- import { createConstantVariableAdapter } from '../constant/adapter';
- import { initialConstantVariableModelState } from '../constant/reducer';
- import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../constants';
- import { changeVariableNameSucceeded } from '../editor/reducer';
- import { createQueryVariableAdapter } from '../query/adapter';
- import { initialQueryVariableModelState } from '../query/reducer';
- import { ConstantVariableModel, QueryVariableModel, VariableHide, VariableOption } from '../types';
- import { toVariablePayload } from '../utils';
- import { getVariableState, getVariableTestContext } from './helpers';
- import {
- addVariable,
- changeVariableOrder,
- changeVariableProp,
- changeVariableType,
- duplicateVariable,
- removeVariable,
- setCurrentVariableValue,
- sharedReducer,
- variableStateCompleted,
- variableStateFailed,
- variableStateFetching,
- variableStateNotStarted,
- } from './sharedReducer';
- import { initialVariablesState, KeyedVariableIdentifier, VariablesState } from './types';
- variableAdapters.setInit(() => [createQueryVariableAdapter(), createConstantVariableAdapter()]);
- describe('sharedReducer', () => {
- describe('when addVariable is dispatched', () => {
- it('then state should be correct', () => {
- const model: any = {
- name: 'name from model',
- type: 'type from model',
- current: undefined,
- };
- const expected: QueryVariableModel = {
- ...initialQueryVariableModelState,
- id: 'name from model',
- global: true,
- index: 0,
- name: 'name from model',
- type: 'type from model' as unknown as VariableType,
- current: {} as unknown as VariableOption,
- };
- const payload = toVariablePayload({ id: 'name from model', type: 'query' }, { global: true, index: 0, model });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, { ...initialVariablesState })
- .whenActionIsDispatched(addVariable(payload))
- .thenStateShouldEqual({
- ['name from model']: expected,
- });
- });
- });
- describe('when addVariable is dispatched for a constant model', () => {
- it('then state should be correct', () => {
- const model: any = {
- name: 'constant',
- type: 'constant',
- query: 'a constant',
- current: { selected: true, text: 'A', value: 'A' },
- options: [{ selected: true, text: 'A', value: 'A' }],
- };
- const expected: ConstantVariableModel = {
- ...initialConstantVariableModelState,
- id: 'constant',
- global: true,
- index: 0,
- name: 'constant',
- type: 'constant',
- query: 'a constant',
- current: { selected: true, text: 'a constant', value: 'a constant' },
- options: [{ selected: true, text: 'a constant', value: 'a constant' }],
- };
- const payload = toVariablePayload({ id: 'constant', type: 'constant' }, { global: true, index: 0, model });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, { ...initialVariablesState })
- .whenActionIsDispatched(addVariable(payload))
- .thenStateShouldEqual({
- ['constant']: expected,
- });
- });
- });
- describe('when removeVariable is dispatched and reIndex is true', () => {
- it('then state should be correct', () => {
- const initialState: VariablesState = getVariableState(3);
- const payload = toVariablePayload({ id: '1', type: 'query' }, { reIndex: true });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, initialState)
- .whenActionIsDispatched(removeVariable(payload))
- .thenStateShouldEqual({
- '0': {
- id: '0',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-0',
- hide: VariableHide.dontHide,
- index: 0,
- label: 'Label-0',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '2': {
- id: '2',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-2',
- hide: VariableHide.dontHide,
- index: 1,
- label: 'Label-2',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- });
- });
- });
- describe('when removeVariable is dispatched and reIndex is false', () => {
- it('then state should be correct', () => {
- const initialState: VariablesState = getVariableState(3);
- const payload = toVariablePayload({ id: '1', type: 'query' }, { reIndex: false });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, initialState)
- .whenActionIsDispatched(removeVariable(payload))
- .thenStateShouldEqual({
- '0': {
- id: '0',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-0',
- hide: VariableHide.dontHide,
- index: 0,
- label: 'Label-0',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '2': {
- id: '2',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-2',
- hide: VariableHide.dontHide,
- index: 2,
- label: 'Label-2',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- });
- });
- });
- describe('when duplicateVariable is dispatched', () => {
- it('then state should be correct', () => {
- const initialState: VariablesState = getVariableState(3, -1, false, true);
- const payload = toVariablePayload({ id: '1', type: 'query' }, { newId: '11' });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, initialState)
- .whenActionIsDispatched(duplicateVariable(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- id: '0',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-0',
- hide: VariableHide.dontHide,
- index: 0,
- label: 'Label-0',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '1': {
- id: '1',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-1',
- hide: VariableHide.dontHide,
- index: 1,
- label: 'Label-1',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '2': {
- id: '2',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-2',
- hide: VariableHide.dontHide,
- index: 2,
- label: 'Label-2',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '11': {
- ...initialQueryVariableModelState,
- id: '11',
- rootStateKey: 'key',
- name: 'copy_of_Name-1',
- index: 3,
- label: 'Label-1',
- },
- });
- });
- });
- describe('when changeVariableOrder is dispatched', () => {
- it('then state should be correct', () => {
- const initialState: VariablesState = getVariableState(3);
- const payload = toVariablePayload({ id: '2', type: 'query' }, { fromIndex: 2, toIndex: 0 });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, initialState)
- .whenActionIsDispatched(changeVariableOrder(payload))
- .thenStateShouldEqual({
- '0': {
- id: '0',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-0',
- hide: VariableHide.dontHide,
- index: 1,
- label: 'Label-0',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '1': {
- id: '1',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-1',
- hide: VariableHide.dontHide,
- index: 2,
- label: 'Label-1',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '2': {
- id: '2',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-2',
- hide: VariableHide.dontHide,
- index: 0,
- label: 'Label-2',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- });
- });
- it('then state should be correct', () => {
- const initialState: VariablesState = getVariableState(3);
- const payload = toVariablePayload({ id: '0', type: 'query' }, { fromIndex: 0, toIndex: 2 });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, initialState)
- .whenActionIsDispatched(changeVariableOrder(payload))
- .thenStateShouldEqual({
- '0': {
- id: '0',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-0',
- hide: VariableHide.dontHide,
- index: 2,
- label: 'Label-0',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '1': {
- id: '1',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-1',
- hide: VariableHide.dontHide,
- index: 0,
- label: 'Label-1',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- '2': {
- id: '2',
- rootStateKey: 'key',
- type: 'query',
- name: 'Name-2',
- hide: VariableHide.dontHide,
- index: 1,
- label: 'Label-2',
- skipUrlSync: false,
- global: false,
- state: LoadingState.NotStarted,
- error: null,
- description: null,
- },
- });
- });
- });
- describe('when setCurrentVariableValue is dispatched and current.text is an Array with values', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter, {
- options: [
- { text: 'All', value: '$__all', selected: false },
- { text: 'A', value: 'A', selected: false },
- { text: 'B', value: 'B', selected: false },
- ],
- });
- const current = { text: ['A', 'B'], selected: true, value: ['A', 'B'] };
- const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(setCurrentVariableValue(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- options: [
- { selected: false, text: 'All', value: '$__all' },
- { selected: true, text: 'A', value: 'A' },
- { selected: true, text: 'B', value: 'B' },
- ],
- current: { selected: true, text: ['A', 'B'], value: ['A', 'B'] },
- } as unknown as QueryVariableModel,
- });
- });
- });
- describe('when setCurrentVariableValue is dispatched and current.value is an Array with values except All value', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter, {
- options: [
- { text: 'All', value: '$__all', selected: false },
- { text: 'A', value: 'A', selected: false },
- { text: 'B', value: 'B', selected: false },
- ],
- });
- const current = { text: 'A + B', selected: true, value: ['A', 'B'] };
- const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(setCurrentVariableValue(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- options: [
- { selected: false, text: 'All', value: '$__all' },
- { selected: true, text: 'A', value: 'A' },
- { selected: true, text: 'B', value: 'B' },
- ],
- current: { selected: true, text: 'A + B', value: ['A', 'B'] },
- } as unknown as QueryVariableModel,
- });
- });
- });
- describe('when setCurrentVariableValue is dispatched and current.value is an Array with values containing All value', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter, {
- options: [
- { text: 'All', value: '$__all', selected: false },
- { text: 'A', value: 'A', selected: false },
- { text: 'B', value: 'B', selected: false },
- ],
- });
- const current = { text: ALL_VARIABLE_TEXT, selected: true, value: [ALL_VARIABLE_VALUE] };
- const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(setCurrentVariableValue(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- options: [
- { selected: true, text: 'All', value: '$__all' },
- { selected: false, text: 'A', value: 'A' },
- { selected: false, text: 'B', value: 'B' },
- ],
- current: { selected: true, text: 'All', value: ['$__all'] },
- } as unknown as QueryVariableModel,
- });
- });
- });
- describe('when variableStateNotStarted is dispatched', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter, {
- state: LoadingState.Done,
- error: 'Some error',
- });
- const payload = toVariablePayload({ id: '0', type: 'query' });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(variableStateNotStarted(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- state: LoadingState.NotStarted,
- error: null,
- } as unknown as QueryVariableModel,
- });
- });
- });
- describe('when variableStateFetching is dispatched', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter, {
- state: LoadingState.Done,
- error: 'Some error',
- });
- const payload = toVariablePayload({ id: '0', type: 'query' });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(variableStateFetching(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- state: LoadingState.Loading,
- error: null,
- } as unknown as QueryVariableModel,
- });
- });
- });
- describe('when variableStateCompleted is dispatched', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter, {
- state: LoadingState.Loading,
- error: 'Some error',
- });
- const payload = toVariablePayload({ id: '0', type: 'query' });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(variableStateCompleted(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- state: LoadingState.Done,
- error: null,
- } as unknown as QueryVariableModel,
- });
- });
- });
- describe('when variableStateFailed is dispatched', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter, { state: LoadingState.Loading });
- const payload = toVariablePayload({ id: '0', type: 'query' }, { error: 'Some error' });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(variableStateFailed(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- state: LoadingState.Error,
- error: 'Some error',
- } as unknown as QueryVariableModel,
- });
- });
- });
- describe('when changeVariableProp is dispatched', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter);
- const propName = 'label';
- const propValue = 'Updated label';
- const payload = toVariablePayload({ id: '0', type: 'query' }, { propName, propValue });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(changeVariableProp(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- label: 'Updated label',
- },
- });
- });
- });
- describe('when changeVariableNameSucceeded is dispatched', () => {
- it('then state should be correct', () => {
- const adapter = createQueryVariableAdapter();
- const { initialState } = getVariableTestContext(adapter);
- const newName = 'A new name';
- const payload = toVariablePayload({ id: '0', type: 'query' }, { newName });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(initialState))
- .whenActionIsDispatched(changeVariableNameSucceeded(payload))
- .thenStateShouldEqual({
- ...initialState,
- '0': {
- ...initialState[0],
- name: 'A new name',
- },
- });
- });
- });
- describe('when changeVariableType is dispatched', () => {
- it('then state should be correct', () => {
- const queryAdapter = createQueryVariableAdapter();
- const { initialState: queryAdapterState } = getVariableTestContext(queryAdapter);
- const constantAdapter = createConstantVariableAdapter();
- const { initialState: constantAdapterState } = getVariableTestContext(constantAdapter);
- const newType = 'constant' as VariableType;
- const identifier: KeyedVariableIdentifier = { id: '0', type: 'query', rootStateKey: 'key' };
- const payload = toVariablePayload(identifier, { newType });
- reducerTester<VariablesState>()
- .givenReducer(sharedReducer, cloneDeep(queryAdapterState))
- .whenActionIsDispatched(changeVariableNameSucceeded(toVariablePayload(identifier, { newName: 'test' })))
- .whenActionIsDispatched(
- changeVariableProp(toVariablePayload(identifier, { propName: 'description', propValue: 'new description' }))
- )
- .whenActionIsDispatched(
- changeVariableProp(toVariablePayload(identifier, { propName: 'label', propValue: 'new label' }))
- )
- .whenActionIsDispatched(changeVariableType(payload))
- .thenStateShouldEqual({
- ...constantAdapterState,
- '0': {
- ...constantAdapterState[0],
- rootStateKey: 'key',
- name: 'test',
- description: 'new description',
- label: 'new label',
- type: 'constant',
- },
- });
- });
- });
- });
|