|
| 1 | +import cron, { type CronData } from 'cron-validate'; |
| 2 | +import { type Err, type Valid } from 'cron-validate/lib/result'; |
| 3 | +import { type InputOptions } from 'cron-validate/lib/types'; |
| 4 | + |
| 5 | +import { cronValidate } from '../cron-validate'; |
| 6 | +import { |
| 7 | + CRON_VALIDATE_CADENCE_PRESET_ID, |
| 8 | + CRON_VALIDATE_CADENCE_PRESET, |
| 9 | +} from '../cron-validate.constants'; |
| 10 | + |
| 11 | +// Mock the cron-validate library to test our wrapper behavior |
| 12 | +jest.mock('cron-validate'); |
| 13 | +jest.mock('cron-validate/lib/option'); |
| 14 | + |
| 15 | +describe('cronValidate', () => { |
| 16 | + beforeEach(() => { |
| 17 | + jest.clearAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should have registered the Cadence preset', () => { |
| 21 | + // Since the module is already loaded, we can't test the registration call directly |
| 22 | + // Instead, we verify that the constants are properly defined and would be used for registration |
| 23 | + expect(CRON_VALIDATE_CADENCE_PRESET_ID).toBe('cadence'); |
| 24 | + expect(CRON_VALIDATE_CADENCE_PRESET).toEqual( |
| 25 | + expect.objectContaining({ |
| 26 | + presetId: 'cadence', |
| 27 | + useSeconds: false, |
| 28 | + useYears: false, |
| 29 | + useAliases: true, |
| 30 | + useBlankDay: false, |
| 31 | + allowOnlyOneBlankDayField: false, |
| 32 | + allowStepping: true, |
| 33 | + mustHaveBlankDayField: false, |
| 34 | + useLastDayOfMonth: false, |
| 35 | + useLastDayOfWeek: false, |
| 36 | + useNearestWeekday: false, |
| 37 | + useNthWeekdayOfMonth: false, |
| 38 | + }) |
| 39 | + ); |
| 40 | + }); |
| 41 | + it('should have correct field validation ranges in preset', () => { |
| 42 | + expect(CRON_VALIDATE_CADENCE_PRESET.minutes).toEqual({ |
| 43 | + minValue: 0, |
| 44 | + maxValue: 59, |
| 45 | + }); |
| 46 | + expect(CRON_VALIDATE_CADENCE_PRESET.hours).toEqual({ |
| 47 | + minValue: 0, |
| 48 | + maxValue: 23, |
| 49 | + }); |
| 50 | + expect(CRON_VALIDATE_CADENCE_PRESET.daysOfMonth).toEqual({ |
| 51 | + minValue: 0, |
| 52 | + maxValue: 31, |
| 53 | + }); |
| 54 | + expect(CRON_VALIDATE_CADENCE_PRESET.months).toEqual({ |
| 55 | + minValue: 0, |
| 56 | + maxValue: 12, |
| 57 | + }); |
| 58 | + expect(CRON_VALIDATE_CADENCE_PRESET.daysOfWeek).toEqual({ |
| 59 | + minValue: 0, |
| 60 | + maxValue: 6, // Limited to 6 instead of 7 for Cadence |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should call cron-validate with Cadence preset by default', () => { |
| 65 | + const { mockResult, mockCron } = setup(); |
| 66 | + |
| 67 | + const cronString = '0 12 * * *'; |
| 68 | + const result = cronValidate(cronString); |
| 69 | + |
| 70 | + expect(mockCron).toHaveBeenCalledWith(cronString, { |
| 71 | + preset: CRON_VALIDATE_CADENCE_PRESET_ID, |
| 72 | + }); |
| 73 | + expect(result).toBe(mockResult); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should merge custom options with preset', () => { |
| 77 | + const cronString = '0 12 * * *'; |
| 78 | + const customOptions = { |
| 79 | + override: { |
| 80 | + minutes: { |
| 81 | + lowerLimit: 5, |
| 82 | + upperLimit: 55, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }; |
| 86 | + const { mockResult, mockCron, result } = setup({ |
| 87 | + value: cronString, |
| 88 | + options: customOptions, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(mockCron).toHaveBeenCalledWith(cronString, { |
| 92 | + override: { |
| 93 | + minutes: { |
| 94 | + lowerLimit: 5, |
| 95 | + upperLimit: 55, |
| 96 | + }, |
| 97 | + }, |
| 98 | + preset: CRON_VALIDATE_CADENCE_PRESET_ID, |
| 99 | + }); |
| 100 | + expect(result).toBe(mockResult); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return the exact result from cron-validate library', () => { |
| 104 | + const { mockResult, result } = setup({ |
| 105 | + mockIsValid: false, |
| 106 | + mockErrors: ['Test error message'], |
| 107 | + mockCronValue: { |
| 108 | + minutes: '0', |
| 109 | + hours: '12', |
| 110 | + daysOfMonth: '1', |
| 111 | + months: '1', |
| 112 | + daysOfWeek: '1', |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + expect(result).toBe(mockResult); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle empty options parameter', () => { |
| 120 | + const { mockCron } = setup({ |
| 121 | + value: '*/5 * * * *', |
| 122 | + }); |
| 123 | + |
| 124 | + expect(mockCron).toHaveBeenCalledWith('*/5 * * * *', { |
| 125 | + preset: CRON_VALIDATE_CADENCE_PRESET_ID, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle undefined options parameter', () => { |
| 130 | + const { mockCron } = setup({ |
| 131 | + value: '0 0 * * 1-5', |
| 132 | + }); |
| 133 | + |
| 134 | + expect(mockCron).toHaveBeenCalledWith('0 0 * * 1-5', { |
| 135 | + preset: CRON_VALIDATE_CADENCE_PRESET_ID, |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should allow overriding the preset when custom preset option is provided', () => { |
| 140 | + const options: InputOptions = { |
| 141 | + preset: 'some-other-preset', |
| 142 | + override: { |
| 143 | + minutes: { |
| 144 | + lowerLimit: 0, |
| 145 | + upperLimit: 30, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }; |
| 149 | + const value = '0 12 * * *'; |
| 150 | + const { mockCron } = setup({ |
| 151 | + value, |
| 152 | + options, |
| 153 | + }); |
| 154 | + |
| 155 | + expect(mockCron).toHaveBeenCalledWith(value, options); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +type CronValidateResult = Valid<CronData, string[]> | Err<CronData, string[]>; |
| 160 | +type SetupOptions = { |
| 161 | + mockIsValid?: ReturnType<CronValidateResult['isValid']>; |
| 162 | + mockErrors?: ReturnType<CronValidateResult['getError']>; |
| 163 | + mockCronValue?: ReturnType<CronValidateResult['getValue']>; |
| 164 | + value?: string; |
| 165 | + options?: InputOptions; |
| 166 | +}; |
| 167 | + |
| 168 | +function setup({ |
| 169 | + mockIsValid = true, |
| 170 | + mockErrors = [], |
| 171 | + mockCronValue = { |
| 172 | + minutes: '0', |
| 173 | + hours: '12', |
| 174 | + daysOfMonth: '1', |
| 175 | + months: '1', |
| 176 | + daysOfWeek: '1', |
| 177 | + }, |
| 178 | + value = '0 12 * * *', |
| 179 | + options, |
| 180 | +}: SetupOptions = {}) { |
| 181 | + const mockResult: Partial<CronValidateResult> = { |
| 182 | + isValid: jest.fn(() => mockIsValid), |
| 183 | + isError: jest.fn(() => !mockIsValid), |
| 184 | + getError: jest.fn(() => mockErrors), |
| 185 | + getValue: jest.fn(() => mockCronValue), |
| 186 | + }; |
| 187 | + |
| 188 | + const mockCron = cron as jest.MockedFunction<typeof cron>; |
| 189 | + mockCron.mockReturnValue(mockResult as CronValidateResult); |
| 190 | + |
| 191 | + const result = cronValidate(value, options); |
| 192 | + |
| 193 | + return { mockResult, mockCron, cronValidate, result }; |
| 194 | +} |
0 commit comments