|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | + |
| 5 | +import { mockLogFn, mockLogger } from '@dd/tests/_jest/helpers/mocks'; |
| 6 | + |
| 7 | +import { resetEnableWarnings, resolveEnable } from './options'; |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + jest.clearAllMocks(); |
| 11 | + resetEnableWarnings(); |
| 12 | +}); |
| 13 | + |
| 14 | +describe('resolveEnable', () => { |
| 15 | + describe('standard boolean / omitted values', () => { |
| 16 | + const cases = [ |
| 17 | + { |
| 18 | + description: 'return false when the config key is undefined', |
| 19 | + options: {}, |
| 20 | + expected: false, |
| 21 | + }, |
| 22 | + { |
| 23 | + description: 'return false when the config key is null', |
| 24 | + options: { myPlugin: null }, |
| 25 | + expected: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + description: 'return true when the config key is a truthy object without enable', |
| 29 | + options: { myPlugin: { someOther: 'val' } }, |
| 30 | + expected: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + description: 'return true when enable is true', |
| 34 | + options: { myPlugin: { enable: true } }, |
| 35 | + expected: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + description: 'return false when enable is false', |
| 39 | + options: { myPlugin: { enable: false } }, |
| 40 | + expected: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + description: 'return true when enable is undefined (object present)', |
| 44 | + options: { myPlugin: { enable: undefined } }, |
| 45 | + expected: true, |
| 46 | + }, |
| 47 | + ]; |
| 48 | + |
| 49 | + test.each(cases)('should $description', ({ options, expected }) => { |
| 50 | + expect(resolveEnable(options, 'myPlugin', mockLogger)).toBe(expected); |
| 51 | + expect(mockLogFn).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('non-boolean coercion with deprecation warning', () => { |
| 56 | + const cases = [ |
| 57 | + { |
| 58 | + description: 'coerce enable: 1 to true and warn', |
| 59 | + options: { myPlugin: { enable: 1 } }, |
| 60 | + expected: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + description: 'coerce enable: 0 to false and warn', |
| 64 | + options: { myPlugin: { enable: 0 } }, |
| 65 | + expected: false, |
| 66 | + }, |
| 67 | + { |
| 68 | + description: 'coerce enable: "true" to true and warn', |
| 69 | + options: { myPlugin: { enable: 'true' } }, |
| 70 | + expected: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + description: 'coerce enable: "" to false and warn', |
| 74 | + options: { myPlugin: { enable: '' } }, |
| 75 | + expected: false, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + test.each(cases)('should $description', ({ options, expected }) => { |
| 80 | + expect(resolveEnable(options, 'myPlugin', mockLogger)).toBe(expected); |
| 81 | + expect(mockLogFn).toHaveBeenCalledTimes(1); |
| 82 | + expect(mockLogFn).toHaveBeenCalledWith( |
| 83 | + expect.stringContaining('myPlugin.enable'), |
| 84 | + 'warn', |
| 85 | + ); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('warn-once behavior', () => { |
| 90 | + test('should only warn once per config key across multiple calls', () => { |
| 91 | + resolveEnable({ myPlugin: { enable: 1 } }, 'myPlugin', mockLogger); |
| 92 | + resolveEnable({ myPlugin: { enable: 'yes' } }, 'myPlugin', mockLogger); |
| 93 | + expect(mockLogFn).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | + |
| 96 | + test('should warn separately for different config keys', () => { |
| 97 | + resolveEnable({ pluginA: { enable: 1 } }, 'pluginA', mockLogger); |
| 98 | + resolveEnable({ pluginB: { enable: 1 } }, 'pluginB', mockLogger); |
| 99 | + expect(mockLogFn).toHaveBeenCalledTimes(2); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments