- Fix seed-products.ts TS error (implicit any) and BOM handling - Re-run full seed to restore correct Persian encoding in DB - Fix productService query→search param mismatch - Fix IngredientWiki hardcoded port 4000→use settingsStore - Restore seed-products-data.json from git after accidental corruption
112 lines
3.0 KiB
TypeScript
112 lines
3.0 KiB
TypeScript
"use client";
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { render, screen, fireEvent } from '@testing-library/react';
|
|
import React from 'react';
|
|
import Header from '../Header';
|
|
import { useCartStore } from '../../store/cartStore';
|
|
import { usePetStore } from '../../store/usePetStore';
|
|
import { useUserStore } from '../../store/userStore';
|
|
|
|
vi.mock('../../store/cartStore', () => ({
|
|
useCartStore: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../../store/usePetStore', () => ({
|
|
usePetStore: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../../store/userStore', () => ({
|
|
useUserStore: vi.fn(),
|
|
}));
|
|
|
|
describe('Header', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
|
|
vi.mocked(useCartStore).mockReturnValue({
|
|
getTotalItems: () => 3,
|
|
} as any);
|
|
|
|
vi.mocked(usePetStore).mockReturnValue({
|
|
pets: [],
|
|
activePetId: null,
|
|
setActivePet: vi.fn(),
|
|
getActivePet: () => null,
|
|
} as any);
|
|
|
|
vi.mocked(useUserStore).mockReturnValue({
|
|
role: 'User_Guest',
|
|
isLoggedIn: false,
|
|
logout: vi.fn(),
|
|
profile: { firstName: '' },
|
|
} as any);
|
|
});
|
|
|
|
it('renders brand name and login button when guest', () => {
|
|
render(
|
|
<Header
|
|
onNavigate={vi.fn()}
|
|
onShopNavigate={vi.fn()}
|
|
currentView="home"
|
|
onCartOpen={vi.fn()}
|
|
onSearch={vi.fn()}
|
|
onB2BOpen={vi.fn()}
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText('Canina')).toBeInTheDocument();
|
|
expect(screen.getByText('ایران')).toBeInTheDocument();
|
|
expect(screen.getByText('ورود / ثبتنام')).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders user first name and pet selection when logged in', () => {
|
|
vi.mocked(useUserStore).mockReturnValue({
|
|
role: 'User_PetOwner',
|
|
isLoggedIn: true,
|
|
logout: vi.fn(),
|
|
profile: { firstName: 'کوروش' },
|
|
} as any);
|
|
|
|
vi.mocked(usePetStore).mockReturnValue({
|
|
pets: [{ id: 'pet-1', name: 'ملوس', type: 'گربه' }],
|
|
activePetId: 'pet-1',
|
|
setActivePet: vi.fn(),
|
|
getActivePet: () => ({ id: 'pet-1', name: 'ملوس', type: 'گربه' }),
|
|
} as any);
|
|
|
|
render(
|
|
<Header
|
|
onNavigate={vi.fn()}
|
|
onShopNavigate={vi.fn()}
|
|
currentView="home"
|
|
onCartOpen={vi.fn()}
|
|
onSearch={vi.fn()}
|
|
onB2BOpen={vi.fn()}
|
|
/>
|
|
);
|
|
|
|
expect(screen.queryByText('ورود / ثبتنام')).not.toBeInTheDocument();
|
|
expect(screen.getByText('کوروش')).toBeInTheDocument();
|
|
expect(screen.getByText('ملوس')).toBeInTheDocument();
|
|
});
|
|
|
|
it('calls onCartOpen when click on cart button', () => {
|
|
const handleCartOpen = vi.fn();
|
|
render(
|
|
<Header
|
|
onNavigate={vi.fn()}
|
|
onShopNavigate={vi.fn()}
|
|
currentView="home"
|
|
onCartOpen={handleCartOpen}
|
|
onSearch={vi.fn()}
|
|
onB2BOpen={vi.fn()}
|
|
/>
|
|
);
|
|
|
|
const cartBtn = screen.getByText('سبد خرید').closest('button');
|
|
expect(cartBtn).toBeInTheDocument();
|
|
fireEvent.click(cartBtn!);
|
|
expect(handleCartOpen).toHaveBeenCalled();
|
|
});
|
|
});
|