fix: remove redundant timer, add navigation verification to CartSnackbar tests
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { useEffect } from 'react'
|
||||
import Alert from '@mui/material/Alert'
|
||||
import Button from '@mui/material/Button'
|
||||
import Snackbar from '@mui/material/Snackbar'
|
||||
@@ -10,13 +9,10 @@ export function CartSnackbar() {
|
||||
const open = useUnit($cartSnackOpen)
|
||||
const navigate = useNavigate()
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const timer = setTimeout(() => cartDismissed(), 4000)
|
||||
return () => clearTimeout(timer)
|
||||
}, [open])
|
||||
|
||||
const handleClose = () => cartDismissed()
|
||||
const handleClose = (_event: React.SyntheticEvent | Event, reason?: string) => {
|
||||
if (reason === 'clickaway') return
|
||||
cartDismissed()
|
||||
}
|
||||
|
||||
const handleGoToCart = () => {
|
||||
cartDismissed()
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { render, screen, fireEvent, act } from '@testing-library/react'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { MemoryRouter } from 'react-router-dom'
|
||||
import { MemoryRouter, useLocation } from 'react-router-dom'
|
||||
import { cartAdded, cartDismissed } from '@/shared/model/cart-notifications'
|
||||
import { CartSnackbar } from '@/shared/ui/CartSnackbar'
|
||||
|
||||
function LocationSpy() {
|
||||
const location = useLocation()
|
||||
;(window as any).__testLocation = location
|
||||
return null
|
||||
}
|
||||
|
||||
function renderWithRouter() {
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<LocationSpy />
|
||||
<CartSnackbar />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
@@ -50,6 +57,7 @@ describe('CartSnackbar', () => {
|
||||
cartAdded()
|
||||
const goBtn = screen.getByRole('button', { name: /перейти в корзину/i })
|
||||
fireEvent.click(goBtn)
|
||||
expect((window as any).__testLocation.pathname).toBe('/cart')
|
||||
expect(screen.queryByText(/товар добавлен/i)).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user