Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { validateProps } from '../validate-props';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the components package, the tests were importing from the lib directory, but the build failed here with that approach.

import { isDevelopment } from '../../is-development';

jest.mock('../../is-development', () => ({ isDevelopment: false }));

test('does nothing in production builds', () => {
expect(isDevelopment).toBe(false);
expect(() => validateProps('TestComponent', { variant: 'foo' }, ['variant'], {}, 'default')).not.toThrow();
});
26 changes: 26 additions & 0 deletions src/internal/base-component/__tests__/validate-props.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { validateProps } from '../validate-props';

test('should pass validation', () => {
expect(() => validateProps('TestComponent', {}, [], {}, 'default')).not.toThrow();
expect(() =>
validateProps('TestComponent', { variant: 'foo' }, ['bar'], { variant: ['foo'] }, 'default')
).not.toThrow();
expect(() =>
validateProps('TestComponent', { variant: undefined }, ['bar'], { variant: ['foo'] }, 'default')
).not.toThrow();
});

test('should throw error when excluded prop is used', () => {
expect(() => validateProps('TestComponent', { variant: 'foo' }, ['variant'], {}, 'default')).toThrow(
new Error('TestComponent does not support "variant" property when used in default system')
);
});

test('should throw error when invalid prop is used', () => {
expect(() => validateProps('TestComponent', { variant: 'foo' }, [], { variant: ['bar'] }, 'default')).toThrow(
new Error('TestComponent does not support "variant" with value "foo" when used in default system')
);
});
26 changes: 26 additions & 0 deletions src/internal/base-component/validate-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { isDevelopment } from '../is-development';

export function validateProps(
componentName: string,
props: Record<string, any>,
excludedProps: Array<string>,
allowedEnums: Record<string, Array<string>>,
systemName: string
) {
if (!isDevelopment) {
return;
}
for (const [prop, value] of Object.entries(props)) {
if (excludedProps.includes(prop)) {
throw new Error(`${componentName} does not support "${prop}" property when used in ${systemName} system`);
}
if (value && allowedEnums[prop] && !allowedEnums[prop].includes(value)) {
throw new Error(
`${componentName} does not support "${prop}" with value "${value}" when used in ${systemName} system`
);
}
}
}
1 change: 1 addition & 0 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export { default as circleIndex } from './utils/circle-index';
export { default as Portal, PortalProps } from './portal';
export { useMergeRefs } from './use-merge-refs';
export { useRandomId, useUniqueId } from './use-unique-id';
export { validateProps } from './base-component/validate-props';
Loading