-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for validateProps function
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
packages/aws-cdk-lib/core/test/helpers-internal/validate-props.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Construct } from 'constructs'; | ||
import { ValidationError } from '../../lib/errors'; | ||
import { validateProps, ValidationRule } from '../../lib/helpers-internal/validate-props'; | ||
|
||
class MockConstruct extends Construct { | ||
constructor() { | ||
super(undefined as any, 'MockConstruct'); | ||
} | ||
} | ||
|
||
describe('validateProps', () => { | ||
let mockScope: Construct; | ||
|
||
beforeEach(() => { | ||
mockScope = new MockConstruct(); | ||
}); | ||
|
||
it('should not throw an error when all validations pass', () => { | ||
const props = { value: 5 }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.value < 0, | ||
message: (p) => `Value ${p.value} should be non-negative`, | ||
}, | ||
]; | ||
|
||
expect(() => validateProps(mockScope, 'TestClass', props, rules)).not.toThrow(); | ||
}); | ||
|
||
it('should throw a ValidationError when a validation fails', () => { | ||
const props = { value: -5 }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.value < 0, | ||
message: (p) => `Value ${p.value} should be non-negative`, | ||
}, | ||
]; | ||
|
||
expect(() => validateProps(mockScope, 'TestClass', props, rules)).toThrow(ValidationError); | ||
}); | ||
|
||
it('should include all failed validation messages in the error', () => { | ||
const props = { value1: -5, value2: 15 }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.value1 < 0, | ||
message: (p) => `Value1 ${p.value1} should be non-negative`, | ||
}, | ||
{ | ||
condition: (p) => p.value2 > 10, | ||
message: (p) => `Value2 ${p.value2} should be 10 or less`, | ||
}, | ||
]; | ||
|
||
expect(() => validateProps(mockScope, 'TestClass', props, rules)).toThrow(ValidationError); | ||
try { | ||
validateProps(mockScope, 'TestClass', props, rules); | ||
} catch (error) { | ||
if (error instanceof ValidationError) { | ||
expect(error.message).toBe( | ||
'TestClass initialization failed due to the following validation error(s):\n' + | ||
'- Value1 -5 should be non-negative\n' + | ||
'- Value2 15 should be 10 or less', | ||
); | ||
} | ||
} | ||
}); | ||
|
||
it('should work with complex object structures', () => { | ||
const props = { nested: { value: 'invalid' } }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.nested.value !== 'valid', | ||
message: (p) => `Nested value "${p.nested.value}" is not valid`, | ||
}, | ||
]; | ||
|
||
expect(() => validateProps(mockScope, 'TestClass', props, rules)).toThrow(ValidationError); | ||
try { | ||
validateProps(mockScope, 'TestClass', props, rules); | ||
} catch (error) { | ||
if (error instanceof ValidationError) { | ||
expect(error.message).toBe( | ||
'TestClass initialization failed due to the following validation error(s):\n' + | ||
'- Nested value "invalid" is not valid', | ||
); | ||
} | ||
} | ||
}); | ||
}); |