mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 02:23:31 -06:00
* refactor: move utility functions out of the redux actions * refactor: move password handlers to the feature root * refactor: move API related functions to an api.ts * refactor: move components under a /components folder * refactor: move page containers under a /pages folder and extract components * refactor: update mocks to be easier to reuse * refactor: move tests into a state/tests/ subfolder * refactor: expose 'initialState' for plugins * refactor: move generic types to the root folder of the feature * refactor: import path fixe * refactor: update import paths for app routes * chore: update betterer * refactor: fix type errors due to changed mock functions * chore: fix mocking context_srv in tests * refactor: udpate imports to be more concise * fix: update failing test because of mocks * refactor: use the new `navId` prop where we can * fix: use UID instead ID in datasource edit links * fix:clean up Redux state when unmounting the edit page * refactor: use `uid` instead of `id` * refactor: always fetch the plugin details when editing a datasource The deleted lines could provide performance benefits, although they also make the implementation more prone to errors. (Mostly because we are storing the information about the currently loaded plugin in a single field, and it was not validating if it is for the latest one). We are planning to introduce some kind of caching, but first we would like to clean up the underlying state a bit (plugins & datasources. * fix: add missing dispatch() wrapper for update datasource callback * refactor: prefer using absolute import paths Co-authored-by: Ryan McKinley <ryantxu@gmail.com> * fix: ESLINT import order issue * refactor: put test files next to their files * refactor: use implicit return types for components * fix: remove caching from datasource fetching I have introduced a cache to only fetch data-sources once, however as we are missing a good logic for updating the instances in the Redux store when they change (create, update, delete), this approach is not keeping the UI in sync. Due to this reason I have removed the caching for now, and will reintroduce it once we have a more robust client-side state logic. Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
181 lines
6.5 KiB
TypeScript
181 lines
6.5 KiB
TypeScript
import { reducerTester } from 'test/core/redux/reducerTester';
|
|
|
|
import { PluginMeta, PluginMetaInfo, PluginType, LayoutModes } from '@grafana/data';
|
|
import { DataSourceSettingsState, DataSourcesState } from 'app/types';
|
|
|
|
import { getMockDataSource, getMockDataSources } from '../__mocks__';
|
|
import { GenericDataSourcePlugin } from '../types';
|
|
|
|
import {
|
|
dataSourceLoaded,
|
|
dataSourceMetaLoaded,
|
|
dataSourcePluginsLoad,
|
|
dataSourcePluginsLoaded,
|
|
dataSourceSettingsReducer,
|
|
dataSourcesLoaded,
|
|
dataSourcesReducer,
|
|
initDataSourceSettingsFailed,
|
|
initDataSourceSettingsSucceeded,
|
|
initialDataSourceSettingsState,
|
|
initialState,
|
|
setDataSourceName,
|
|
setDataSourcesLayoutMode,
|
|
setDataSourcesSearchQuery,
|
|
setDataSourceTypeSearchQuery,
|
|
setIsDefault,
|
|
} from './reducers';
|
|
|
|
const mockPlugin = () =>
|
|
({
|
|
defaultNavUrl: 'defaultNavUrl',
|
|
enabled: true,
|
|
hasUpdate: true,
|
|
id: 'id',
|
|
info: {} as PluginMetaInfo,
|
|
latestVersion: 'latestVersion',
|
|
name: 'name',
|
|
pinned: true,
|
|
type: PluginType.datasource,
|
|
module: 'path/to/module',
|
|
} as PluginMeta);
|
|
|
|
describe('dataSourcesReducer', () => {
|
|
describe('when dataSourcesLoaded is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
const dataSources = getMockDataSources(1);
|
|
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(dataSourcesLoaded(dataSources))
|
|
.thenStateShouldEqual({ ...initialState, hasFetched: true, dataSources, dataSourcesCount: 1 });
|
|
});
|
|
});
|
|
|
|
describe('when dataSourceLoaded is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
const dataSource = getMockDataSource<{}>();
|
|
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(dataSourceLoaded(dataSource))
|
|
.thenStateShouldEqual({ ...initialState, dataSource });
|
|
});
|
|
});
|
|
|
|
describe('when setDataSourcesSearchQuery is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(setDataSourcesSearchQuery('some query'))
|
|
.thenStateShouldEqual({ ...initialState, searchQuery: 'some query' });
|
|
});
|
|
});
|
|
|
|
describe('when setDataSourcesLayoutMode is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
const layoutMode: LayoutModes = LayoutModes.Grid;
|
|
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(setDataSourcesLayoutMode(layoutMode))
|
|
.thenStateShouldEqual({ ...initialState, layoutMode: LayoutModes.Grid });
|
|
});
|
|
});
|
|
|
|
describe('when dataSourcePluginsLoad is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
const state: DataSourcesState = { ...initialState, plugins: [mockPlugin()] };
|
|
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, state)
|
|
.whenActionIsDispatched(dataSourcePluginsLoad())
|
|
.thenStateShouldEqual({ ...initialState, isLoadingDataSources: true });
|
|
});
|
|
});
|
|
|
|
describe('when dataSourcePluginsLoaded is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
const dataSourceTypes = [mockPlugin()];
|
|
const state: DataSourcesState = { ...initialState, isLoadingDataSources: true };
|
|
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, state)
|
|
.whenActionIsDispatched(dataSourcePluginsLoaded({ plugins: dataSourceTypes, categories: [] }))
|
|
.thenStateShouldEqual({ ...initialState, plugins: dataSourceTypes, isLoadingDataSources: false });
|
|
});
|
|
});
|
|
|
|
describe('when setDataSourceTypeSearchQuery is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(setDataSourceTypeSearchQuery('type search query'))
|
|
.thenStateShouldEqual({ ...initialState, dataSourceTypeSearchQuery: 'type search query' });
|
|
});
|
|
});
|
|
|
|
describe('when dataSourceMetaLoaded is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
const dataSourceMeta = mockPlugin();
|
|
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(dataSourceMetaLoaded(dataSourceMeta))
|
|
.thenStateShouldEqual({ ...initialState, dataSourceMeta });
|
|
});
|
|
});
|
|
|
|
describe('when setDataSourceName is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(setDataSourceName('some name'))
|
|
.thenStateShouldEqual({ ...initialState, dataSource: { name: 'some name' } } as DataSourcesState);
|
|
});
|
|
});
|
|
|
|
describe('when setIsDefault is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
reducerTester<DataSourcesState>()
|
|
.givenReducer(dataSourcesReducer, initialState)
|
|
.whenActionIsDispatched(setIsDefault(true))
|
|
.thenStateShouldEqual({ ...initialState, dataSource: { isDefault: true } } as DataSourcesState);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('dataSourceSettingsReducer', () => {
|
|
describe('when initDataSourceSettingsSucceeded is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
reducerTester<DataSourceSettingsState>()
|
|
.givenReducer(dataSourceSettingsReducer, { ...initialDataSourceSettingsState })
|
|
.whenActionIsDispatched(initDataSourceSettingsSucceeded({} as GenericDataSourcePlugin))
|
|
.thenStateShouldEqual({
|
|
...initialDataSourceSettingsState,
|
|
plugin: {} as GenericDataSourcePlugin,
|
|
loading: false,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when initDataSourceSettingsFailed is dispatched', () => {
|
|
it('then state should be correct', () => {
|
|
reducerTester<DataSourceSettingsState>()
|
|
.givenReducer(dataSourceSettingsReducer, {
|
|
...initialDataSourceSettingsState,
|
|
plugin: {} as GenericDataSourcePlugin,
|
|
})
|
|
.whenActionIsDispatched(initDataSourceSettingsFailed(new Error('Some error')))
|
|
.thenStatePredicateShouldEqual((resultingState) => {
|
|
expect(resultingState).toEqual({
|
|
testingStatus: {},
|
|
loadError: 'Some error',
|
|
loading: false,
|
|
plugin: null,
|
|
});
|
|
return true;
|
|
});
|
|
});
|
|
});
|
|
});
|