| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- import { mockExploreState } from 'test/mocks/mockExploreState';
- import { epicTester } from 'test/core/redux/epicTester';
- import { runQueriesBatchEpic } from './runQueriesBatchEpic';
- import {
- runQueriesBatchAction,
- queryStartAction,
- historyUpdatedAction,
- processQueryResultsAction,
- processQueryErrorsAction,
- limitMessageRatePayloadAction,
- resetExploreAction,
- updateDatasourceInstanceAction,
- changeRefreshIntervalAction,
- clearQueriesAction,
- stateSaveAction,
- } from '../actionTypes';
- import { LoadingState, DataFrame, FieldType } from '@grafana/data';
- import { DataQueryRequest } from '@grafana/ui';
- const testContext = () => {
- const series: DataFrame[] = [
- {
- fields: [
- {
- name: 'Value',
- },
- {
- name: 'Time',
- type: FieldType.time,
- unit: 'dateTimeAsIso',
- },
- ],
- rows: [],
- refId: 'A',
- },
- ];
- const response = { data: series };
- return {
- response,
- series,
- };
- };
- describe('runQueriesBatchEpic', () => {
- let originalDateNow = Date.now;
- beforeEach(() => {
- originalDateNow = Date.now;
- Date.now = () => 1337;
- });
- afterEach(() => {
- Date.now = originalDateNow;
- });
- describe('when runQueriesBatchAction is dispatched', () => {
- describe('and query targets are not live', () => {
- describe('and query is successful', () => {
- it('then correct actions are dispatched', () => {
- const { response, series } = testContext();
- const { exploreId, state, history, datasourceId } = mockExploreState();
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: false, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryReceivesResponse(response)
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- historyUpdatedAction({ exploreId, history }),
- processQueryResultsAction({
- exploreId,
- delta: null,
- series,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction()
- );
- });
- });
- describe('and query is not successful', () => {
- it('then correct actions are dispatched', () => {
- const error = {
- message: 'Error parsing line x',
- };
- const { exploreId, state, datasourceId } = mockExploreState();
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: false, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryThrowsError(error)
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- processQueryErrorsAction({ exploreId, response: error, datasourceId })
- );
- });
- });
- });
- describe('and query targets are live', () => {
- describe('and state equals Streaming', () => {
- it('then correct actions are dispatched', () => {
- const { exploreId, state, datasourceId } = mockExploreState();
- const unsubscribe = jest.fn();
- const serieA: any = {
- fields: [],
- rows: [],
- refId: 'A',
- };
- const serieB: any = {
- fields: [],
- rows: [],
- refId: 'B',
- };
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: true, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryObserverReceivesEvent({
- state: LoadingState.Streaming,
- delta: [serieA],
- key: 'some key',
- request: {} as DataQueryRequest,
- unsubscribe,
- })
- .whenQueryObserverReceivesEvent({
- state: LoadingState.Streaming,
- delta: [serieB],
- key: 'some key',
- request: {} as DataQueryRequest,
- unsubscribe,
- })
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- limitMessageRatePayloadAction({ exploreId, series: [serieA], datasourceId }),
- limitMessageRatePayloadAction({ exploreId, series: [serieB], datasourceId })
- );
- });
- });
- describe('and state equals Error', () => {
- it('then correct actions are dispatched', () => {
- const { exploreId, state, datasourceId } = mockExploreState();
- const unsubscribe = jest.fn();
- const error = { message: 'Something went really wrong!' };
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: true, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryObserverReceivesEvent({
- state: LoadingState.Error,
- error,
- key: 'some key',
- request: {} as DataQueryRequest,
- unsubscribe,
- })
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- processQueryErrorsAction({ exploreId, response: error, datasourceId })
- );
- });
- });
- describe('and state equals Done', () => {
- it('then correct actions are dispatched', () => {
- const { exploreId, state, datasourceId, history } = mockExploreState();
- const unsubscribe = jest.fn();
- const serieA: any = {
- fields: [],
- rows: [],
- refId: 'A',
- };
- const serieB: any = {
- fields: [],
- rows: [],
- refId: 'B',
- };
- const delta = [serieA, serieB];
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: true, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryObserverReceivesEvent({
- state: LoadingState.Done,
- data: null,
- delta,
- key: 'some key',
- request: {} as DataQueryRequest,
- unsubscribe,
- })
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- historyUpdatedAction({ exploreId, history }),
- processQueryResultsAction({
- exploreId,
- delta,
- series: null,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction()
- );
- });
- });
- });
- describe('and another runQueriesBatchAction is dispatched', () => {
- it('then the observable should be unsubscribed', () => {
- const { response, series } = testContext();
- const { exploreId, state, history, datasourceId } = mockExploreState();
- const unsubscribe = jest.fn();
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: false, interval: '', maxDataPoints: 1980 } }) // first observable
- )
- .whenQueryReceivesResponse(response)
- .whenQueryObserverReceivesEvent({
- key: 'some key',
- request: {} as DataQueryRequest,
- state: LoadingState.Loading, // fake just to setup and test unsubscribe
- unsubscribe,
- })
- .whenActionIsDispatched(
- // second observable and unsubscribes the first observable
- runQueriesBatchAction({ exploreId, queryOptions: { live: true, interval: '', maxDataPoints: 800 } })
- )
- .whenQueryReceivesResponse(response)
- .whenQueryObserverReceivesEvent({
- key: 'some key',
- request: {} as DataQueryRequest,
- state: LoadingState.Loading, // fake just to setup and test unsubscribe
- unsubscribe,
- })
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }), // output from first observable
- historyUpdatedAction({ exploreId, history }), // output from first observable
- processQueryResultsAction({
- exploreId,
- delta: null,
- series,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction(),
- // output from first observable
- queryStartAction({ exploreId }), // output from second observable
- historyUpdatedAction({ exploreId, history }), // output from second observable
- processQueryResultsAction({
- exploreId,
- delta: null,
- series,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction()
- // output from second observable
- );
- expect(unsubscribe).toBeCalledTimes(1); // first unsubscribe should be called but not second as that isn't unsubscribed
- });
- });
- describe('and resetExploreAction is dispatched', () => {
- it('then the observable should be unsubscribed', () => {
- const { response, series } = testContext();
- const { exploreId, state, history, datasourceId } = mockExploreState();
- const unsubscribe = jest.fn();
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: false, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryReceivesResponse(response)
- .whenQueryObserverReceivesEvent({
- key: 'some key',
- request: {} as DataQueryRequest,
- state: LoadingState.Loading, // fake just to setup and test unsubscribe
- unsubscribe,
- })
- .whenActionIsDispatched(resetExploreAction()) // unsubscribes the observable
- .whenQueryReceivesResponse(response) // new updates will not reach anywhere
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- historyUpdatedAction({ exploreId, history }),
- processQueryResultsAction({
- exploreId,
- delta: null,
- series,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction()
- );
- expect(unsubscribe).toBeCalledTimes(1);
- });
- });
- describe('and updateDatasourceInstanceAction is dispatched', () => {
- it('then the observable should be unsubscribed', () => {
- const { response, series } = testContext();
- const { exploreId, state, history, datasourceId, datasourceInstance } = mockExploreState();
- const unsubscribe = jest.fn();
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: false, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryReceivesResponse(response)
- .whenQueryObserverReceivesEvent({
- key: 'some key',
- request: {} as DataQueryRequest,
- state: LoadingState.Loading, // fake just to setup and test unsubscribe
- unsubscribe,
- })
- .whenActionIsDispatched(updateDatasourceInstanceAction({ exploreId, datasourceInstance })) // unsubscribes the observable
- .whenQueryReceivesResponse(response) // new updates will not reach anywhere
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- historyUpdatedAction({ exploreId, history }),
- processQueryResultsAction({
- exploreId,
- delta: null,
- series,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction()
- );
- expect(unsubscribe).toBeCalledTimes(1);
- });
- });
- describe('and changeRefreshIntervalAction is dispatched', () => {
- it('then the observable should be unsubscribed', () => {
- const { response, series } = testContext();
- const { exploreId, state, history, datasourceId } = mockExploreState();
- const unsubscribe = jest.fn();
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: false, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryReceivesResponse(response)
- .whenQueryObserverReceivesEvent({
- key: 'some key',
- request: {} as DataQueryRequest,
- state: LoadingState.Loading, // fake just to setup and test unsubscribe
- unsubscribe,
- })
- .whenActionIsDispatched(changeRefreshIntervalAction({ exploreId, refreshInterval: '' })) // unsubscribes the observable
- .whenQueryReceivesResponse(response) // new updates will not reach anywhere
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- historyUpdatedAction({ exploreId, history }),
- processQueryResultsAction({
- exploreId,
- delta: null,
- series,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction()
- );
- expect(unsubscribe).toBeCalledTimes(1);
- });
- });
- describe('and clearQueriesAction is dispatched', () => {
- it('then the observable should be unsubscribed', () => {
- const { response, series } = testContext();
- const { exploreId, state, history, datasourceId } = mockExploreState();
- const unsubscribe = jest.fn();
- epicTester(runQueriesBatchEpic, state)
- .whenActionIsDispatched(
- runQueriesBatchAction({ exploreId, queryOptions: { live: false, interval: '', maxDataPoints: 1980 } })
- )
- .whenQueryReceivesResponse(response)
- .whenQueryObserverReceivesEvent({
- key: 'some key',
- request: {} as DataQueryRequest,
- state: LoadingState.Loading, // fake just to setup and test unsubscribe
- unsubscribe,
- })
- .whenActionIsDispatched(clearQueriesAction({ exploreId })) // unsubscribes the observable
- .whenQueryReceivesResponse(response) // new updates will not reach anywhere
- .thenResultingActionsEqual(
- queryStartAction({ exploreId }),
- historyUpdatedAction({ exploreId, history }),
- processQueryResultsAction({
- exploreId,
- delta: null,
- series,
- latency: 0,
- datasourceId,
- loadingState: LoadingState.Done,
- }),
- stateSaveAction()
- );
- expect(unsubscribe).toBeCalledTimes(1);
- });
- });
- });
- });
|