Skip to content

Commit

Permalink
fix(DIST-713): Force iframe redraw onLoad (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Prilutskiy authored Feb 4, 2021
1 parent fdf29a6 commit 1c675a0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/core/views/components/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Iframe extends Component {

this.iframeRef = null
this.handleLoad = this.handleLoad.bind(this)
this.triggerIframeRedraw = this.triggerIframeRedraw.bind(this)
this.getRef = this.getRef.bind(this)
}

Expand All @@ -20,8 +21,32 @@ class Iframe extends Component {
this.iframeRef = node
}

handleLoad() {
this.props.onLoad && this.props.onLoad(this.iframeRef)
async handleLoad() {
this.props.onLoad(this.iframeRef)
await this.triggerIframeRedraw()
}

/**
* Tell browser to redraw the iframe. DIST-713.
*
*/
triggerIframeRedraw() {
return new Promise((resolve, reject) => {
if (!this.iframeRef) {
resolve()
return
}

try {
this.iframeRef.style.display = 'none'
setTimeout(() => {
this.iframeRef.style.display = 'block'
resolve()
})
} catch (err) {
reject(err)
}
})
}

render() {
Expand Down Expand Up @@ -50,4 +75,8 @@ Iframe.propTypes = {
style: PropTypes.object,
}

Iframe.defaultProps = {
onLoad: () => {},
}

export default Iframe
21 changes: 21 additions & 0 deletions src/core/views/components/iframe.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react'
import Enzyme, { shallow } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

import Iframe from './iframe'

Enzyme.configure({ adapter: new Adapter() })

describe('Iframe test suite', () => {
it('Trigger iframe redraw right after load', async () => {
const triggerIframeRedrawReal = Iframe.prototype.triggerIframeRedraw
const triggerIframeRedrawMock = jest.fn(() => Promise.resolve())

Iframe.prototype.triggerIframeRedraw = triggerIframeRedrawMock
const popup = shallow(<Iframe src="example.com" />)
await popup.instance().handleLoad()

Iframe.prototype.triggerIframeRedraw = triggerIframeRedrawReal
expect(triggerIframeRedrawMock).toHaveBeenCalledTimes(1)
})
})

0 comments on commit 1c675a0

Please sign in to comment.