-
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.
Fix sandbox html refresh bug (#20176)
* Force rerender of Sandbox component when html prop changes
- Loading branch information
1 parent
c5eb962
commit 5a5564b
Showing
2 changed files
with
84 additions
and
4 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
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,75 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import ReactDOM from 'react-dom'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Sandbox from '../'; | ||
|
||
let container; | ||
|
||
const TestWrapper = () => { | ||
const [ html, setHtml ] = useState( | ||
'<iframe class="mock-iframe" src="https://super.embed"></iframe>' | ||
); | ||
|
||
const updateHtml = () => { | ||
setHtml( | ||
'<iframe class="mock-iframe" src="https://another.super.embed"></iframe>' | ||
); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={ updateHtml } className="mock-button"> | ||
Mock Button | ||
</button> | ||
<Sandbox html={ html } /> | ||
</div> | ||
); | ||
}; | ||
|
||
beforeEach( () => { | ||
container = document.createElement( 'div' ); | ||
document.body.appendChild( container ); | ||
} ); | ||
|
||
afterEach( () => { | ||
document.body.removeChild( container ); | ||
container = null; | ||
} ); | ||
|
||
it( 'should rerender with new emdeded content if html prop changes', () => { | ||
act( () => { | ||
ReactDOM.render( <TestWrapper />, container ); | ||
} ); | ||
|
||
const button = container.querySelector( '.mock-button' ); | ||
const iframe = container.querySelector( '.components-sandbox' ); | ||
|
||
let sandboxedIframe = iframe.contentWindow.document.body.querySelector( | ||
'.mock-iframe' | ||
); | ||
|
||
expect( sandboxedIframe.src ).toEqual( 'https://super.embed/' ); | ||
|
||
act( () => { | ||
button.dispatchEvent( | ||
new window.MouseEvent( 'click', { bubbles: true } ) | ||
); | ||
} ); | ||
|
||
sandboxedIframe = iframe.contentWindow.document.body.querySelector( | ||
'.mock-iframe' | ||
); | ||
|
||
expect( sandboxedIframe.src ).toEqual( 'https://another.super.embed/' ); | ||
} ); |