Skip to content

Commit

Permalink
update context-update behavior when auto-apply disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Mar 1, 2022
1 parent fba80cb commit d6ba1db
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import moment from 'moment';

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

Expand All @@ -29,7 +29,8 @@ const createMiddleware = (data: DataPublicPluginStart, state?: Partial<LensAppSt
};
const next = jest.fn();

const invoke = (action: PayloadAction<Partial<LensAppState>>) => middleware(store)(next)(action);
const invoke = (action: PayloadAction<Partial<LensAppState> | void>) =>
middleware(store)(next)(action);

return { store, next, invoke };
};
Expand Down Expand Up @@ -70,39 +71,46 @@ 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',
describe('when auto-apply is disabled', () => {
it('only updates searchSessionId when user applies 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 { invoke, store } = createMiddleware(data, {
...initialState,
autoApplyDisabled: true,
});

// setState shouldn't trigger
const setStateAction = {
type: 'lens/setState',
payload: {
visualization: {
state: {},
activeId: 'id2',
},
},
},
};
};
invoke(setStateAction);
expect(store.dispatch).not.toHaveBeenCalledWith(
expect.objectContaining({ type: 'lens/setState' })
);

// test
invoke(action);
expect(store.dispatch).not.toHaveBeenCalledWith(
expect.objectContaining({ type: 'lens/setState' })
);
expect(next).toHaveBeenCalledWith(action);
// applyChanges should trigger
const applyChangesAction = applyChanges();
invoke(applyChangesAction);
expect(store.dispatch).toHaveBeenCalledWith(
expect.objectContaining({ type: 'lens/setState' })
);
});
});
it('does not update the searchSessionId when the state changes and too little time has passed', () => {
const data = mockDataPlugin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import { Dispatch, MiddlewareAPI, PayloadAction } from '@reduxjs/toolkit';
import moment from 'moment';
import { DataPublicPluginStart } from '../../../../../../src/plugins/data/public';
import { setState, LensDispatch, LensStoreDeps, navigateAway, selectChangesApplied } from '..';
import {
setState,
LensDispatch,
LensStoreDeps,
navigateAway,
applyChanges,
selectAutoApplyEnabled,
} from '..';
import { LensAppState } from '../types';
import { getResolvedDateRange, containsDynamicMath } from '../../utils';
import { subscribeToExternalContext } from './subscribe_to_external_context';
Expand All @@ -24,7 +31,7 @@ export const contextMiddleware = (storeDeps: LensStoreDeps) => (store: Middlewar
if (
!action.payload?.searchSessionId &&
!onActiveDataChange.match(action) &&
selectChangesApplied(store.getState())
(selectAutoApplyEnabled(store.getState()) || applyChanges.match(action))
) {
updateTimeRange(storeDeps.lensServices.data, store.dispatch);
}
Expand Down

0 comments on commit d6ba1db

Please sign in to comment.