|
| 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 | +// @ts-ignore |
| 21 | +import { getQueryParameterActions } from './actions'; |
| 22 | +import { |
| 23 | + FilterManager, |
| 24 | + IndexPatternsContract, |
| 25 | +} from '../../../../../../../../../plugins/data/public'; |
| 26 | +let state: { |
| 27 | + queryParameters: { |
| 28 | + defaultStepSize: number; |
| 29 | + indexPatternId: string; |
| 30 | + predecessorCount: number; |
| 31 | + successorCount: number; |
| 32 | + }; |
| 33 | +}; |
| 34 | +let filterManager: FilterManager; |
| 35 | +let indexPatterns: IndexPatternsContract; |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + indexPatterns = { |
| 39 | + get: () => { |
| 40 | + return { |
| 41 | + popularizeField: jest.fn(), |
| 42 | + }; |
| 43 | + }, |
| 44 | + } as IndexPatternsContract; |
| 45 | + |
| 46 | + filterManager = { |
| 47 | + filters: [], |
| 48 | + addFilters: jest.fn(), |
| 49 | + setFilters: jest.fn(), |
| 50 | + getAppFilters: () => [], |
| 51 | + } as FilterManager; |
| 52 | + |
| 53 | + state = { |
| 54 | + queryParameters: { |
| 55 | + defaultStepSize: 3, |
| 56 | + indexPatternId: 'INDEX_PATTERN_ID', |
| 57 | + predecessorCount: 10, |
| 58 | + successorCount: 10, |
| 59 | + }, |
| 60 | + }; |
| 61 | +}); |
| 62 | + |
| 63 | +describe('context query_parameter actions', function() { |
| 64 | + describe('action addFilter', () => { |
| 65 | + it('should pass the given arguments to the filterManager', () => { |
| 66 | + const { addFilter } = getQueryParameterActions(filterManager, indexPatterns); |
| 67 | + |
| 68 | + addFilter(state)('FIELD_NAME', 'FIELD_VALUE', 'FILTER_OPERATION'); |
| 69 | + |
| 70 | + // get the generated filter |
| 71 | + const generatedFilter = filterManager.addFilters.mock.calls[0][0][0]; |
| 72 | + const queryKeys = Object.keys(generatedFilter.query.match_phrase); |
| 73 | + expect(filterManager.addFilters.mock.calls.length).toBe(1); |
| 74 | + expect(queryKeys[0]).toBe('FIELD_NAME'); |
| 75 | + expect(generatedFilter.query.match_phrase[queryKeys[0]]).toBe('FIELD_VALUE'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should pass the index pattern id to the filterManager', () => { |
| 79 | + const { addFilter } = getQueryParameterActions(filterManager, indexPatterns); |
| 80 | + addFilter(state)('FIELD_NAME', 'FIELD_VALUE', 'FILTER_OPERATION'); |
| 81 | + const generatedFilter = filterManager.addFilters.mock.calls[0][0][0]; |
| 82 | + expect(generatedFilter.meta.index).toBe('INDEX_PATTERN_ID'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + describe('action setPredecessorCount', () => { |
| 86 | + it('should set the predecessorCount to the given value', () => { |
| 87 | + const { setPredecessorCount } = getQueryParameterActions(indexPatterns, filterManager); |
| 88 | + setPredecessorCount(state)(20); |
| 89 | + expect(state.queryParameters.predecessorCount).toBe(20); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should limit the predecessorCount to 0 as a lower bound', () => { |
| 93 | + const { setPredecessorCount } = getQueryParameterActions(indexPatterns, filterManager); |
| 94 | + setPredecessorCount(state)(-1); |
| 95 | + expect(state.queryParameters.predecessorCount).toBe(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should limit the predecessorCount to 10000 as an upper bound', () => { |
| 99 | + const { setPredecessorCount } = getQueryParameterActions(indexPatterns, filterManager); |
| 100 | + setPredecessorCount(state)(20000); |
| 101 | + expect(state.queryParameters.predecessorCount).toBe(10000); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments