Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Fix wrong sort order with empty sort URL parameter #97434

Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ describe('Test discover state', () => {
expect(state.getPreviousAppState()).toEqual(stateA);
});
});
describe('Test discover initial state sort handling', () => {
test('Non-empty sort in URL should not fallback to state defaults', async () => {
history = createBrowserHistory();
history.push('/#?_a=(sort:!(!(order_date,desc)))');

state = getState({
getStateDefaults: () => ({ sort: [['fallback', 'desc']] }),
history,
uiSettings: uiSettingsMock,
});
await state.replaceUrlAppState({});
await state.startSync();
expect(state.appStateContainer.getState().sort).toMatchInlineSnapshot(`
Array [
Array [
"order_date",
"desc",
],
]
`);
});
test('Empty sort in URL should allow fallback state defaults', async () => {
history = createBrowserHistory();
history.push('/#?_a=(sort:!())');

state = getState({
getStateDefaults: () => ({ sort: [['fallback', 'desc']] }),
history,
uiSettings: uiSettingsMock,
});
await state.replaceUrlAppState({});
await state.startSync();
expect(state.appStateContainer.getState().sort).toMatchInlineSnapshot(`
Array [
Array [
"fallback",
"desc",
],
]
`);
});
});

describe('Test discover state with legacy migration', () => {
test('migration of legacy query ', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,20 @@ export function getState({
appStateFromUrl.query = migrateLegacyQuery(appStateFromUrl.query);
}

if (appStateFromUrl && appStateFromUrl.sort && !appStateFromUrl.sort.length) {
// If there's an empty array given in the URL, the sort prop should be removed
// This allows the sort prop to be overwritten with the default sorting
delete appStateFromUrl.sort;
}

let initialAppState = handleSourceColumnState(
{
...defaultAppState,
...appStateFromUrl,
},
uiSettings
);

// todo filter source depending on fields fetching flag (if no columns remain and source fetching is enabled, use default columns)
let previousAppState: AppState;
const appStateContainer = createStateContainer<AppState>(initialAppState);
Expand Down
27 changes: 27 additions & 0 deletions test/functional/apps/discover/_shared_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const browser = getService('browser');
const toasts = getService('toasts');
const deployment = getService('deployment');
const dataGrid = getService('dataGrid');

describe('shared links', function describeIndexTests() {
let baseUrl: string;
Expand Down Expand Up @@ -87,6 +88,32 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
);
});

it('should load snapshot URL with empty sort param correctly', async function () {
const expectedUrl =
baseUrl +
'/app/discover?_t=1453775307251#' +
'/?_g=(filters:!(),refreshInterval:(pause:!t,value:0),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:!())';
await browser.navigateTo(expectedUrl);
await PageObjects.discover.waitUntilSearchingHasFinished();
await retry.waitFor('url to contain default sorting', async () => {
// url fallback default sort should have been pushed to URL
const url = await browser.getCurrentUrl();
return url.includes('sort:!(!(%27@timestamp%27,desc))');
});

const row = await dataGrid.getRow({ rowIndex: 0 });
const firstRowText = await Promise.all(
row.map(async (cell) => await cell.getVisibleText())
);

// sorting requested by ES should be correct
expect(firstRowText).to.contain('Sep 22, 2015 @ 23:50:13.253');
});

it('should allow for copying the snapshot URL as a short URL', async function () {
const re = new RegExp(baseUrl + '/goto/[0-9a-f]{32}$');
await PageObjects.share.checkShortenUrl();
Expand Down