Skip to content

Commit

Permalink
[Discover] Fix wrong sort order with empty sort URL parameter (elasti…
Browse files Browse the repository at this point in the history
…c#97434)

Co-authored-by: Tim Roes <mail@timroes.de>
# Conflicts:
#	src/plugins/discover/public/application/angular/discover_state.ts
  • Loading branch information
kertal committed Apr 20, 2021
1 parent f6e2101 commit 82235b9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
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,14 +170,21 @@ export function getState({
appStateFromUrl.query = migrateLegacyQuery(appStateFromUrl.query);
}

if (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 fetchinbg flag (if no columns remain and source fetching is enabled, use default columns)

// 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 @@ -110,6 +111,32 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const actualUrl = await PageObjects.share.getSharedUrl();
expect(actualUrl).to.be(expectedUrl);
});

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');
});
});
});

Expand Down

0 comments on commit 82235b9

Please sign in to comment.