Skip to content

Commit

Permalink
[Discover] Fix sorting by _score behavior (#92132)
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal authored Feb 24, 2021
1 parent d0b39b1 commit fdc637d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/plugins/discover/public/__mocks__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
* Side Public License, v 1.
*/

import { IUiSettingsClient } from '../../../../core/public';
import { IUiSettingsClient } from 'kibana/public';
import { SORT_DEFAULT_ORDER_SETTING } from '../../common';

export const configMock = ({
get: (key: string) => {
if (key === 'defaultIndex') {
return 'the-index-pattern-id';
} else if (key === SORT_DEFAULT_ORDER_SETTING) {
return 'desc';
}

return '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 { getStateColumnActions } from './columns';
import { configMock } from '../../../../__mocks__/config';
import { indexPatternMock } from '../../../../__mocks__/index_pattern';
import { indexPatternsMock } from '../../../../__mocks__/index_patterns';
import { Capabilities } from '../../../../../../../core/types';
import { AppState } from '../../discover_state';

function getStateColumnAction(state: {}, setAppState: (state: Partial<AppState>) => void) {
return getStateColumnActions({
capabilities: ({
discover: {
save: false,
},
} as unknown) as Capabilities,
config: configMock,
indexPattern: indexPatternMock,
indexPatterns: indexPatternsMock,
useNewFieldsApi: true,
setAppState,
state,
});
}

describe('Test column actions', () => {
test('getStateColumnActions with empty state', () => {
const setAppState = jest.fn();
const actions = getStateColumnAction({}, setAppState);

actions.onAddColumn('_score');
expect(setAppState).toHaveBeenCalledWith({ columns: ['_score'], sort: [['_score', 'desc']] });
actions.onAddColumn('test');
expect(setAppState).toHaveBeenCalledWith({ columns: ['test'] });
});
test('getStateColumnActions with columns and sort in state', () => {
const setAppState = jest.fn();
const actions = getStateColumnAction(
{ columns: ['first', 'second'], sort: [['first', 'desc']] },
setAppState
);

actions.onAddColumn('_score');
expect(setAppState).toHaveBeenCalledWith({
columns: ['first', 'second', '_score'],
sort: [['first', 'desc']],
});
setAppState.mockClear();
actions.onAddColumn('third');
expect(setAppState).toHaveBeenCalledWith({
columns: ['first', 'second', 'third'],
sort: [['first', 'desc']],
});
setAppState.mockClear();
actions.onRemoveColumn('first');
expect(setAppState).toHaveBeenCalledWith({
columns: ['second'],
sort: [],
});
setAppState.mockClear();
actions.onSetColumns(['first', 'second', 'third']);
expect(setAppState).toHaveBeenCalledWith({
columns: ['first', 'second', 'third'],
});
setAppState.mockClear();

actions.onMoveColumn('second', 0);
expect(setAppState).toHaveBeenCalledWith({
columns: ['second', 'first'],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { Capabilities } from 'kibana/public';
import { Capabilities, IUiSettingsClient } from 'kibana/public';
import { popularizeField } from '../../../helpers/popularize_field';
import { IndexPattern, IndexPatternsContract } from '../../../../kibana_services';
import { AppState } from '../../discover_state';
import { SORT_DEFAULT_ORDER_SETTING } from '../../../../../common';

/**
* Helper function to provide a fallback to a single _source column if the given array of columns
Expand Down Expand Up @@ -54,13 +55,15 @@ export function moveColumn(columns: string[], columnName: string, newIndex: numb

export function getStateColumnActions({
capabilities,
config,
indexPattern,
indexPatterns,
useNewFieldsApi,
setAppState,
state,
}: {
capabilities: Capabilities;
config: IUiSettingsClient;
indexPattern: IndexPattern;
indexPatterns: IndexPatternsContract;
useNewFieldsApi: boolean;
Expand All @@ -72,7 +75,10 @@ export function getStateColumnActions({
popularizeField(indexPattern, columnName, indexPatterns);
}
const columns = addColumn(state.columns || [], columnName, useNewFieldsApi);
setAppState({ columns });
const defaultOrder = config.get(SORT_DEFAULT_ORDER_SETTING);
const sort =
columnName === '_score' && !state.sort?.length ? [['_score', defaultOrder]] : state.sort;
setAppState({ columns, sort });
}

function onRemoveColumn(columnName: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ export function Discover({
() =>
getStateColumnActions({
capabilities,
config,
indexPattern,
indexPatterns,
setAppState,
state,
useNewFieldsApi,
}),
[capabilities, indexPattern, indexPatterns, setAppState, state, useNewFieldsApi]
[capabilities, config, indexPattern, indexPatterns, setAppState, state, useNewFieldsApi]
);

const onOpenInspector = useCallback(() => {
Expand Down

0 comments on commit fdc637d

Please sign in to comment.