diff --git a/src/plugins/discover/public/__mocks__/config.ts b/src/plugins/discover/public/__mocks__/config.ts index a79c56aa77f3b..977dc5699a57a 100644 --- a/src/plugins/discover/public/__mocks__/config.ts +++ b/src/plugins/discover/public/__mocks__/config.ts @@ -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 ''; diff --git a/src/plugins/discover/public/application/angular/doc_table/actions/columns.test.ts b/src/plugins/discover/public/application/angular/doc_table/actions/columns.test.ts new file mode 100644 index 0000000000000..43044d08f6ac0 --- /dev/null +++ b/src/plugins/discover/public/application/angular/doc_table/actions/columns.test.ts @@ -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) => 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'], + }); + }); +}); diff --git a/src/plugins/discover/public/application/angular/doc_table/actions/columns.ts b/src/plugins/discover/public/application/angular/doc_table/actions/columns.ts index 53ced59b17c5d..8028aa6c08634 100644 --- a/src/plugins/discover/public/application/angular/doc_table/actions/columns.ts +++ b/src/plugins/discover/public/application/angular/doc_table/actions/columns.ts @@ -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 @@ -54,6 +55,7 @@ export function moveColumn(columns: string[], columnName: string, newIndex: numb export function getStateColumnActions({ capabilities, + config, indexPattern, indexPatterns, useNewFieldsApi, @@ -61,6 +63,7 @@ export function getStateColumnActions({ state, }: { capabilities: Capabilities; + config: IUiSettingsClient; indexPattern: IndexPattern; indexPatterns: IndexPatternsContract; useNewFieldsApi: boolean; @@ -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) { diff --git a/src/plugins/discover/public/application/components/discover.tsx b/src/plugins/discover/public/application/components/discover.tsx index d0c839aac5a6c..1c4a5be2f2b24 100644 --- a/src/plugins/discover/public/application/components/discover.tsx +++ b/src/plugins/discover/public/application/components/discover.tsx @@ -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(() => {