fix toast tests

This commit is contained in:
Pig Fang 2020-06-23 17:52:50 +08:00
parent 7a78c612bf
commit 49b18783ba
2 changed files with 9 additions and 16 deletions

View File

@ -38,7 +38,6 @@ export const ToastContainer: React.FC = () => {
return (
<>
{queue.length}
{queue.map((el, i) => (
<ToastBox
key={el.id}
@ -56,12 +55,13 @@ export const ToastContainer: React.FC = () => {
export class Toast {
private container: HTMLDivElement
constructor() {
constructor(render?: (element: JSX.Element) => void) {
this.container = document.createElement('div')
document.body.appendChild(this.container)
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'test') {
if (render) {
render(<ToastContainer />)
} else {
ReactDOM.render(<ToastContainer />, this.container)
}
}
@ -87,10 +87,7 @@ export class Toast {
}
dispose() {
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'test') {
ReactDOM.unmountComponentAtNode(this.container)
}
ReactDOM.unmountComponentAtNode(this.container)
this.container.remove()
}
}

View File

@ -1,10 +1,8 @@
import React from 'react'
import { render, fireEvent, screen } from '@testing-library/react'
import { Toast, ToastContainer } from '@/scripts/toast'
import { Toast } from '@/scripts/toast'
test('"Toast" class', () => {
render(<ToastContainer />)
const toast = new Toast()
const toast = new Toast(render)
toast.success('success')
expect(document.querySelector('.alert-success')!.textContent).toContain(
@ -29,8 +27,7 @@ test('"Toast" class', () => {
})
test('clear toasts', () => {
render(<ToastContainer />)
const toast = new Toast()
const toast = new Toast(render)
toast.success('success')
toast.info('info')
@ -42,8 +39,7 @@ test('clear toasts', () => {
})
test('close toast manually', () => {
render(<ToastContainer />)
const toast = new Toast()
const toast = new Toast(render)
toast.success('success')
fireEvent.click(screen.getByText('×'))