From 1185eb24625abf6adf31fd8f224595cd2ab7dc3e Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Wed, 22 Sep 2021 12:52:17 -0400 Subject: [PATCH 1/2] Add tests for `as` component (failing typescript) --- src/ErrorMessage.test.tsx | 43 +++++++++++++++++--- src/__snapshots__/ErrorMessage.test.tsx.snap | 26 +++++++++--- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/ErrorMessage.test.tsx b/src/ErrorMessage.test.tsx index cbe9c8c..4c8636b 100644 --- a/src/ErrorMessage.test.tsx +++ b/src/ErrorMessage.test.tsx @@ -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( } @@ -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( } @@ -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
{children}
; + } + const { asFragment } = render( + , + ); + + expect(asFragment()).toMatchSnapshot(); + }); + + it('should render correctly with flat errors and as with component and render', () => { + function CustErrComp({ children }: { children: React.ReactNode }) { + return
{children}
; + } + const { asFragment } = render( + message} + />, + ); + + expect(asFragment()).toMatchSnapshot(); + }); + it('should render correctly with flat multiple errors and render', () => { const { asFragment } = render( { 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( } @@ -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( } @@ -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( } @@ -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( } diff --git a/src/__snapshots__/ErrorMessage.test.tsx.snap b/src/__snapshots__/ErrorMessage.test.tsx.snap index 1eee977..f9fc891 100644 --- a/src/__snapshots__/ErrorMessage.test.tsx.snap +++ b/src/__snapshots__/ErrorMessage.test.tsx.snap @@ -8,7 +8,21 @@ exports[`ErrorMessage should render correctly with flat errors 1`] = ` `; -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`] = ` + +
+ flat +
+
+`; + +exports[`ErrorMessage should render correctly with flat errors and as with component and render 1`] = ` + + flat + +`; + +exports[`ErrorMessage should render correctly with flat errors and as with element and className and render 1`] = ` `; -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`] = ` flat @@ -44,7 +58,7 @@ exports[`ErrorMessage should render correctly with flat errors and as with strin `; -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`] = `
flat @@ -86,7 +100,7 @@ exports[`ErrorMessage should render correctly with nested errors array 1`] = ` `; -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`] = ` array @@ -116,7 +130,7 @@ exports[`ErrorMessage should render correctly with nested errors object and as w `; -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`] = `
object @@ -138,7 +152,7 @@ exports[`ErrorMessage should render correctly with nested multiple errors and re `; -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`] = `
array From f0d5c51b3d04006eaa6bf19ed7224a312cbdb0f6 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Wed, 22 Sep 2021 13:02:05 -0400 Subject: [PATCH 2/2] Remove `children` from props of ErrorMessage when using component --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 4f57c08..5a4426a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,7 +17,7 @@ type AsProps = TAs extends undefined : TAs extends React.ReactElement ? Record : TAs extends React.ComponentType - ? P + ? Omit : TAs extends keyof JSX.IntrinsicElements ? JSX.IntrinsicElements[TAs] : never;