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

[7.x] [Discover] Migrate get_sort.js test from mocha to TypeScript (#56011) #56742

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import '../components/field_chooser/field_chooser';
import { RequestAdapter } from '../../../../../../../plugins/inspector/public';
// doc table
import './doc_table';
import { getSort } from './doc_table/lib/get_sort';
import { getSortArray } from './doc_table/lib/get_sort';
import { getSortForSearchSource } from './doc_table/lib/get_sort_for_search_source';
import * as columnActions from './doc_table/actions/columns';

Expand Down Expand Up @@ -525,7 +525,7 @@ function discoverController(
language:
localStorage.get('kibana.userQueryLanguage') || config.get('search:queryLanguage'),
},
sort: getSort.array(savedSearch.sort, $scope.indexPattern),
sort: getSortArray(savedSearch.sort, $scope.indexPattern),
columns:
savedSearch.columns.length > 0 ? savedSearch.columns : config.get('defaultColumns').slice(),
index: $scope.indexPattern.id,
Expand All @@ -537,7 +537,7 @@ function discoverController(
}

$state.index = $scope.indexPattern.id;
$state.sort = getSort.array($state.sort, $scope.indexPattern);
$state.sort = getSortArray($state.sort, $scope.indexPattern);

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

// get the current sort from searchSource as array of arrays
const currentSort = getSort.array(
$scope.searchSource.getField('sort'),
$scope.indexPattern
);
const currentSort = getSortArray($scope.searchSource.getField('sort'), $scope.indexPattern);

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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { getSort, getSortArray } from './get_sort';
// @ts-ignore
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
import { IndexPattern } from '../../../../kibana_services';

describe('docTable', function() {
let indexPattern: IndexPattern;

beforeEach(() => {
indexPattern = FixturesStubbedLogstashIndexPatternProvider() as IndexPattern;
});

describe('getSort function', function() {
test('should be a function', function() {
expect(typeof getSort === 'function').toBeTruthy();
});

test('should return an array of objects', function() {
expect(getSort([['bytes', 'desc']], indexPattern)).toEqual([{ bytes: 'desc' }]);

delete indexPattern.timeFieldName;
expect(getSort([['bytes', 'desc']], indexPattern)).toEqual([{ bytes: 'desc' }]);
});

test('should passthrough arrays of objects', () => {
expect(getSort([{ bytes: 'desc' }], indexPattern)).toEqual([{ bytes: 'desc' }]);
});

test('should return an empty array when passed an unsortable field', function() {
expect(getSort([['non-sortable', 'asc']], indexPattern)).toEqual([]);
expect(getSort([['lol_nope', 'asc']], indexPattern)).toEqual([]);

delete indexPattern.timeFieldName;
expect(getSort([['non-sortable', 'asc']], indexPattern)).toEqual([]);
});

test('should return an empty array ', function() {
expect(getSort([], indexPattern)).toEqual([]);
expect(getSort([['foo', 'bar']], indexPattern)).toEqual([]);
expect(getSort([{ foo: 'bar' }], indexPattern)).toEqual([]);
});
});

describe('getSortArray function', function() {
test('should have an array method', function() {
expect(getSortArray).toBeInstanceOf(Function);
});

test('should return an array of arrays for sortable fields', function() {
expect(getSortArray([['bytes', 'desc']], indexPattern)).toEqual([['bytes', 'desc']]);
});

test('should return an array of arrays from an array of elasticsearch sort objects', function() {
expect(getSortArray([{ bytes: 'desc' }], indexPattern)).toEqual([['bytes', 'desc']]);
});

test('should sort by an empty array when an unsortable field is given', function() {
expect(getSortArray([{ 'non-sortable': 'asc' }], indexPattern)).toEqual([]);
expect(getSortArray([{ lol_nope: 'asc' }], indexPattern)).toEqual([]);

delete indexPattern.timeFieldName;
expect(getSortArray([{ 'non-sortable': 'asc' }], indexPattern)).toEqual([]);
});

test('should return an empty array when passed an empty sort array', () => {
expect(getSortArray([], indexPattern)).toEqual([]);

delete indexPattern.timeFieldName;
expect(getSortArray([], indexPattern)).toEqual([]);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import _ from 'lodash';
import { IndexPattern } from '../../../../../../../../../plugins/data/public';

export type SortPairObj = Record<string, string>;
export type SortPairArr = [string, string];
export type SortPair = SortPairArr | SortPairObj;
export type SortInput = SortPair | SortPair[];

export function isSortable(fieldName: string, indexPattern: IndexPattern) {
const field = indexPattern.getFieldByName(fieldName);
return field && field.sortable;
}

function createSortObject(
sortPair: SortInput,
indexPattern: IndexPattern
): SortPairObj | undefined {
if (
Array.isArray(sortPair) &&
sortPair.length === 2 &&
isSortable(String(sortPair[0]), indexPattern)
) {
const [field, direction] = sortPair as SortPairArr;
return { [field]: direction };
} else if (_.isPlainObject(sortPair) && isSortable(Object.keys(sortPair)[0], indexPattern)) {
return sortPair as SortPairObj;
}
}

/**
* Take a sorting array and make it into an object
* @param {array} sort two dimensional array [[fieldToSort, directionToSort]]
* or an array of objects [{fieldToSort: directionToSort}]
* @param {object} indexPattern used for determining default sort
* @returns Array<{object}> an array of sort objects
*/
export function getSort(sort: SortPair[], indexPattern: IndexPattern): SortPairObj[] {
if (Array.isArray(sort)) {
return sort
.map((sortPair: SortPair) => createSortObject(sortPair, indexPattern))
.filter(sortPairObj => typeof sortPairObj === 'object') as SortPairObj[];
}
return [];
}

/**
* compared to getSort it doesn't return an array of objects, it returns an array of arrays
* [[fieldToSort: directionToSort]]
*/
export function getSortArray(sort: SortPair[], indexPattern: IndexPattern) {
return getSort(sort, indexPattern).map(sortPair => Object.entries(sortPair).pop());
}
Loading