Skip to content

Commit

Permalink
Fix functional
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Oct 25, 2022
1 parent e8558ab commit a082771
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function getInitialState(
services,
});
return handleSourceColumnState(
savedSearch.id && (!appStateFromUrl || !Object.keys(appStateFromUrl).length)
savedSearch.id
? { ...defaultAppState }
: {
...defaultAppState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { BehaviorSubject } from 'rxjs';
import type { DataView } from '@kbn/data-views-plugin/common';
import { SavedObjectSaveOpts } from '@kbn/saved-objects-plugin/public';
import { differenceWith, isEqual, toPairs } from 'lodash';
import { DataViewSpec } from '@kbn/data-views-plugin/common';
import { loadSavedSearch } from '../utils/load_saved_search';
import { InternalStateContainer } from './discover_internal_state_container';
import { updateSavedSearch } from '../utils/update_saved_search';
import { addLog } from '../../../utils/add_log';
import { handleSourceColumnState } from '../../../utils/state_helpers';
Expand All @@ -31,6 +34,18 @@ export interface SavedSearchContainer {
savedSearchPersisted$: BehaviorSubject<SavedSearch>;
hasChanged$: BehaviorSubject<boolean>;
set: (savedSearch: SavedSearch) => SavedSearch;
load: (
id: string,
{
internalStateContainer,
setError,
dataViewSpec,
}: {
internalStateContainer: InternalStateContainer;
setError: (e: Error) => void;
dataViewSpec?: DataViewSpec;
}
) => Promise<SavedSearch | undefined>;
get: () => SavedSearch;
update: (
nextDataView: DataView | undefined,
Expand Down Expand Up @@ -218,10 +233,32 @@ export function getSavedSearchContainer({
return resetUrl(get().id || '');
};

const load = (
id: string,
{
internalStateContainer,
setError,
dataViewSpec,
}: {
internalStateContainer: InternalStateContainer;
setError: (e: Error) => void;
dataViewSpec?: DataViewSpec;
}
) => {
return loadSavedSearch(id, {
services,
appStateContainer,
dataViewSpec,
internalStateContainer,
setError,
});
};

return {
savedSearch$,
savedSearchPersisted$,
hasChanged$,
load,
set,
reset,
resetUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,22 @@ describe('Test discover state', () => {
describe('Test discover initial state sort handling', () => {
test('Non-empty sort in URL should not be overwritten by saved search sort', async () => {
history = createBrowserHistory();
history.push('/#?_a=(sort:!(!(order_date,desc)))');
history.push('/#?_a=(sort:!(!(timestamp,desc)))');
const savedSearch = {
...savedSearchMockWithTimeField,
...{ sort: [['bytes', 'desc']] },
} as SavedSearch;

state = getDiscoverStateContainer({
savedSearch: { ...savedSearchMock, ...{ sort: [['bytes', 'desc']] } },
savedSearch: undefined,
services: discoverServiceMock,
history,
});
// await state.setAppState({}, true);
state.savedSearchState.load = jest.fn(() => Promise.resolve(savedSearch));
await state.actions.loadSavedSearch(savedSearch.id!, undefined, jest.fn());
// await state.setAppState({});
const stopSync = state.appState.syncState().stop;
expect(state.appState.getState().sort).toEqual([['order_date', 'desc']]);
expect(state.appState.getState().sort).toEqual([['timestamp', 'desc']]);
stopSync();
});
test('Empty sort in URL should use saved search sort for state', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import { getSavedSearchContainer, SavedSearchContainer } from './discover_saved_
import { DiscoverServices } from '../../../build_services';
import { DISCOVER_APP_LOCATOR, DiscoverAppLocatorParams } from '../../../locator';
import { DiscoverSearchSessionManager } from './discover_search_session';
import { loadSavedSearch } from '../utils/load_saved_search';

export interface AppStateUrl extends Omit<AppState, 'sort'> {
/**
Expand Down Expand Up @@ -123,7 +122,7 @@ export interface DiscoverStateContainer {
id: string,
dataViewSpec: DataViewSpec | undefined,
onError: (e: Error) => void
) => void;
) => Promise<SavedSearch | undefined>;
loadNewSavedSearch: (
dataViewSpec: DataViewSpec | undefined,
onError: (e: Error) => void
Expand Down Expand Up @@ -267,9 +266,7 @@ export function getDiscoverStateContainer({
if (appStateContainer.isEmptyURL()) {
appStateContainer.set({});
}
const currentSavedSearch = await loadSavedSearch(id, {
services,
appStateContainer,
const currentSavedSearch = await savedSearchContainer.load(id, {
internalStateContainer,
setError: onError,
dataViewSpec,
Expand Down

0 comments on commit a082771

Please sign in to comment.