Skip to content

Commit

Permalink
test for sneaky DOM manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit committed Aug 20, 2022
1 parent 6e98823 commit 7909220
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions packages/components/src/disabled/test/index.tsx
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';

/**
* Internal dependencies
Expand All @@ -24,6 +24,7 @@ jest.mock( '@wordpress/dom', () => {
Object.defineProperties( element, {
offsetWidth: {
get: () => 1,
configurable: true,
},
} );
}
Expand All @@ -38,7 +39,7 @@ jest.mock( '@wordpress/dom', () => {

describe( 'Disabled', () => {
const Form = () => (
<form>
<form title="form">
<input />
<div title="edit my content" contentEditable tabIndex={ 0 } />
</form>
Expand Down Expand Up @@ -108,11 +109,34 @@ describe( 'Disabled', () => {
// Ideally, we'd have two more test cases here:
//
// - it( 'will disable all fields on component render change' )
// - it( 'will disable all fields on sneaky DOM manipulation' )
//
// Alas, JSDOM does not support MutationObserver:
//
// https://github.com/jsdom/jsdom/issues/639
it( 'will disable all fields on sneaky DOM manipulation', async () => {
render(
<Disabled>
<Form />
</Disabled>
);

const form = screen.getByTitle( 'form' );
form.insertAdjacentHTML(
'beforeend',
'<input title="sneaky input" />'
);
form.insertAdjacentHTML(
'beforeend',
'<div title="sneaky editable content" contentEditable tabIndex={ 0 } />'
);
const sneakyInput = screen.getByTitle( 'sneaky input' );
const sneakyEditable = screen.getByTitle( 'sneaky editable content' );

await waitFor( () => expect( sneakyInput ).toBeDisabled() );
await waitFor( () =>
expect( sneakyEditable ).toHaveAttribute(
'contenteditable',
'false'
)
);
} );

describe( 'Consumer', () => {
function DisabledStatus() {
Expand Down

0 comments on commit 7909220

Please sign in to comment.