Skip to content

Commit 1b13bea

Browse files
committed
[Discover] Migrate get_sort.js test from mocha to TypeScript (#56011)
* Migrate get_sort.js test from mocha to jest and convert to TypeScript * Add jest test
1 parent 58536f8 commit 1b13bea

8 files changed

Lines changed: 177 additions & 206 deletions

File tree

src/legacy/core_plugins/kibana/public/discover/__tests__/doc_table/lib/get_sort.js

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import '../components/field_chooser/field_chooser';
2828
import { RequestAdapter } from '../../../../../../../plugins/inspector/public';
2929
// doc table
3030
import './doc_table';
31-
import { getSort } from './doc_table/lib/get_sort';
31+
import { getSortArray } from './doc_table/lib/get_sort';
3232
import { getSortForSearchSource } from './doc_table/lib/get_sort_for_search_source';
3333
import * as columnActions from './doc_table/actions/columns';
3434

@@ -525,7 +525,7 @@ function discoverController(
525525
language:
526526
localStorage.get('kibana.userQueryLanguage') || config.get('search:queryLanguage'),
527527
},
528-
sort: getSort.array(savedSearch.sort, $scope.indexPattern),
528+
sort: getSortArray(savedSearch.sort, $scope.indexPattern),
529529
columns:
530530
savedSearch.columns.length > 0 ? savedSearch.columns : config.get('defaultColumns').slice(),
531531
index: $scope.indexPattern.id,
@@ -537,7 +537,7 @@ function discoverController(
537537
}
538538

539539
$state.index = $scope.indexPattern.id;
540-
$state.sort = getSort.array($state.sort, $scope.indexPattern);
540+
$state.sort = getSortArray($state.sort, $scope.indexPattern);
541541

542542
$scope.getBucketIntervalToolTipText = () => {
543543
return i18n.translate('kbn.discover.bucketIntervalTooltip', {
@@ -619,10 +619,7 @@ function discoverController(
619619
if (!sort) return;
620620

621621
// get the current sort from searchSource as array of arrays
622-
const currentSort = getSort.array(
623-
$scope.searchSource.getField('sort'),
624-
$scope.indexPattern
625-
);
622+
const currentSort = getSortArray($scope.searchSource.getField('sort'), $scope.indexPattern);
626623

627624
// if the searchSource doesn't know, tell it so
628625
if (!angular.equals(sort, currentSort)) $scope.fetch();

src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { getSort, getSortArray } from './get_sort';
21+
// @ts-ignore
22+
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
23+
import { IndexPattern } from '../../../../kibana_services';
24+
25+
describe('docTable', function() {
26+
let indexPattern: IndexPattern;
27+
28+
beforeEach(() => {
29+
indexPattern = FixturesStubbedLogstashIndexPatternProvider() as IndexPattern;
30+
});
31+
32+
describe('getSort function', function() {
33+
test('should be a function', function() {
34+
expect(typeof getSort === 'function').toBeTruthy();
35+
});
36+
37+
test('should return an array of objects', function() {
38+
expect(getSort([['bytes', 'desc']], indexPattern)).toEqual([{ bytes: 'desc' }]);
39+
40+
delete indexPattern.timeFieldName;
41+
expect(getSort([['bytes', 'desc']], indexPattern)).toEqual([{ bytes: 'desc' }]);
42+
});
43+
44+
test('should passthrough arrays of objects', () => {
45+
expect(getSort([{ bytes: 'desc' }], indexPattern)).toEqual([{ bytes: 'desc' }]);
46+
});
47+
48+
test('should return an empty array when passed an unsortable field', function() {
49+
expect(getSort([['non-sortable', 'asc']], indexPattern)).toEqual([]);
50+
expect(getSort([['lol_nope', 'asc']], indexPattern)).toEqual([]);
51+
52+
delete indexPattern.timeFieldName;
53+
expect(getSort([['non-sortable', 'asc']], indexPattern)).toEqual([]);
54+
});
55+
56+
test('should return an empty array ', function() {
57+
expect(getSort([], indexPattern)).toEqual([]);
58+
expect(getSort([['foo', 'bar']], indexPattern)).toEqual([]);
59+
expect(getSort([{ foo: 'bar' }], indexPattern)).toEqual([]);
60+
});
61+
});
62+
63+
describe('getSortArray function', function() {
64+
test('should have an array method', function() {
65+
expect(getSortArray).toBeInstanceOf(Function);
66+
});
67+
68+
test('should return an array of arrays for sortable fields', function() {
69+
expect(getSortArray([['bytes', 'desc']], indexPattern)).toEqual([['bytes', 'desc']]);
70+
});
71+
72+
test('should return an array of arrays from an array of elasticsearch sort objects', function() {
73+
expect(getSortArray([{ bytes: 'desc' }], indexPattern)).toEqual([['bytes', 'desc']]);
74+
});
75+
76+
test('should sort by an empty array when an unsortable field is given', function() {
77+
expect(getSortArray([{ 'non-sortable': 'asc' }], indexPattern)).toEqual([]);
78+
expect(getSortArray([{ lol_nope: 'asc' }], indexPattern)).toEqual([]);
79+
80+
delete indexPattern.timeFieldName;
81+
expect(getSortArray([{ 'non-sortable': 'asc' }], indexPattern)).toEqual([]);
82+
});
83+
84+
test('should return an empty array when passed an empty sort array', () => {
85+
expect(getSortArray([], indexPattern)).toEqual([]);
86+
87+
delete indexPattern.timeFieldName;
88+
expect(getSortArray([], indexPattern)).toEqual([]);
89+
});
90+
});
91+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import _ from 'lodash';
21+
import { IndexPattern } from '../../../../../../../../../plugins/data/public';
22+
23+
export type SortPairObj = Record<string, string>;
24+
export type SortPairArr = [string, string];
25+
export type SortPair = SortPairArr | SortPairObj;
26+
export type SortInput = SortPair | SortPair[];
27+
28+
export function isSortable(fieldName: string, indexPattern: IndexPattern) {
29+
const field = indexPattern.getFieldByName(fieldName);
30+
return field && field.sortable;
31+
}
32+
33+
function createSortObject(
34+
sortPair: SortInput,
35+
indexPattern: IndexPattern
36+
): SortPairObj | undefined {
37+
if (
38+
Array.isArray(sortPair) &&
39+
sortPair.length === 2 &&
40+
isSortable(String(sortPair[0]), indexPattern)
41+
) {
42+
const [field, direction] = sortPair as SortPairArr;
43+
return { [field]: direction };
44+
} else if (_.isPlainObject(sortPair) && isSortable(Object.keys(sortPair)[0], indexPattern)) {
45+
return sortPair as SortPairObj;
46+
}
47+
}
48+
49+
/**
50+
* Take a sorting array and make it into an object
51+
* @param {array} sort two dimensional array [[fieldToSort, directionToSort]]
52+
* or an array of objects [{fieldToSort: directionToSort}]
53+
* @param {object} indexPattern used for determining default sort
54+
* @returns Array<{object}> an array of sort objects
55+
*/
56+
export function getSort(sort: SortPair[], indexPattern: IndexPattern): SortPairObj[] {
57+
if (Array.isArray(sort)) {
58+
return sort
59+
.map((sortPair: SortPair) => createSortObject(sortPair, indexPattern))
60+
.filter(sortPairObj => typeof sortPairObj === 'object') as SortPairObj[];
61+
}
62+
return [];
63+
}
64+
65+
/**
66+
* compared to getSort it doesn't return an array of objects, it returns an array of arrays
67+
* [[fieldToSort: directionToSort]]
68+
*/
69+
export function getSortArray(sort: SortPair[], indexPattern: IndexPattern) {
70+
return getSort(sort, indexPattern).map(sortPair => Object.entries(sortPair).pop());
71+
}

0 commit comments

Comments
 (0)