Skip to content

Commit

Permalink
[Discover] Remove kbnUrlStateStorage of Discover main state container (
Browse files Browse the repository at this point in the history
…#158225)

The exposed kbnUrlStateStorage property of `DiscoverStateContainer` is removed. It was just used for testing and is replaced with `await new Promise(process.nextTick);`
  • Loading branch information
kertal committed May 23, 2023
1 parent 3800ce4 commit 84d070d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Test discover state', () => {
});
test('setting app state and syncing to URL', async () => {
state.appState.update({ index: 'modified' });
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_a=(columns:!(default_column),index:modified,interval:auto,sort:!())"`
);
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('Test discover state with legacy migration', () => {
});
});

describe('createSearchSessionRestorationDataProvider', () => {
describe('Test createSearchSessionRestorationDataProvider', () => {
let mockSavedSearch: SavedSearch = {} as unknown as SavedSearch;
const history = createBrowserHistory();
const mockDataPlugin = dataPluginMock.createStartContract();
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('createSearchSessionRestorationDataProvider', () => {
});
});

describe('searchSessionManager', () => {
describe('Test discover searchSessionManager', () => {
test('getting the next session id', async () => {
const { state } = await getState();
const nextId = 'id';
Expand All @@ -247,7 +247,7 @@ describe('searchSessionManager', () => {
});
});

describe('actions', () => {
describe('Test discover state actions', () => {
beforeEach(async () => {
discoverServiceMock.data.query.timefilter.timefilter.getTime = jest.fn(() => {
return { from: 'now-15d', to: 'now' };
Expand Down Expand Up @@ -319,7 +319,7 @@ describe('actions', () => {
const newSavedSearch = await state.actions.loadSavedSearch();
expect(newSavedSearch?.id).toBeUndefined();
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_g=(refreshInterval:(pause:!t,value:1000),time:(from:now-15d,to:now))&_a=(columns:!(default_column),index:the-data-view-id,interval:auto,sort:!())"`
);
Expand All @@ -346,7 +346,7 @@ describe('actions', () => {
const newSavedSearch = await state.actions.loadSavedSearch();
expect(newSavedSearch?.id).toBeUndefined();
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_g=(refreshInterval:(pause:!t,value:1000),time:(from:now-15d,to:now))&_a=(columns:!(default_column),index:the-data-view-id,interval:auto,sort:!())"`
);
Expand All @@ -360,7 +360,7 @@ describe('actions', () => {
const newSavedSearch = await state.actions.loadSavedSearch({ useAppState: true });
expect(newSavedSearch?.id).toBeUndefined();
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_a=(columns:!(bytes),index:the-data-view-id,interval:month,sort:!())&_g=()"`
);
Expand All @@ -374,7 +374,7 @@ describe('actions', () => {
const newSavedSearch = await state.actions.loadSavedSearch({ useAppState: true });
expect(newSavedSearch?.id).toBeUndefined();
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_a=(columns:!(bytes),index:the-data-view-id,interval:month,sort:!())&_g=()"`
);
Expand All @@ -387,7 +387,7 @@ describe('actions', () => {
savedSearchId: 'the-saved-search-id',
});
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(newSavedSearch?.id).toBe('the-saved-search-id');
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_g=(refreshInterval:(pause:!t,value:1000),time:(from:now-15d,to:now))&_a=(columns:!(default_column),index:the-data-view-id,interval:auto,sort:!())"`
Expand All @@ -400,7 +400,7 @@ describe('actions', () => {
const { state, getCurrentUrl } = await getState(url, savedSearchMock);
await state.actions.loadSavedSearch({ savedSearchId: savedSearchMock.id, useAppState: true });
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_a=(columns:!(message),index:the-data-view-id,interval:month,sort:!())&_g=()"`
);
Expand Down Expand Up @@ -533,23 +533,28 @@ describe('actions', () => {

test('onChangeDataView', async () => {
const { state, getCurrentUrl } = await getState('/', savedSearchMock);
await state.actions.loadSavedSearch({ savedSearchId: savedSearchMock.id });
expect(state.savedSearchState.getState().searchSource.getField('index')!.id).toBe(
dataViewMock.id
);
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_g=(refreshInterval:(pause:!t,value:1000),time:(from:now-15d,to:now))&_a=(columns:!(default_column),index:the-data-view-id,interval:auto,sort:!())"`
);
await state.actions.onChangeDataView(dataViewComplexMock.id!);
await waitFor(() => {
expect(state.internalState.getState().dataView?.id).toBe(dataViewComplexMock.id);
});
expect(state.appState.get().index).toBe(dataViewComplexMock.id);
expect(state.savedSearchState.getState().searchSource.getField('index')!.id).toBe(
const { actions, savedSearchState, dataState, appState } = state;

await actions.loadSavedSearch({ savedSearchId: savedSearchMock.id });
const unsubscribe = actions.initializeAndSync();
await new Promise(process.nextTick);
// test initial state
expect(dataState.fetch).toHaveBeenCalledTimes(0);
expect(savedSearchState.getState().searchSource.getField('index')!.id).toBe(dataViewMock.id);
expect(getCurrentUrl()).toContain(dataViewMock.id);

// change data view
await actions.onChangeDataView(dataViewComplexMock.id!);
await new Promise(process.nextTick);

// test changed state, fetch should be called once and URL should be updated
expect(dataState.fetch).toHaveBeenCalledTimes(1);
expect(appState.get().index).toBe(dataViewComplexMock.id);
expect(savedSearchState.getState().searchSource.getField('index')!.id).toBe(
dataViewComplexMock.id
);
// check if the changed data view is reflected in the URL
expect(getCurrentUrl()).toContain(dataViewComplexMock.id);
unsubscribe();
});
test('onDataViewCreated - persisted data view', async () => {
Expand Down Expand Up @@ -649,15 +654,15 @@ describe('actions', () => {
// Load a given persisted saved search
await state.actions.loadSavedSearch({ savedSearchId: savedSearchMock.id });
const unsubscribe = state.actions.initializeAndSync();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
const initialUrlState =
'/#?_g=(refreshInterval:(pause:!t,value:1000),time:(from:now-15d,to:now))&_a=(columns:!(default_column),index:the-data-view-id,interval:auto,sort:!())';
expect(getCurrentUrl()).toBe(initialUrlState);
expect(state.internalState.getState().dataView?.id).toBe(dataViewMock.id!);

// Change the data view, this should change the URL and trigger a fetch
await state.actions.onChangeDataView(dataViewComplexMock.id!);
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toMatchInlineSnapshot(
`"/#?_g=(refreshInterval:(pause:!t,value:1000),time:(from:now-15d,to:now))&_a=(columns:!(default_column),index:data-view-with-various-field-types-id,interval:auto,sort:!(!(data,desc)))"`
);
Expand All @@ -668,7 +673,7 @@ describe('actions', () => {

// Undo all changes to the saved search, this should trigger a fetch, again
await state.actions.undoSavedSearchChanges();
state.kbnUrlStateStorage.kbnUrlControls.flush();
await new Promise(process.nextTick);
expect(getCurrentUrl()).toBe(initialUrlState);
await waitFor(() => {
expect(state.dataState.fetch).toHaveBeenCalledTimes(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { i18n } from '@kbn/i18n';
import { History } from 'history';
import {
createKbnUrlStateStorage,
IKbnUrlStateStorage,
StateContainer,
withNotifyOnErrors,
} from '@kbn/kibana-utils-plugin/public';
Expand Down Expand Up @@ -100,10 +99,6 @@ export interface DiscoverStateContainer {
* Internal shared state that's used at several places in the UI
*/
internalState: DiscoverInternalStateContainer;
/**
* kbnUrlStateStorage - it keeps the state in sync with the URL
*/
kbnUrlStateStorage: IKbnUrlStateStorage;
/**
* State of saved search, the saved object of Discover
*/
Expand Down Expand Up @@ -449,7 +444,6 @@ export function getDiscoverStateContainer({
};

return {
kbnUrlStateStorage: stateStorage,
appState: appStateContainer,
internalState: internalStateContainer,
dataState: dataStateContainer,
Expand Down

0 comments on commit 84d070d

Please sign in to comment.