"Fossies" - the Fresh Open Source Software Archive

Member "grafana-6.5.1/public/app/features/explore/state/actions.test.ts" (28 Nov 2019, 12044 Bytes) of package /linux/misc/grafana-6.5.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "actions.test.ts": 6.4.4_vs_6.5.0.

    1 import { loadDatasource, navigateToExplore, refreshExplore } from './actions';
    2 import { ExploreId, ExploreMode, ExploreUpdateState, ExploreUrlState } from 'app/types';
    3 import { thunkTester } from 'test/core/thunk/thunkTester';
    4 import {
    5   initializeExploreAction,
    6   InitializeExplorePayload,
    7   loadDatasourcePendingAction,
    8   loadDatasourceReadyAction,
    9   setQueriesAction,
   10   updateUIStateAction,
   11 } from './actionTypes';
   12 import { Emitter } from 'app/core/core';
   13 import { ActionOf } from 'app/core/redux/actionCreatorFactory';
   14 import { makeInitialUpdateState } from './reducers';
   15 import { DataQuery, DefaultTimeZone, LogsDedupStrategy, RawTimeRange, toUtc } from '@grafana/data';
   16 import { PanelModel } from 'app/features/dashboard/state';
   17 import { updateLocation } from '../../../core/actions';
   18 import { MockDataSourceApi } from '../../../../test/mocks/datasource_srv';
   19 
   20 jest.mock('app/features/plugins/datasource_srv', () => ({
   21   getDatasourceSrv: () => ({
   22     getExternal: jest.fn().mockReturnValue([]),
   23     get: jest.fn().mockReturnValue({
   24       testDatasource: jest.fn(),
   25       init: jest.fn(),
   26     }),
   27   }),
   28 }));
   29 
   30 jest.mock('../../dashboard/services/TimeSrv', () => ({
   31   getTimeSrv: jest.fn().mockReturnValue({
   32     init: jest.fn(),
   33   }),
   34 }));
   35 
   36 const t = toUtc();
   37 const testRange = {
   38   from: t,
   39   to: t,
   40   raw: {
   41     from: t,
   42     to: t,
   43   },
   44 };
   45 jest.mock('app/core/utils/explore', () => ({
   46   ...jest.requireActual('app/core/utils/explore'),
   47   getTimeRangeFromUrl: (range: RawTimeRange) => testRange,
   48 }));
   49 
   50 const setup = (updateOverides?: Partial<ExploreUpdateState>) => {
   51   const exploreId = ExploreId.left;
   52   const containerWidth = 1920;
   53   const eventBridge = {} as Emitter;
   54   const ui = { dedupStrategy: LogsDedupStrategy.none, showingGraph: false, showingLogs: false, showingTable: false };
   55   const timeZone = DefaultTimeZone;
   56   const range = testRange;
   57   const urlState: ExploreUrlState = {
   58     datasource: 'some-datasource',
   59     queries: [],
   60     range: range.raw,
   61     mode: ExploreMode.Metrics,
   62     ui,
   63   };
   64   const updateDefaults = makeInitialUpdateState();
   65   const update = { ...updateDefaults, ...updateOverides };
   66   const initialState = {
   67     user: {
   68       orgId: '1',
   69       timeZone,
   70     },
   71     explore: {
   72       [exploreId]: {
   73         initialized: true,
   74         urlState,
   75         containerWidth,
   76         eventBridge,
   77         update,
   78         datasourceInstance: { name: 'some-datasource' },
   79         queries: [] as DataQuery[],
   80         range,
   81         ui,
   82         refreshInterval: {
   83           label: 'Off',
   84           value: 0,
   85         },
   86       },
   87     },
   88   };
   89 
   90   return {
   91     initialState,
   92     exploreId,
   93     range,
   94     ui,
   95     containerWidth,
   96     eventBridge,
   97   };
   98 };
   99 
  100 describe('refreshExplore', () => {
  101   describe('when explore is initialized', () => {
  102     describe('and update datasource is set', () => {
  103       it('then it should dispatch initializeExplore', async () => {
  104         const { exploreId, ui, initialState, containerWidth, eventBridge } = setup({ datasource: true });
  105 
  106         const dispatchedActions = await thunkTester(initialState)
  107           .givenThunk(refreshExplore)
  108           .whenThunkIsDispatched(exploreId);
  109 
  110         const initializeExplore = dispatchedActions[2] as ActionOf<InitializeExplorePayload>;
  111         const { type, payload } = initializeExplore;
  112 
  113         expect(type).toEqual(initializeExploreAction.type);
  114         expect(payload.containerWidth).toEqual(containerWidth);
  115         expect(payload.eventBridge).toEqual(eventBridge);
  116         expect(payload.queries.length).toBe(1); // Queries have generated keys hard to expect on
  117         expect(payload.range.from).toEqual(testRange.from);
  118         expect(payload.range.to).toEqual(testRange.to);
  119         expect(payload.range.raw.from).toEqual(testRange.raw.from);
  120         expect(payload.range.raw.to).toEqual(testRange.raw.to);
  121         expect(payload.ui).toEqual(ui);
  122       });
  123     });
  124 
  125     describe('and update ui is set', () => {
  126       it('then it should dispatch updateUIStateAction', async () => {
  127         const { exploreId, initialState, ui } = setup({ ui: true });
  128 
  129         const dispatchedActions = await thunkTester(initialState)
  130           .givenThunk(refreshExplore)
  131           .whenThunkIsDispatched(exploreId);
  132 
  133         expect(dispatchedActions[0].type).toEqual(updateUIStateAction.type);
  134         expect(dispatchedActions[0].payload).toEqual({ ...ui, exploreId });
  135       });
  136     });
  137 
  138     describe('and update queries is set', () => {
  139       it('then it should dispatch setQueriesAction', async () => {
  140         const { exploreId, initialState } = setup({ queries: true });
  141 
  142         const dispatchedActions = await thunkTester(initialState)
  143           .givenThunk(refreshExplore)
  144           .whenThunkIsDispatched(exploreId);
  145 
  146         expect(dispatchedActions[0].type).toEqual(setQueriesAction.type);
  147         expect(dispatchedActions[0].payload).toEqual({ exploreId, queries: [] });
  148       });
  149     });
  150   });
  151 
  152   describe('when update is not initialized', () => {
  153     it('then it should not dispatch any actions', async () => {
  154       const exploreId = ExploreId.left;
  155       const initialState = { explore: { [exploreId]: { initialized: false } } };
  156 
  157       const dispatchedActions = await thunkTester(initialState)
  158         .givenThunk(refreshExplore)
  159         .whenThunkIsDispatched(exploreId);
  160 
  161       expect(dispatchedActions).toEqual([]);
  162     });
  163   });
  164 });
  165 
  166 describe('loading datasource', () => {
  167   describe('when loadDatasource thunk is dispatched', () => {
  168     describe('and all goes fine', () => {
  169       it('then it should dispatch correct actions', async () => {
  170         const exploreId = ExploreId.left;
  171         const name = 'some-datasource';
  172         const initialState = { explore: { [exploreId]: { requestedDatasourceName: name } } };
  173         const mockDatasourceInstance = {
  174           testDatasource: () => {
  175             return Promise.resolve({ status: 'success' });
  176           },
  177           name,
  178           init: jest.fn(),
  179           meta: { id: 'some id' },
  180         };
  181 
  182         const dispatchedActions = await thunkTester(initialState)
  183           .givenThunk(loadDatasource)
  184           .whenThunkIsDispatched(exploreId, mockDatasourceInstance);
  185 
  186         expect(dispatchedActions).toEqual([
  187           loadDatasourcePendingAction({
  188             exploreId,
  189             requestedDatasourceName: mockDatasourceInstance.name,
  190           }),
  191           loadDatasourceReadyAction({ exploreId, history: [] }),
  192         ]);
  193       });
  194     });
  195 
  196     describe('and user changes datasource during load', () => {
  197       it('then it should dispatch correct actions', async () => {
  198         const exploreId = ExploreId.left;
  199         const name = 'some-datasource';
  200         const initialState = { explore: { [exploreId]: { requestedDatasourceName: 'some-other-datasource' } } };
  201         const mockDatasourceInstance = {
  202           testDatasource: () => {
  203             return Promise.resolve({ status: 'success' });
  204           },
  205           name,
  206           init: jest.fn(),
  207           meta: { id: 'some id' },
  208         };
  209 
  210         const dispatchedActions = await thunkTester(initialState)
  211           .givenThunk(loadDatasource)
  212           .whenThunkIsDispatched(exploreId, mockDatasourceInstance);
  213 
  214         expect(dispatchedActions).toEqual([
  215           loadDatasourcePendingAction({
  216             exploreId,
  217             requestedDatasourceName: mockDatasourceInstance.name,
  218           }),
  219         ]);
  220       });
  221     });
  222   });
  223 });
  224 
  225 const getNavigateToExploreContext = async (openInNewWindow: (url: string) => void = undefined) => {
  226   const url = 'http://www.someurl.com';
  227   const panel: Partial<PanelModel> = {
  228     datasource: 'mocked datasource',
  229     targets: [{ refId: 'A' }],
  230   };
  231   const datasource = new MockDataSourceApi(panel.datasource);
  232   const get = jest.fn().mockResolvedValue(datasource);
  233   const getDataSourceSrv = jest.fn().mockReturnValue({ get });
  234   const getTimeSrv = jest.fn();
  235   const getExploreUrl = jest.fn().mockResolvedValue(url);
  236 
  237   const dispatchedActions = await thunkTester({})
  238     .givenThunk(navigateToExplore)
  239     .whenThunkIsDispatched(panel, { getDataSourceSrv, getTimeSrv, getExploreUrl, openInNewWindow });
  240 
  241   return {
  242     url,
  243     panel,
  244     datasource,
  245     get,
  246     getDataSourceSrv,
  247     getTimeSrv,
  248     getExploreUrl,
  249     dispatchedActions,
  250   };
  251 };
  252 
  253 describe('navigateToExplore', () => {
  254   describe('when navigateToExplore thunk is dispatched', () => {
  255     describe('and openInNewWindow is undefined', () => {
  256       const openInNewWindow: (url: string) => void = undefined;
  257       it('then it should dispatch correct actions', async () => {
  258         const { dispatchedActions, url } = await getNavigateToExploreContext(openInNewWindow);
  259 
  260         expect(dispatchedActions).toEqual([updateLocation({ path: url, query: {} })]);
  261       });
  262 
  263       it('then getDataSourceSrv should have been once', async () => {
  264         const { getDataSourceSrv } = await getNavigateToExploreContext(openInNewWindow);
  265 
  266         expect(getDataSourceSrv).toHaveBeenCalledTimes(1);
  267       });
  268 
  269       it('then getDataSourceSrv.get should have been called with correct arguments', async () => {
  270         const { get, panel } = await getNavigateToExploreContext(openInNewWindow);
  271 
  272         expect(get).toHaveBeenCalledTimes(1);
  273         expect(get).toHaveBeenCalledWith(panel.datasource);
  274       });
  275 
  276       it('then getTimeSrv should have been called once', async () => {
  277         const { getTimeSrv } = await getNavigateToExploreContext(openInNewWindow);
  278 
  279         expect(getTimeSrv).toHaveBeenCalledTimes(1);
  280       });
  281 
  282       it('then getExploreUrl should have been called with correct arguments', async () => {
  283         const { getExploreUrl, panel, datasource, getDataSourceSrv, getTimeSrv } = await getNavigateToExploreContext(
  284           openInNewWindow
  285         );
  286 
  287         expect(getExploreUrl).toHaveBeenCalledTimes(1);
  288         expect(getExploreUrl).toHaveBeenCalledWith({
  289           panel,
  290           panelTargets: panel.targets,
  291           panelDatasource: datasource,
  292           datasourceSrv: getDataSourceSrv(),
  293           timeSrv: getTimeSrv(),
  294         });
  295       });
  296     });
  297 
  298     describe('and openInNewWindow is defined', () => {
  299       const openInNewWindow: (url: string) => void = jest.fn();
  300       it('then it should dispatch no actions', async () => {
  301         const { dispatchedActions } = await getNavigateToExploreContext(openInNewWindow);
  302 
  303         expect(dispatchedActions).toEqual([]);
  304       });
  305 
  306       it('then getDataSourceSrv should have been once', async () => {
  307         const { getDataSourceSrv } = await getNavigateToExploreContext(openInNewWindow);
  308 
  309         expect(getDataSourceSrv).toHaveBeenCalledTimes(1);
  310       });
  311 
  312       it('then getDataSourceSrv.get should have been called with correct arguments', async () => {
  313         const { get, panel } = await getNavigateToExploreContext(openInNewWindow);
  314 
  315         expect(get).toHaveBeenCalledTimes(1);
  316         expect(get).toHaveBeenCalledWith(panel.datasource);
  317       });
  318 
  319       it('then getTimeSrv should have been called once', async () => {
  320         const { getTimeSrv } = await getNavigateToExploreContext(openInNewWindow);
  321 
  322         expect(getTimeSrv).toHaveBeenCalledTimes(1);
  323       });
  324 
  325       it('then getExploreUrl should have been called with correct arguments', async () => {
  326         const { getExploreUrl, panel, datasource, getDataSourceSrv, getTimeSrv } = await getNavigateToExploreContext(
  327           openInNewWindow
  328         );
  329 
  330         expect(getExploreUrl).toHaveBeenCalledTimes(1);
  331         expect(getExploreUrl).toHaveBeenCalledWith({
  332           panel,
  333           panelTargets: panel.targets,
  334           panelDatasource: datasource,
  335           datasourceSrv: getDataSourceSrv(),
  336           timeSrv: getTimeSrv(),
  337         });
  338       });
  339 
  340       it('then openInNewWindow should have been called with correct arguments', async () => {
  341         const openInNewWindowFunc = jest.fn();
  342         const { url } = await getNavigateToExploreContext(openInNewWindowFunc);
  343 
  344         expect(openInNewWindowFunc).toHaveBeenCalledTimes(1);
  345         expect(openInNewWindowFunc).toHaveBeenCalledWith(url);
  346       });
  347     });
  348   });
  349 });