Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cypress/helpers/delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function delay(ms) {
return new Promise((resolve) => setTimeout(() => resolve(), ms))
}
15 changes: 15 additions & 0 deletions cypress/helpers/renderReactApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createRoot } from 'react-dom/client'

let root
let rootElement

export function renderReactApp(app) {
if (root) {
root.unmount()
document.body.removeChild(rootElement)
}
rootElement = document.createElement('div')
document.body.appendChild(rootElement)
root = createRoot(rootElement)
root.render(app)
}
58 changes: 58 additions & 0 deletions cypress/integration/SwalRoot.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// <reference types="cypress" />

import React from 'react'
import Swal from 'sweetalert2'
import { delay } from '../helpers/delay'
import { renderReactApp } from '../helpers/renderReactApp'
import withReactContent, { SwalRoot } from '../../src/index'

context('SwalRoot', () => {
it('works', async () => {
const MySwal = withReactContent(Swal)

// 'bad' is the default used when no context value has actually been provided
const CountContext = React.createContext('bad')

const MyApp = () => {
const [count, setCount] = React.useState(1)
const incrementCount = React.useCallback(() => setCount(count => count + 1), [])
return (
<CountContext.Provider value={count}>
<div style={{textAlign: 'center'}}>
<span>{count}</span>{' '}
<button id="increment-count" onClick={incrementCount}>Increment</button>
</div>
<SwalRoot />
<MyComponent />
</CountContext.Provider>
)
}

const MyComponent = () => {
const showPopup = React.useCallback(() => {
MySwal.fire({
html: <MySwalContent />,
})
}, [])
return (
<div style={{textAlign: 'center'}}>
<button id="show-popup" onClick={showPopup}>Show Popup</button>
</div>
)
}

const MySwalContent = () => {
const count = React.useContext(CountContext)
return <span>{count}</span>
}

renderReactApp(<MyApp />)
await delay(100)
document.querySelector('button#show-popup').click()
await delay(100)
expect(MySwal.getHtmlContainer().innerHTML).to.eq('<span>1</span>')
document.querySelector('button#increment-count').click()
await delay(100)
expect(MySwal.getHtmlContainer().innerHTML).to.eq('<span>2</span>')
})
})