Skip to content

Commit

Permalink
test: inherited props
Browse files Browse the repository at this point in the history
  • Loading branch information
jonlambert committed Apr 26, 2022
1 parent 667436c commit f2fb783
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function propose<
OriginalProps extends ComponentProps<ComponentType>,
SuppliedProps extends Partial<OriginalProps>
>(Component: ComponentType, props: SuppliedProps, displayName?: string) {
type FinalProps = Omit<OriginalProps, keyof SuppliedProps> & Partial<SuppliedProps>;
type FinalProps = Omit<OriginalProps, keyof SuppliedProps> &
Partial<SuppliedProps>;

const NewComponent: React.FC<FinalProps> = (p) => {
const combinedProps = { ...props, ...p };
Expand Down
21 changes: 21 additions & 0 deletions src/propose.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('propose', () => {
const Decorated = propose(Base, { message: 'hello, world' }, 'Decorated');
expect(Decorated.displayName).toBe('Decorated');
});

it('renders correctly', () => {
const Base: React.FC<{ message: string; title?: string }> = ({
message,
Expand Down Expand Up @@ -52,4 +53,24 @@ describe('propose', () => {
const { getByRole } = render(<Decorated title="Foo" message="Bar" />);
expect(getByRole('heading')).toHaveTextContent('Foo');
});

it('respects inherited props', () => {
interface SharedLinkProps {
variant: 'primary' | 'secondary';
}
interface LinkButtonProps
extends SharedLinkProps,
Pick<
React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>,
'onClick'
> {}

const Base: React.FC<LinkButtonProps> = () => <div></div>;

const Derived = propose(Base, { variant: 'primary' });
render(<Derived onClick={() => {}} variant="primary" />);
});
});

0 comments on commit f2fb783

Please sign in to comment.