[Discover] Fix Discover sidebar nav link global state syncing (#169905)

## Summary

This PR fixes an issue where global state was not being synced to the
Discover sidebar nav link when modifying it from other applications,
such as Lens.

Fixes #165899.

### Checklist

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [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
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [x] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
This commit is contained in:
Davis McPhee 2023-10-26 11:22:36 -03:00 committed by GitHub
parent cadbaee505
commit 63db41ddae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 127 additions and 39 deletions

View file

@ -10,6 +10,7 @@ import type { BehaviorSubject } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { createGetterSetter, createKbnUrlTracker } from '@kbn/kibana-utils-plugin/public';
import { replaceUrlHashQuery } from '@kbn/kibana-utils-plugin/common';
import { isFilterPinned } from '@kbn/es-query';
import { getScopedHistory } from '../kibana_services';
import { SEARCH_SESSION_ID_QUERY_PARAM } from '../constants';
import type { DiscoverSetupPlugins } from '../plugin';
@ -61,13 +62,10 @@ export function initializeKbnUrlTracking(
filter(
({ changes }) => !!(changes.globalFilters || changes.time || changes.refreshInterval)
),
map(async ({ state }) => {
const { isFilterPinned } = await import('@kbn/es-query');
return {
...state,
filters: state.filters?.filter(isFilterPinned),
};
})
map(({ state }) => ({
...state,
filters: state.filters?.filter(isFilterPinned),
}))
),
},
],

View file

@ -334,37 +334,5 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
expect(newMainPanelSize).to.be(mainPanelSize - resizeDistance);
});
});
describe('URL state', () => {
it('should show a warning and fall back to the default data view when navigating to a URL with an invalid data view ID', async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
await PageObjects.header.waitUntilLoadingHasFinished();
const dataViewId = await PageObjects.discover.getCurrentDataViewId();
const originalUrl = await browser.getCurrentUrl();
const newUrl = originalUrl.replace(dataViewId, 'invalid-data-view-id');
await browser.get(newUrl);
await PageObjects.header.waitUntilLoadingHasFinished();
await retry.try(async () => {
expect(await browser.getCurrentUrl()).to.be(originalUrl);
expect(await testSubjects.exists('dscDataViewNotFoundShowDefaultWarning')).to.be(true);
});
});
it('should show a warning and fall back to the current data view if the URL is updated to an invalid data view ID', async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
const originalHash = await browser.execute<[], string>('return window.location.hash');
const dataViewId = await PageObjects.discover.getCurrentDataViewId();
const newHash = originalHash.replace(dataViewId, 'invalid-data-view-id');
await browser.execute(`window.location.hash = "${newHash}"`);
await PageObjects.header.waitUntilLoadingHasFinished();
await retry.try(async () => {
const currentHash = await browser.execute<[], string>('return window.location.hash');
expect(currentHash).to.be(originalHash);
expect(await testSubjects.exists('dscDataViewNotFoundShowSavedWarning')).to.be(true);
});
});
});
});
}

View file

