-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Add createComponent unit tests (#28949)
- Loading branch information
1 parent
db8da15
commit 80d77fd
Showing
1 changed file
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createComponent } from '../create-component'; | ||
|
||
describe( 'createComponent', () => { | ||
/** | ||
* @param {import('@wp-g2/create-styles').ViewOwnProps<{}, 'output'>} props | ||
*/ | ||
const useHook = ( props ) => ( { ...props, 'data-hook-test-prop': true } ); | ||
const name = 'Output'; | ||
const MemoizedOutput = createComponent( { | ||
as: 'output', | ||
name, | ||
useHook, | ||
} ); | ||
const Output = createComponent( { | ||
as: 'output', | ||
name, | ||
useHook, | ||
memo: false, | ||
} ); | ||
|
||
it( 'should create a output component', () => { | ||
const { container } = render( | ||
<MemoizedOutput>Example output</MemoizedOutput> | ||
); | ||
|
||
expect( container.firstChild.tagName ).toBe( 'OUTPUT' ); | ||
expect( container.firstChild.innerHTML ).toBe( 'Example output' ); | ||
} ); | ||
|
||
it( 'should create a memoized, ref-forwarded component by default', () => { | ||
expect( MemoizedOutput.$$typeof ).toEqual( Symbol.for( 'react.memo' ) ); | ||
const ref = createRef(); | ||
const wrapper = render( | ||
<MemoizedOutput ref={ ref }>Example output</MemoizedOutput> | ||
); | ||
|
||
expect( ref.current ).toEqual( wrapper.container.firstChild ); | ||
} ); | ||
|
||
it( 'should create a non-memoized ref-forwarded ouput component', () => { | ||
expect( Output.$$typeof ).toEqual( Symbol.for( 'react.forward_ref' ) ); | ||
const ref = createRef(); | ||
const wrapper = render( <Output ref={ ref }>Example output</Output> ); | ||
|
||
expect( ref.current ).toEqual( wrapper.container.firstChild ); | ||
} ); | ||
|
||
it( 'should apply the hook to the component props', () => { | ||
const { container } = render( <Output>Example output</Output> ); | ||
expect( container.firstChild.dataset.hookTestProp ).toBe( 'true' ); | ||
} ); | ||
|
||
it( 'should connect the component to its context', () => { | ||
expect( MemoizedOutput.__contextSystemKey__ ).toContain( 'Output' ); | ||
} ); | ||
} ); |