grafana/public/app/features/variables/state/reducers.test.ts
Hugo Häggmark 845bc7c444
Variables: Adds loading state and indicators (#27917)
* Refactor: Replaces initLock with state machine

* Refactor: removes some states for now

* Refactor: adds loading state in OptionsPicker

* Refactor: major refactor of load state

* Refactor: fixes updating graph in parallell

* Refactor: moves error handling to updateOptions

* Refactor: fixes the last cases

* Tests: disables variable e2e again

* Chore: removes nova config

* Refactor: small changes when going through the code again

* Refactor: fixes typings

* Refactor: changes after PR comments

* Refactor: split up onTimeRangeUpdated and fixed some error handling

* Tests: removes unused func

* Tests: fixes typing
2020-10-02 07:02:06 +02:00

163 lines
5.4 KiB
TypeScript

import { reducerTester } from '../../../../test/core/redux/reducerTester';
import { initialVariableModelState, QueryVariableModel } from '../types';
import { VariableAdapter, variableAdapters } from '../adapters';
import { createAction } from '@reduxjs/toolkit';
import { cleanVariables, variablesReducer, VariablesState } from './variablesReducer';
import { toVariablePayload, VariablePayload } from './types';
import { VariableType } from '@grafana/data';
const variableAdapter: VariableAdapter<QueryVariableModel> = {
id: ('mock' as unknown) as VariableType,
name: 'Mock label',
description: 'Mock description',
dependsOn: jest.fn(),
updateOptions: jest.fn(),
initialState: {} as QueryVariableModel,
reducer: jest.fn().mockReturnValue({}),
getValueForUrl: jest.fn(),
getSaveModel: jest.fn(),
picker: null as any,
editor: null as any,
setValue: jest.fn(),
setValueFromUrl: jest.fn(),
};
variableAdapters.setInit(() => [{ ...variableAdapter }]);
describe('variablesReducer', () => {
describe('when cleanUpDashboard is dispatched', () => {
it('then all variables except global variables should be removed', () => {
const initialState: VariablesState = {
'0': {
...initialVariableModelState,
id: '0',
index: 0,
type: 'query',
name: 'Name-0',
label: 'Label-0',
},
'1': {
...initialVariableModelState,
id: '1',
index: 1,
type: 'query',
name: 'Name-1',
label: 'Label-1',
global: true,
},
'2': {
...initialVariableModelState,
id: '2',
index: 2,
type: 'query',
name: 'Name-2',
label: 'Label-2',
},
'3': {
...initialVariableModelState,
id: '3',
index: 3,
type: 'query',
name: 'Name-3',
label: 'Label-3',
global: true,
},
};
reducerTester<VariablesState>()
.givenReducer(variablesReducer, initialState)
.whenActionIsDispatched(cleanVariables())
.thenStateShouldEqual({
'1': {
...initialVariableModelState,
id: '1',
index: 1,
type: 'query',
name: 'Name-1',
label: 'Label-1',
global: true,
},
'3': {
...initialVariableModelState,
id: '3',
index: 3,
type: 'query',
name: 'Name-3',
label: 'Label-3',
global: true,
},
});
});
});
describe('when any action is dispatched with a type prop that is registered in variableAdapters', () => {
it('then the reducer for that variableAdapter should be invoked', () => {
const initialState: VariablesState = {
'0': {
...initialVariableModelState,
id: '0',
index: 0,
type: 'query',
name: 'Name-0',
label: 'Label-0',
},
};
variableAdapters.get('mock').reducer = jest.fn().mockReturnValue(initialState);
const mockAction = createAction<VariablePayload>('mockAction');
reducerTester<VariablesState>()
.givenReducer(variablesReducer, initialState)
.whenActionIsDispatched(mockAction(toVariablePayload({ type: ('mock' as unknown) as VariableType, id: '0' })))
.thenStateShouldEqual(initialState);
expect(variableAdapters.get('mock').reducer).toHaveBeenCalledTimes(1);
expect(variableAdapters.get('mock').reducer).toHaveBeenCalledWith(
initialState,
mockAction(toVariablePayload({ type: ('mock' as unknown) as VariableType, id: '0' }))
);
});
});
describe('when any action is dispatched with a type prop that is not registered in variableAdapters', () => {
it('then the reducer for that variableAdapter should be invoked', () => {
const initialState: VariablesState = {
'0': {
...initialVariableModelState,
id: '0',
index: 0,
type: 'query',
name: 'Name-0',
label: 'Label-0',
},
};
variableAdapters.get('mock').reducer = jest.fn().mockReturnValue(initialState);
const mockAction = createAction<VariablePayload>('mockAction');
reducerTester<VariablesState>()
.givenReducer(variablesReducer, initialState)
.whenActionIsDispatched(mockAction(toVariablePayload({ type: 'adhoc', id: '0' })))
.thenStateShouldEqual(initialState);
expect(variableAdapters.get('mock').reducer).toHaveBeenCalledTimes(0);
});
});
describe('when any action is dispatched missing type prop', () => {
it('then the reducer for that variableAdapter should be invoked', () => {
const initialState: VariablesState = {
'0': {
...initialVariableModelState,
id: '0',
index: 0,
type: 'query',
name: 'Name-0',
label: 'Label-0',
},
};
variableAdapters.get('mock').reducer = jest.fn().mockReturnValue(initialState);
const mockAction = createAction<string>('mockAction');
reducerTester<VariablesState>()
.givenReducer(variablesReducer, initialState)
.whenActionIsDispatched(mockAction('mocked'))
.thenStateShouldEqual(initialState);
expect(variableAdapters.get('mock').reducer).toHaveBeenCalledTimes(0);
});
});
});