@ -0,0 +1,121 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import expect from '@kbn/expect';
import { FtrProviderContext } from '../ftr_provider_context';
export default function ({ getService, getPageObjects }: FtrProviderContext) {
const browser = getService('browser');
const log = getService('log');
const retry = getService('retry');
const esArchiver = getService('esArchiver');
const kibanaServer = getService('kibanaServer');
const filterBar = getService('filterBar');
const testSubjects = getService('testSubjects');
const appsMenu = getService('appsMenu');
const PageObjects = getPageObjects([
'common',
'discover',
'header',
'timePicker',
'unifiedFieldList',
'visualize',
]);
const defaultSettings = {
defaultIndex: 'logstash-*',
};
describe('discover URL state', () => {
before(async function () {
log.debug('load kibana index with default index pattern');
await kibanaServer.importExport.load('test/functional/fixtures/kbn_archiver/discover');
// and load a set of makelogs data
await esArchiver.loadIfNeeded('test/functional/fixtures/es_archiver/logstash_functional');
await kibanaServer.uiSettings.replace(defaultSettings);
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
});
after(async () => {
await kibanaServer.savedObjects.clean({ types: ['search', 'index-pattern'] });
});
it('should show a warning and fall back to the default data view when navigating to a URL with an invalid data view ID', async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
await PageObjects.header.waitUntilLoadingHasFinished();
const dataViewId = await PageObjects.discover.getCurrentDataViewId();
const originalUrl = await browser.getCurrentUrl();
const newUrl = originalUrl.replace(dataViewId, 'invalid-data-view-id');
await browser.get(newUrl);
await PageObjects.header.waitUntilLoadingHasFinished();
await retry.try(async () => {
expect(await browser.getCurrentUrl()).to.be(originalUrl);
expect(await testSubjects.exists('dscDataViewNotFoundShowDefaultWarning')).to.be(true);
});
});
it('should show a warning and fall back to the current data view if the URL is updated to an invalid data view ID', async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.timePicker.setDefaultAbsoluteRange();
const originalHash = await browser.execute<[], string>('return window.location.hash');
const dataViewId = await PageObjects.discover.getCurrentDataViewId();
const newHash = originalHash.replace(dataViewId, 'invalid-data-view-id');
await browser.execute(`window.location.hash = "${newHash}"`);
await PageObjects.header.waitUntilLoadingHasFinished();
await retry.try(async () => {
const currentHash = await browser.execute<[], string>('return window.location.hash');
expect(currentHash).to.be(originalHash);
expect(await testSubjects.exists('dscDataViewNotFoundShowSavedWarning')).to.be(true);
});
});
it('should sync Lens global state to Discover sidebar link and carry over the state when navigating to Discover', async () => {
await PageObjects.common.navigateToApp('discover');
await PageObjects.common.navigateToApp('lens');
await appsMenu.openCollapsibleNav();
let discoverLink = await appsMenu.getLink('Discover');
expect(discoverLink?.href).to.contain(
'/app/discover#/?_g=(filters:!(),refreshInterval:(pause:!t,value:60000),time:(from:now-15m,to:now))' +
"&_a=(columns:!(),filters:!(),index:'logstash-*',interval:auto,query:(language:kuery,query:''),sort:!(!('@timestamp',desc)))"
);
await appsMenu.closeCollapsibleNav();
await PageObjects.timePicker.setDefaultAbsoluteRange();
await filterBar.addFilter({
field: 'extension.raw',
operation: 'is one of',
value: ['jpg', 'css'],
});
await filterBar.toggleFilterPinned('extension.raw');
await PageObjects.header.waitUntilLoadingHasFinished();
await appsMenu.openCollapsibleNav();
discoverLink = await appsMenu.getLink('Discover');
expect(discoverLink?.href).to.contain(
"/app/discover#/?_g=(filters:!(('$state':(store:globalState)," +
"meta:(alias:!n,disabled:!f,field:extension.raw,index:'logstash-*'," +
'key:extension.raw,negate:!f,params:!(jpg,css),type:phrases,value:!(jpg,css)),' +
'query:(bool:(minimum_should_match:1,should:!((match_phrase:(extension.raw:jpg)),' +
"(match_phrase:(extension.raw:css))))))),query:(language:kuery,query:'')," +
"refreshInterval:(pause:!t,value:60000),time:(from:'2015-09-19T06:31:44.000Z'," +
"to:'2015-09-23T18:31:44.000Z'))&_a=(columns:!(),filters:!(),index:'logstash-*'," +
"interval:auto,query:(language:kuery,query:''),sort:!(!('@timestamp',desc)))"
);
await appsMenu.clickLink('Discover');
await PageObjects.header.waitUntilLoadingHasFinished();
expect(await filterBar.hasFilter('extension.raw', '', undefined, true)).to.be(true);
expect(await filterBar.isFilterPinned('extension.raw')).to.be(true);
expect(await PageObjects.timePicker.getTimeConfig()).to.eql({
start: 'Sep 19, 2015 @ 06:31:44.000',
end: 'Sep 23, 2015 @ 18:31:44.000',
});
expect(await PageObjects.discover.getHitCount()).to.be('11,268');
});
});
}

View file

@ -37,5 +37,6 @@ export default function ({ getService, loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./_inspector'));
loadTestFile(require.resolve('./_date_nanos'));
loadTestFile(require.resolve('./_date_nanos_mixed'));
loadTestFile(require.resolve('./_url_state'));
});
}