Skip to content

Commit

Permalink
Merge pull request #87 from IanVS/children-typescript
Browse files Browse the repository at this point in the history
Remove `children` from props of ErrorMessage when using component
  • Loading branch information
bluebill1049 committed Dec 3, 2021
2 parents 27ea14d + f0d5c51 commit 81b0492
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
43 changes: 37 additions & 6 deletions src/ErrorMessage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('ErrorMessage', () => {
expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with flat errors and as with component and render', () => {
it('should render correctly with flat errors and as with element and render', () => {
const { asFragment } = render(
<ErrorMessage
as={<span />}
Expand All @@ -58,7 +58,7 @@ describe('ErrorMessage', () => {
expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with flat errors and as with component and className and render', () => {
it('should render correctly with flat errors and as with element and className and render', () => {
const { asFragment } = render(
<ErrorMessage
as={<span />}
Expand All @@ -72,6 +72,37 @@ describe('ErrorMessage', () => {
expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with flat errors and as with component', () => {
function CustErrComp({ children }: { children: React.ReactNode }) {
return <div>{children}</div>;
}
const { asFragment } = render(
<ErrorMessage
as={CustErrComp}
errors={{ flat: { type: 'flat', message: 'flat' } }}
name="flat"
/>,
);

expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with flat errors and as with component and render', () => {
function CustErrComp({ children }: { children: React.ReactNode }) {
return <div>{children}</div>;
}
const { asFragment } = render(
<ErrorMessage
as={CustErrComp}
errors={{ flat: { type: 'flat', message: 'flat' } }}
name="flat"
render={({ message }) => message}
/>,
);

expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with flat multiple errors and render', () => {
const { asFragment } = render(
<ErrorMessage
Expand Down Expand Up @@ -99,7 +130,7 @@ describe('ErrorMessage', () => {
expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with flat multiple errors and as with component and render', () => {
it('should render correctly with flat multiple errors and as with element and render', () => {
const { asFragment } = render(
<ErrorMessage
as={<div />}
Expand Down Expand Up @@ -204,7 +235,7 @@ describe('ErrorMessage', () => {
expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with nested multiple errors and as with component and render', () => {
it('should render correctly with nested multiple errors and as with element and render', () => {
const { asFragment } = render(
<ErrorMessage
as={<div />}
Expand Down Expand Up @@ -269,7 +300,7 @@ describe('ErrorMessage', () => {
expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with nested errors array and as with component and render', () => {
it('should render correctly with nested errors array and as with element and render', () => {
const { asFragment } = render(
<ErrorMessage
as={<span />}
Expand Down Expand Up @@ -319,7 +350,7 @@ describe('ErrorMessage', () => {
expect(asFragment()).toMatchSnapshot();
});

it('should render correctly with nested multiple errors array and as with component and render', () => {
it('should render correctly with nested multiple errors array and as with element and render', () => {
const { asFragment } = render(
<ErrorMessage
as={<div />}
Expand Down
26 changes: 20 additions & 6 deletions src/__snapshots__/ErrorMessage.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ exports[`ErrorMessage should render correctly with flat errors 1`] = `
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with flat errors and as with component and className and render 1`] = `
exports[`ErrorMessage should render correctly with flat errors and as with component 1`] = `
<DocumentFragment>
<div>
flat
</div>
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with flat errors and as with component and render 1`] = `
<DocumentFragment>
flat
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with flat errors and as with element and className and render 1`] = `
<DocumentFragment>
<span
class="test"
Expand All @@ -18,7 +32,7 @@ exports[`ErrorMessage should render correctly with flat errors and as with compo
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with flat errors and as with component and render 1`] = `
exports[`ErrorMessage should render correctly with flat errors and as with element and render 1`] = `
<DocumentFragment>
<span>
flat
Expand All @@ -44,7 +58,7 @@ exports[`ErrorMessage should render correctly with flat errors and as with strin
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with flat multiple errors and as with component and render 1`] = `
exports[`ErrorMessage should render correctly with flat multiple errors and as with element and render 1`] = `
<DocumentFragment>
<div>
flat
Expand Down Expand Up @@ -86,7 +100,7 @@ exports[`ErrorMessage should render correctly with nested errors array 1`] = `
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with nested errors array and as with component and render 1`] = `
exports[`ErrorMessage should render correctly with nested errors array and as with element and render 1`] = `
<DocumentFragment>
<span>
array
Expand Down Expand Up @@ -116,7 +130,7 @@ exports[`ErrorMessage should render correctly with nested errors object and as w
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with nested multiple errors and as with component and render 1`] = `
exports[`ErrorMessage should render correctly with nested multiple errors and as with element and render 1`] = `
<DocumentFragment>
<div>
object
Expand All @@ -138,7 +152,7 @@ exports[`ErrorMessage should render correctly with nested multiple errors and re
</DocumentFragment>
`;

exports[`ErrorMessage should render correctly with nested multiple errors array and as with component and render 1`] = `
exports[`ErrorMessage should render correctly with nested multiple errors array and as with element and render 1`] = `
<DocumentFragment>
<div>
array
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type AsProps<TAs> = TAs extends undefined
: TAs extends React.ReactElement
? Record<string, any>
: TAs extends React.ComponentType<infer P>
? P
? Omit<P, 'children'>
: TAs extends keyof JSX.IntrinsicElements
? JSX.IntrinsicElements[TAs]
: never;
Expand Down

0 comments on commit 81b0492

Please sign in to comment.