Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Feb 4, 2023
1 parent 9987ede commit bcfaf18
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const getTopNavLinks = ({
defaultMessage: 'Untitled discover search',
}),
},
isDirty: !savedSearch.id || state.isAppStateDirty(),
isDirty: !savedSearch.id || state.appState.hasChanged(),
showPublicUrlSwitch,
onClose: () => {
anchorElement?.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,42 @@ jest.mock('../../utils/persist_saved_search', () => ({
import { onSaveSearch } from './on_save_search';
import { dataViewMock } from '../../../../__mocks__/data_view';
import { savedSearchMock } from '../../../../__mocks__/saved_search';
import { DiscoverServices } from '../../../../build_services';
import { DiscoverStateContainer } from '../../services/discover_state';
import { i18nServiceMock } from '@kbn/core/public/mocks';
import { getDiscoverStateContainer } from '../../services/discover_state';
import { ReactElement } from 'react';
import { discoverServiceMock } from '../../../../__mocks__/services';
import * as persistSavedSearchUtils from '../../utils/persist_saved_search';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { createBrowserHistory } from 'history';

function getStateContainer() {
const savedSearch = savedSearchMock;
const history = createBrowserHistory();
const stateContainer = getDiscoverStateContainer({
savedSearch,
services: discoverServiceMock,
history,
});
stateContainer.appState.getState = jest.fn(() => ({
rowsPerPage: 250,
}));
return stateContainer;
}

describe('onSaveSearch', () => {
it('should call showSaveModal', async () => {
const serviceMock = {
core: {
i18n: i18nServiceMock.create(),
},
} as unknown as DiscoverServices;
const stateMock = {
appState: {
getState: () => ({
rowsPerPage: 250,
}),
},
} as unknown as DiscoverStateContainer;

await onSaveSearch({
dataView: dataViewMock,
navigateTo: jest.fn(),
savedSearch: savedSearchMock,
services: serviceMock,
state: stateMock,
services: discoverServiceMock,
state: getStateContainer(),
updateAdHocDataViewId: jest.fn(),
});

expect(savedObjectsPlugin.showSaveModal).toHaveBeenCalled();
});

it('should pass tags to the save modal', async () => {
const serviceMock = discoverServiceMock;
const stateMock = {
appState: {
getState: () => ({
rowsPerPage: 250,
}),
},
} as unknown as DiscoverStateContainer;
let saveModal: ReactElement | undefined;
jest.spyOn(savedObjectsPlugin, 'showSaveModal').mockImplementationOnce((modal) => {
saveModal = modal;
Expand All @@ -69,23 +61,14 @@ describe('onSaveSearch', () => {
...savedSearchMock,
tags: ['tag1', 'tag2'],
},
services: serviceMock,
state: stateMock,
services: discoverServiceMock,
state: getStateContainer(),
updateAdHocDataViewId: jest.fn(),
});
expect(saveModal?.props.tags).toEqual(['tag1', 'tag2']);
});

it('should update the saved search tags', async () => {
const serviceMock = discoverServiceMock;
const stateMock = {
appState: {
getState: () => ({
rowsPerPage: 250,
}),
},
resetInitialAppState: jest.fn(),
} as unknown as DiscoverStateContainer;
let saveModal: ReactElement | undefined;
jest.spyOn(savedObjectsPlugin, 'showSaveModal').mockImplementationOnce((modal) => {
saveModal = modal;
Expand All @@ -98,8 +81,8 @@ describe('onSaveSearch', () => {
dataView: dataViewMock,
navigateTo: jest.fn(),
savedSearch,
services: serviceMock,
state: stateMock,
services: discoverServiceMock,
state: getStateContainer(),
updateAdHocDataViewId: jest.fn(),
});
expect(savedSearch.tags).toEqual(['tag1', 'tag2']);
Expand All @@ -122,14 +105,6 @@ describe('onSaveSearch', () => {

it('should not update tags if savedObjectsTagging is undefined', async () => {
const serviceMock = discoverServiceMock;
const stateMock = {
appState: {
getState: () => ({
rowsPerPage: 250,
}),
},
resetInitialAppState: jest.fn(),
} as unknown as DiscoverStateContainer;
let saveModal: ReactElement | undefined;
jest.spyOn(savedObjectsPlugin, 'showSaveModal').mockImplementationOnce((modal) => {
saveModal = modal;
Expand All @@ -146,7 +121,7 @@ describe('onSaveSearch', () => {
...serviceMock,
savedObjectsTagging: undefined,
},
state: stateMock,
state: getStateContainer(),
updateAdHocDataViewId: jest.fn(),
});
expect(savedSearch.tags).toEqual(['tag1', 'tag2']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function saveDataSource({
navigateTo(`/view/${encodeURIComponent(id)}`);
} else {
// Update defaults so that "reload saved query" functions correctly
state.resetAppState(savedSearch);
state.appState.resetWithSavedSearch(savedSearch);
services.chrome.docTitle.change(savedSearch.title!);

setBreadcrumbsTitle(
Expand Down Expand Up @@ -169,7 +169,7 @@ export async function onSaveSearch({
savedSearch.tags = currentTags;
}
} else {
state.resetInitialAppState();
state.appState.resetInitialState();
}
onSaveCb?.();
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const useAdHocDataViews = ({
updateFiltersReferences(prevDataView, newDataView);

stateContainer.actions.replaceAdHocDataViewWithId(prevDataView.id!, newDataView);
await stateContainer.replaceUrlAppState({ index: newDataView.id });
await stateContainer.appState.update({ index: newDataView.id }, true);

setUrlTracking(newDataView);
return newDataView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function useDiscoverState({
timefilter: services.timefilter,
});

await stateContainer.replaceUrlAppState(newAppState);
await stateContainer.appState.update(newAppState, true);
setState(newAppState);
},
[services, dataView, stateContainer]
Expand Down
Loading

0 comments on commit bcfaf18

Please sign in to comment.