fix: use vi.mock for navigate test, fix import order in CartSnackbar
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import Alert from '@mui/material/Alert'
|
||||
import Button from '@mui/material/Button'
|
||||
import Snackbar from '@mui/material/Snackbar'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useUnit } from 'effector-react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { $cartSnackOpen, cartDismissed } from '@/shared/model/cart-notifications'
|
||||
|
||||
export function CartSnackbar() {
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
import { render, screen, fireEvent, act } from '@testing-library/react'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { MemoryRouter, useLocation } from 'react-router-dom'
|
||||
import { MemoryRouter } from 'react-router-dom'
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
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
|
||||
}
|
||||
const navigateMock = vi.fn()
|
||||
|
||||
vi.mock('react-router-dom', async (importOriginal) => {
|
||||
const mod = await importOriginal<typeof import('react-router-dom')>()
|
||||
return { ...mod, useNavigate: () => navigateMock }
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
navigateMock.mockClear()
|
||||
})
|
||||
|
||||
function renderWithRouter() {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<LocationSpy />
|
||||
<CartSnackbar />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
@@ -55,9 +59,8 @@ describe('CartSnackbar', () => {
|
||||
it('navigates to /cart and closes on "Перейти в корзину" click', () => {
|
||||
renderWithRouter()
|
||||
cartAdded()
|
||||
const goBtn = screen.getByRole('button', { name: /перейти в корзину/i })
|
||||
fireEvent.click(goBtn)
|
||||
expect((window as any).__testLocation.pathname).toBe('/cart')
|
||||
fireEvent.click(screen.getByRole('button', { name: /перейти в корзину/i }))
|
||||
expect(navigateMock).toHaveBeenCalledWith('/cart')
|
||||
expect(screen.queryByText(/товар добавлен/i)).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user