Skip to content

Commit

Permalink
stop context middleware from updating time range when user has unappl…
Browse files Browse the repository at this point in the history
…ied changes
  • Loading branch information
drewdaemon committed Mar 1, 2022
1 parent 8a850f0 commit fba80cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import moment from 'moment';

import { contextMiddleware } from '.';
import { DataPublicPluginStart } from '../../../../../../src/plugins/data/public';
import { initialState } from '../lens_slice';
import { disableAutoApply, initialState, setChangesApplied } from '../lens_slice';
import { LensAppState } from '../types';
import { mockDataPlugin, mockStoreDeps } from '../../mocks';

const storeDeps = mockStoreDeps();
const createMiddleware = (data: DataPublicPluginStart) => {
const createMiddleware = (data: DataPublicPluginStart, state?: Partial<LensAppState>) => {
const middleware = contextMiddleware({
...storeDeps,
lensServices: {
Expand All @@ -24,7 +24,7 @@ const createMiddleware = (data: DataPublicPluginStart) => {
},
});
const store = {
getState: jest.fn(() => ({ lens: initialState })),
getState: jest.fn(() => ({ lens: state || initialState })),
dispatch: jest.fn(),
};
const next = jest.fn();
Expand Down Expand Up @@ -70,6 +70,40 @@ describe('contextMiddleware', () => {
});
expect(next).toHaveBeenCalledWith(action);
});
it('does NOT update the searchSessionId when user has unapplied changes', () => {
// setup
const data = mockDataPlugin();
(data.nowProvider.get as jest.Mock).mockReturnValue(new Date(Date.now() - 30000));
(data.query.timefilter.timefilter.getTime as jest.Mock).mockReturnValue({
from: 'now-2m',
to: 'now',
});
(data.query.timefilter.timefilter.getBounds as jest.Mock).mockReturnValue({
min: moment(Date.now() - 100000),
max: moment(Date.now() - 30000),
});
const { next, invoke, store } = createMiddleware(data, {
...initialState,
changesApplied: false,
autoApplyDisabled: true,
});
const action = {
type: 'lens/setState',
payload: {
visualization: {
state: {},
activeId: 'id2',
},
},
};

// test
invoke(action);
expect(store.dispatch).not.toHaveBeenCalledWith(
expect.objectContaining({ type: 'lens/setState' })
);
expect(next).toHaveBeenCalledWith(action);
});
it('does not update the searchSessionId when the state changes and too little time has passed', () => {
const data = mockDataPlugin();
// time range is 100,000ms ago to 300ms ago (that's a lag of .3 percent, not enough to trigger a session update)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { Dispatch, MiddlewareAPI, PayloadAction } from '@reduxjs/toolkit';
import moment from 'moment';
import { DataPublicPluginStart } from '../../../../../../src/plugins/data/public';
import { setState, LensDispatch, LensStoreDeps, navigateAway } from '..';
import { setState, LensDispatch, LensStoreDeps, navigateAway, selectChangesApplied } from '..';
import { LensAppState } from '../types';
import { getResolvedDateRange, containsDynamicMath } from '../../utils';
import { subscribeToExternalContext } from './subscribe_to_external_context';
Expand All @@ -21,7 +21,11 @@ export const contextMiddleware = (storeDeps: LensStoreDeps) => (store: Middlewar
store.dispatch
);
return (next: Dispatch) => (action: PayloadAction<Partial<LensAppState>>) => {
if (!action.payload?.searchSessionId && !onActiveDataChange.match(action)) {
if (
!action.payload?.searchSessionId &&
!onActiveDataChange.match(action) &&
selectChangesApplied(store.getState())
) {
updateTimeRange(storeDeps.lensServices.data, store.dispatch);
}
if (navigateAway.match(action)) {
Expand Down

0 comments on commit fba80cb

Please sign in to comment.