Skip to content

Commit

Permalink
feat(WEB-821): Add onScreenChanged callback to popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Cerrato committed Nov 23, 2020
1 parent fed0734 commit 91cffc9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/core/views/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ const popupWrapper = styled(BaseWrapper)`
const offset = (100 - p.size) / 2
if (p.isContained) {
return `
width: calc(${p.size}% - 80px);
height: calc(${p.size}% - 80px);
width: calc(${p.size}% - 80px);
height: calc(${p.size}% - 80px);
top: calc(${offset}% + 40px);
left: calc(${offset}% + 40px);
`
}
return `
width: calc(${p.size}% - 80px);
height: calc(${p.size}% - 80px);
width: calc(${p.size}% - 80px);
height: calc(${p.size}% - 80px);
top: calc(${offset}% + 40px);
left: calc(${offset}% + 40px);
`
Expand Down Expand Up @@ -167,6 +167,7 @@ class Popup extends Component {
this.handleAnimateBeforeClose = this.handleAnimateBeforeClose.bind(this)
this.handleTransitionEnd = this.handleTransitionEnd.bind(this)
this.setWrapperRef = this.setWrapperRef.bind(this)
this.handleFormScreenChanged = callIfEmbedIdMatches(this.handleFormScreenChanged.bind(this), this.props.embedId)
}

componentDidMount () {
Expand All @@ -177,6 +178,7 @@ class Popup extends Component {
window.addEventListener('embed-auto-close-popup', this.handleAutoClose)
window.addEventListener('redirect-after-submit', redirectToUrl)
window.addEventListener('thank-you-screen-redirect', redirectToUrl)
window.addEventListener('form-screen-changed', this.handleFormScreenChanged)
window.tfClosePopup = this.handleClose

setTimeout(() => {
Expand All @@ -197,6 +199,7 @@ class Popup extends Component {
window.removeEventListener('embed-auto-close-popup', this.handleAutoClose)
window.removeEventListener('redirect-after-submit', redirectToUrl)
window.removeEventListener('thank-you-screen-redirect', redirectToUrl)
window.removeEventListener('form-screen-changed', this.handleFormScreenChanged)
delete window.tfClosePopup
}

Expand All @@ -218,6 +221,12 @@ class Popup extends Component {
return closeImagePopup
}

handleFormScreenChanged (event) {
if (this.props.options.onScreenChanged) {
this.props.options.onScreenChanged(event)
}
}

updateIcon (component) {
if (this.props.icon) {
if (!this.iconHTML) {
Expand Down
20 changes: 20 additions & 0 deletions src/core/views/popup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ describe('Popup', () => {
})
})

describe('onScreenChanged callback', () => {
it('should be called when the user execute an interaction that triggers a screen change', () => {
const onScreenChangedMock = jest.fn()
const options = { ...popupOptions, onScreenChanged: onScreenChangedMock }
mount(<Popup embedId={EMBED_ID} options={options} url={URL}/>)

window.dispatchEvent(new CustomEvent('form-screen-changed', { detail: { embedId: EMBED_ID } }))
expect(onScreenChangedMock).toHaveBeenCalledTimes(1)
})

it('should NOT be called when the user execute an interaction that triggers a screen change and the embed ID doesn\'t match', () => {
const onScreenChangedMock = jest.fn()
const options = { ...popupOptions, onScreenChanged: onScreenChangedMock }
mount(<Popup embedId={EMBED_ID} options={options} url={URL}/>)

window.dispatchEvent(new CustomEvent('form-screen-changed', { detail: { embedId: '098765' } }))
expect(onScreenChangedMock).not.toHaveBeenCalled()
})
})

describe(`upon receiving upon 'embed-auto-close-popup' event`, () => {
let onCloseMock
it('does not call onAutoClose callback function when auto close is not enabled', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/core/views/widget.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ describe('Widget', () => {
expect(onSubmitMock).not.toHaveBeenCalled()
})

it('onScreenChanged callback is executed when the user execute an interaction that triggers a screen change', () => {
const onScreenChangedMock = jest.fn()
const options = { onScreenChanged: onScreenChangedMock }
mount(<Widget options={options} url={URL} />)

window.dispatchEvent(new CustomEvent('form-screen-changed'))
expect(onScreenChangedMock).toHaveBeenCalledTimes(1)
})

it('passes event data to onSubmit callback', () => {
const onSubmitMock = jest.fn()
const options = { onSubmit: onSubmitMock }
Expand Down

0 comments on commit 91cffc9

Please sign in to comment.