Skip to content

Commit

Permalink
[Discover] Fix resetting of breakdown field in a saved search (elasti…
Browse files Browse the repository at this point in the history
…c#184668)

- Closes elastic#184379

## Summary

This PR brings back the logic which was edited in
elastic#169548 This should allow to reset
the saved breakdown field with rather an empty string as `undefined`
seems to be ignored.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
jughosta authored Jun 7, 2024
1 parent 5b7381a commit 132ab34
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ function getSavedSearchFieldForComparison(
return visContext;
}

if (fieldName === 'breakdownField') {
return savedSearch.breakdownField || ''; // ignore the difference between an empty string and undefined
}

return savedSearch[fieldName];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ describe('Test discover state actions', () => {
const { searchSource, ...savedSearch } = state.savedSearchState.getState();
expect(savedSearch).toMatchInlineSnapshot(`
Object {
"breakdownField": undefined,
"columns": Array [
"default_column",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ describe('updateSavedSearch', () => {
});
});

it('should pass breakdownField if state has breakdownField', async () => {
const savedSearch = {
...savedSearchMock,
searchSource: savedSearchMock.searchSource.createCopy(),
};
expect(savedSearch.breakdownField).toBeUndefined();
updateSavedSearch({
savedSearch,
globalStateContainer: createGlobalStateContainer(),
services: discoverServiceMock,
state: {
breakdownField: 'test',
},
});
expect(savedSearch.breakdownField).toEqual('test');
});

it('should pass an empty string if state already has breakdownField', async () => {
const savedSearch = {
...savedSearchMock,
searchSource: savedSearchMock.searchSource.createCopy(),
breakdownField: 'test',
};
updateSavedSearch({
savedSearch,
globalStateContainer: createGlobalStateContainer(),
services: discoverServiceMock,
state: {
breakdownField: undefined,
},
});
expect(savedSearch.breakdownField).toEqual('');
});

it('should set query and filters from services', async () => {
const savedSearch = {
...savedSearchMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export function updateSavedSearch({
savedSearch.viewMode = state.viewMode;
}

savedSearch.breakdownField = state.breakdownField || undefined; // `undefined` instead of an empty string
if (typeof state.breakdownField !== 'undefined') {
savedSearch.breakdownField = state.breakdownField;
} else if (savedSearch.breakdownField) {
savedSearch.breakdownField = '';
}

savedSearch.hideAggregatedPreview = state.hideAggregatedPreview;

// add a flag here to identify ES|QL queries
Expand Down

0 comments on commit 132ab34

Please sign in to comment.