-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Add contextConnectWithoutRef() to bypass ref forwarding (#…
…43611) * Remove unused memo option * Clean up ts-expect-errors * Force check two params * Check contextConnect correctness with TS * Add tests * Add more TS tests and fixup types * Add comment * Move conditional upstream
- Loading branch information
Showing
2 changed files
with
113 additions
and
31 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
packages/components/src/ui/context/test/context-connect.tsx
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,55 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ForwardedRef } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { contextConnect, contextConnectWithoutRef } from '../context-connect'; | ||
import type { WordPressComponentProps } from '../wordpress-component'; | ||
|
||
// Static TypeScript tests | ||
describe( 'ref forwarding', () => { | ||
const ComponentWithRef = ( | ||
props: WordPressComponentProps< {}, 'div' >, | ||
ref: ForwardedRef< any > | ||
) => <div { ...props } ref={ ref } />; | ||
const ComponentWithoutRef = ( | ||
props: WordPressComponentProps< {}, 'div' > | ||
) => <div { ...props } />; | ||
|
||
it( 'should not trigger a TS error if components are passed to the correct connect* functions', () => { | ||
contextConnect( ComponentWithRef, 'Foo' ); | ||
contextConnectWithoutRef( ComponentWithoutRef, 'Foo' ); | ||
} ); | ||
|
||
it( 'should trigger a TS error if components are passed to the wrong connect* functions', () => { | ||
// Wrapped in a thunk because React.forwardRef() will throw a console warning if this is executed | ||
// eslint-disable-next-line no-unused-expressions | ||
() => { | ||
// @ts-expect-error This should error | ||
contextConnect( ComponentWithoutRef, 'Foo' ); | ||
}; | ||
|
||
// @ts-expect-error This should error | ||
contextConnectWithoutRef( ComponentWithRef, 'Foo' ); | ||
} ); | ||
|
||
it( 'should result in a component with the correct prop types', () => { | ||
const AcceptsRef = contextConnect( ComponentWithRef, 'Foo' ); | ||
|
||
<AcceptsRef ref={ null } />; | ||
|
||
// @ts-expect-error An unaccepted prop should trigger an error | ||
<AcceptsRef foo={ null } />; | ||
|
||
const NoRef = contextConnectWithoutRef( ComponentWithoutRef, 'Foo' ); | ||
|
||
// @ts-expect-error The ref prop should trigger an error | ||
<NoRef ref={ null } />; | ||
|
||
// @ts-expect-error An unaccepted prop should trigger an error | ||
<NoRef foo={ null } />; | ||
} ); | ||
} ); |