|
| 1 | +// Copyright 2022 The Parca Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +import {describe, expect, it} from 'vitest'; |
| 15 | + |
| 16 | +import {filterEmptyParams} from './index'; |
| 17 | + |
| 18 | +describe('filterEmptyParams', () => { |
| 19 | + it('should return an array with 2 elements when given object with 2 valid and multiple invalid values', () => { |
| 20 | + const input = { |
| 21 | + validString: 'hello', |
| 22 | + validArray: ['item1', 'item2'], |
| 23 | + emptyString: '', |
| 24 | + undefinedValue: undefined, |
| 25 | + emptyArray: [], |
| 26 | + anotherEmptyString: '', |
| 27 | + }; |
| 28 | + |
| 29 | + const result = filterEmptyParams(input); |
| 30 | + const resultEntries = Object.entries(result); |
| 31 | + |
| 32 | + expect(resultEntries).toHaveLength(2); |
| 33 | + expect(result).toEqual({ |
| 34 | + validString: 'hello', |
| 35 | + validArray: ['item1', 'item2'], |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should filter out empty strings', () => { |
| 40 | + const input = { |
| 41 | + valid: 'test', |
| 42 | + empty: '', |
| 43 | + }; |
| 44 | + |
| 45 | + const result = filterEmptyParams(input); |
| 46 | + expect(result).toEqual({valid: 'test'}); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should filter out undefined values', () => { |
| 50 | + const input = { |
| 51 | + valid: 'test', |
| 52 | + notDefined: undefined, |
| 53 | + }; |
| 54 | + |
| 55 | + const result = filterEmptyParams(input); |
| 56 | + expect(result).toEqual({valid: 'test'}); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should filter out empty arrays', () => { |
| 60 | + const input = { |
| 61 | + valid: 'test', |
| 62 | + emptyArray: [], |
| 63 | + nonEmptyArray: ['item'], |
| 64 | + }; |
| 65 | + |
| 66 | + const result = filterEmptyParams(input); |
| 67 | + expect(result).toEqual({ |
| 68 | + valid: 'test', |
| 69 | + nonEmptyArray: ['item'], |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should keep all valid values including numbers, booleans, and objects', () => { |
| 74 | + const input = { |
| 75 | + string: 'test', |
| 76 | + number: 0, |
| 77 | + boolean: false, |
| 78 | + object: {key: 'value'}, |
| 79 | + array: ['item'], |
| 80 | + }; |
| 81 | + |
| 82 | + const result = filterEmptyParams(input); |
| 83 | + expect(result).toEqual(input); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return empty object when all values are invalid', () => { |
| 87 | + const input = { |
| 88 | + empty1: '', |
| 89 | + empty2: '', |
| 90 | + undefined1: undefined, |
| 91 | + emptyArray: [], |
| 92 | + }; |
| 93 | + |
| 94 | + const result = filterEmptyParams(input); |
| 95 | + expect(result).toEqual({}); |
| 96 | + }); |
| 97 | +}); |
0 commit comments