|
| 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 | +}); |
0 commit comments