|
| 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 type { Logger } from '@dd/core/types'; |
| 6 | + |
| 7 | +import { resetEnableWarnings, resolveEnable, validateEnableStrict } from './options'; |
| 8 | + |
| 9 | +const mockLogger: Logger = { |
| 10 | + getLogger: jest.fn(), |
| 11 | + time: jest.fn() as unknown as Logger['time'], |
| 12 | + error: jest.fn(), |
| 13 | + warn: jest.fn(), |
| 14 | + info: jest.fn(), |
| 15 | + debug: jest.fn(), |
| 16 | +}; |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + jest.clearAllMocks(); |
| 20 | + resetEnableWarnings(); |
| 21 | +}); |
| 22 | + |
| 23 | +describe('resolveEnable', () => { |
| 24 | + describe('standard boolean / omitted values', () => { |
| 25 | + const cases = [ |
| 26 | + { |
| 27 | + description: 'return false when the config key is undefined', |
| 28 | + options: {}, |
| 29 | + expected: false, |
| 30 | + }, |
| 31 | + { |
| 32 | + description: 'return false when the config key is null', |
| 33 | + options: { myPlugin: null }, |
| 34 | + expected: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + description: 'return true when the config key is a truthy object without enable', |
| 38 | + options: { myPlugin: { someOther: 'val' } }, |
| 39 | + expected: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + description: 'return true when enable is true', |
| 43 | + options: { myPlugin: { enable: true } }, |
| 44 | + expected: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + description: 'return false when enable is false', |
| 48 | + options: { myPlugin: { enable: false } }, |
| 49 | + expected: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + description: 'return true when enable is undefined (object present)', |
| 53 | + options: { myPlugin: { enable: undefined } }, |
| 54 | + expected: true, |
| 55 | + }, |
| 56 | + ]; |
| 57 | + |
| 58 | + test.each(cases)('should $description', ({ options, expected }) => { |
| 59 | + expect(resolveEnable(options, 'myPlugin', mockLogger)).toBe(expected); |
| 60 | + expect(mockLogger.warn).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('non-boolean coercion with deprecation warning', () => { |
| 65 | + const cases = [ |
| 66 | + { |
| 67 | + description: 'coerce enable: 1 to true and warn', |
| 68 | + options: { myPlugin: { enable: 1 } }, |
| 69 | + expected: true, |
| 70 | + }, |
| 71 | + { |
| 72 | + description: 'coerce enable: 0 to false and warn', |
| 73 | + options: { myPlugin: { enable: 0 } }, |
| 74 | + expected: false, |
| 75 | + }, |
| 76 | + { |
| 77 | + description: 'coerce enable: "true" to true and warn', |
| 78 | + options: { myPlugin: { enable: 'true' } }, |
| 79 | + expected: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + description: 'coerce enable: "" to false and warn', |
| 83 | + options: { myPlugin: { enable: '' } }, |
| 84 | + expected: false, |
| 85 | + }, |
| 86 | + ]; |
| 87 | + |
| 88 | + test.each(cases)('should $description', ({ options, expected }) => { |
| 89 | + expect(resolveEnable(options, 'myPlugin', mockLogger)).toBe(expected); |
| 90 | + expect(mockLogger.warn).toHaveBeenCalledTimes(1); |
| 91 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 92 | + expect.stringContaining('myPlugin.enable'), |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('warn-once behavior', () => { |
| 98 | + test('should only warn once per config key across multiple calls', () => { |
| 99 | + resolveEnable({ myPlugin: { enable: 1 } }, 'myPlugin', mockLogger); |
| 100 | + resolveEnable({ myPlugin: { enable: 'yes' } }, 'myPlugin', mockLogger); |
| 101 | + expect(mockLogger.warn).toHaveBeenCalledTimes(1); |
| 102 | + }); |
| 103 | + |
| 104 | + test('should warn separately for different config keys', () => { |
| 105 | + resolveEnable({ pluginA: { enable: 1 } }, 'pluginA', mockLogger); |
| 106 | + resolveEnable({ pluginB: { enable: 1 } }, 'pluginB', mockLogger); |
| 107 | + expect(mockLogger.warn).toHaveBeenCalledTimes(2); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe('validateEnableStrict', () => { |
| 113 | + test('should not push an error when enable is a boolean', () => { |
| 114 | + const errors: string[] = []; |
| 115 | + validateEnableStrict({ enable: true }, errors); |
| 116 | + expect(errors).toHaveLength(0); |
| 117 | + }); |
| 118 | + |
| 119 | + test('should not push an error when enable is undefined', () => { |
| 120 | + const errors: string[] = []; |
| 121 | + validateEnableStrict({ enable: undefined }, errors); |
| 122 | + expect(errors).toHaveLength(0); |
| 123 | + }); |
| 124 | + |
| 125 | + test('should push an error when enable is a non-boolean', () => { |
| 126 | + const errors: string[] = []; |
| 127 | + validateEnableStrict({ enable: 'yes' }, errors); |
| 128 | + expect(errors).toHaveLength(1); |
| 129 | + expect(errors[0]).toContain('enable'); |
| 130 | + expect(errors[0]).toContain('boolean'); |
| 131 | + }); |
| 132 | +}); |
0 commit comments