Skip to content

Commit

Permalink
Add useDisabled test to cover updates to a component
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Nov 16, 2021
1 parent dba28f4 commit 53fb162
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions packages/compose/src/hooks/use-disabled/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -30,6 +30,7 @@ jest.mock( '@wordpress/dom', () => {
Object.defineProperties( element, {
offsetWidth: {
get: () => 1,
configurable: true,
},
} );
}
Expand All @@ -43,34 +44,20 @@ jest.mock( '@wordpress/dom', () => {
} );

describe( 'useDisabled', () => {
let MutationObserver;

beforeAll( () => {
MutationObserver = window.MutationObserver;
window.MutationObserver = function () {};
window.MutationObserver.prototype = {
observe() {},
disconnect() {},
};
} );

afterAll( () => {
window.MutationObserver = MutationObserver;
} );

const Form = forwardRef( ( _, ref ) => {
const Form = forwardRef( ( { showButton }, ref ) => {
return (
<form ref={ ref }>
<input />
<a href="https://wordpress.org/">A link</a>
<p contentEditable={ true } tabIndex="0"></p>
{ showButton && <button>Button</button> }
</form>
);
} );

function DisabledComponent() {
function DisabledComponent( props ) {
const disabledRef = useDisabled();
return <Form ref={ disabledRef } />;
return <Form ref={ disabledRef } { ...props } />;
}

it( 'will disable all fields', () => {
Expand All @@ -86,4 +73,18 @@ describe( 'useDisabled', () => {
expect( p.hasAttribute( 'tabindex' ) ).toBe( false );
expect( p.hasAttribute( 'disabled' ) ).toBe( false );
} );

it( 'will disable an element rendered in an update to the component', async () => {
const { rerender } = render(
<DisabledComponent showButton={ false } />
);

expect( screen.queryByText( 'Button' ) ).not.toBeInTheDocument();
rerender( <DisabledComponent showButton={ true } /> );

const button = screen.getByText( 'Button' );
await waitFor( () => {
expect( button.hasAttribute( 'disabled' ) ).toBe( true );
} );
} );
} );

0 comments on commit 53fb162

Please sign in to comment.