feat: layout and overlays migrated to Next.js
This commit is contained in:
1 parent
c5a1b8e5d2
commit
72d2a5600a
42 files changed
+9064
-42
No files matched your search
@@ -0,0 +1,102 @@
|
||||
"use client";
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import CartDrawer from '../CartDrawer';
|
||||
import { useCartStore } from '../../store/cartStore';
|
||||
import { productService } from '../../services/productService';
|
||||
|
||||
vi.mock('../../store/cartStore', () => ({
|
||||
useCartStore: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../services/productService', () => ({
|
||||
productService: {
|
||||
getProducts: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
const mockProduct = {
|
||||
id: 'canhydrox-gag',
|
||||
name: 'Canhydrox GAG',
|
||||
price: '۱,۰۰۰ تومان',
|
||||
priceValue: 1000,
|
||||
category: 'joints',
|
||||
image: '',
|
||||
};
|
||||
|
||||
describe('CartDrawer', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(productService.getProducts).mockResolvedValue([]);
|
||||
});
|
||||
|
||||
it('renders empty cart state when no items in cart', () => {
|
||||
vi.mocked(useCartStore).mockReturnValue({
|
||||
items: [],
|
||||
updateQuantity: vi.fn(),
|
||||
removeItem: vi.fn(),
|
||||
isSubscribed: false,
|
||||
toggleSubscription: vi.fn(),
|
||||
getTotal: () => 0,
|
||||
getSubtotal: () => 0,
|
||||
getDiscount: () => 0,
|
||||
coupon: null,
|
||||
applyCoupon: vi.fn(),
|
||||
addItem: vi.fn(),
|
||||
} as any);
|
||||
|
||||
render(<CartDrawer isOpen={true} onClose={vi.fn()} onCheckout={vi.fn()} />);
|
||||
|
||||
expect(screen.getByText('سبد خرید شما خالی است')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders cart items and total when items are present', () => {
|
||||
vi.mocked(useCartStore).mockReturnValue({
|
||||
items: [{ product: mockProduct, quantity: 2 }],
|
||||
updateQuantity: vi.fn(),
|
||||
removeItem: vi.fn(),
|
||||
isSubscribed: false,
|
||||
toggleSubscription: vi.fn(),
|
||||
getTotal: () => 2000,
|
||||
getSubtotal: () => 2000,
|
||||
getDiscount: () => 0,
|
||||
coupon: null,
|
||||
applyCoupon: vi.fn(),
|
||||
addItem: vi.fn(),
|
||||
} as any);
|
||||
|
||||
render(<CartDrawer isOpen={true} onClose={vi.fn()} onCheckout={vi.fn()} />);
|
||||
|
||||
expect(screen.getByText('Canhydrox GAG')).toBeInTheDocument();
|
||||
expect(screen.getAllByText('۲,۰۰۰').length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it('triggers updateQuantity when plus/minus buttons are clicked', () => {
|
||||
const mockUpdateQuantity = vi.fn();
|
||||
vi.mocked(useCartStore).mockReturnValue({
|
||||
items: [{ product: mockProduct, quantity: 2 }],
|
||||
updateQuantity: mockUpdateQuantity,
|
||||
removeItem: vi.fn(),
|
||||
isSubscribed: false,
|
||||
toggleSubscription: vi.fn(),
|
||||
getTotal: () => 2000,
|
||||
getSubtotal: () => 2000,
|
||||
getDiscount: () => 0,
|
||||
coupon: null,
|
||||
applyCoupon: vi.fn(),
|
||||
addItem: vi.fn(),
|
||||
} as any);
|
||||
|
||||
render(<CartDrawer isOpen={true} onClose={vi.fn()} onCheckout={vi.fn()} />);
|
||||
|
||||
const plusBtn = screen.getByTestId('plus-btn');
|
||||
const minusBtn = screen.getByTestId('minus-btn');
|
||||
|
||||
fireEvent.click(plusBtn);
|
||||
expect(mockUpdateQuantity).toHaveBeenCalledWith('canhydrox-gag', 3);
|
||||
|
||||
fireEvent.click(minusBtn);
|
||||
expect(mockUpdateQuantity).toHaveBeenCalledWith('canhydrox-gag', 1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
"use client";
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import FeaturedProducts from '../FeaturedProducts';
|
||||
import { productService } from '../../services/productService';
|
||||
import { usePetStore } from '../../store/usePetStore';
|
||||
|
||||
vi.mock('../../services/productService', () => ({
|
||||
productService: {
|
||||
getFeaturedProducts: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../store/usePetStore', () => ({
|
||||
usePetStore: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockProducts = [
|
||||
{
|
||||
id: 'prod-1',
|
||||
name: 'Canhydrox GAG',
|
||||
description: 'Joint helper',
|
||||
price: '۱,۰۰۰ تومان',
|
||||
priceValue: 1000,
|
||||
category: 'joints',
|
||||
image: '',
|
||||
suitableFor: 'سگ',
|
||||
symptoms: ['joint_pain'],
|
||||
benefits: 'Strengthens joints',
|
||||
calculateDosage: vi.fn(),
|
||||
},
|
||||
];
|
||||
|
||||
describe('FeaturedProducts', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(usePetStore).mockReturnValue({
|
||||
getActivePet: () => null,
|
||||
} as any);
|
||||
});
|
||||
|
||||
it('renders loading skeletons initially', () => {
|
||||
vi.mocked(productService.getFeaturedProducts).mockReturnValue(new Promise(() => {}));
|
||||
const { container } = render(<FeaturedProducts onProductClick={vi.fn()} onShopNavigate={vi.fn()} />);
|
||||
// Check if skeletons are present (e.g. searching for animate-pulse)
|
||||
expect(container.getElementsByClassName('animate-pulse').length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('renders products once loaded', async () => {
|
||||
vi.mocked(productService.getFeaturedProducts).mockResolvedValue(mockProducts as any);
|
||||
render(<FeaturedProducts onProductClick={vi.fn()} onShopNavigate={vi.fn()} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Canhydrox GAG')).toBeInTheDocument();
|
||||
});
|
||||
expect(screen.getByText('Joint helper')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onProductClick when product card is clicked', async () => {
|
||||
const handleProductClick = vi.fn();
|
||||
vi.mocked(productService.getFeaturedProducts).mockResolvedValue(mockProducts as any);
|
||||
render(<FeaturedProducts onProductClick={handleProductClick} onShopNavigate={vi.fn()} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Canhydrox GAG')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByText('Canhydrox GAG'));
|
||||
expect(handleProductClick).toHaveBeenCalledWith(mockProducts[0]);
|
||||
});
|
||||
|
||||
it('calls onShopNavigate when navigation link is clicked', async () => {
|
||||
const handleShopNavigate = vi.fn();
|
||||
vi.mocked(productService.getFeaturedProducts).mockResolvedValue(mockProducts as any);
|
||||
render(<FeaturedProducts onProductClick={vi.fn()} onShopNavigate={handleShopNavigate} />);
|
||||
|
||||
const navBtn = screen.getByText('مشاهده تمامی محصولات');
|
||||
fireEvent.click(navBtn);
|
||||
expect(handleShopNavigate).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
"use client";
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import Footer from '../Footer';
|
||||
|
||||
describe('Footer', () => {
|
||||
it('renders footer brand text and standard layout elements', () => {
|
||||
render(<Footer onNavigate={vi.fn()} onShopNavigate={vi.fn()} onB2BOpen={vi.fn()} />);
|
||||
|
||||
expect(screen.getByText('کانینا ایران')).toBeInTheDocument();
|
||||
expect(screen.getByText('نماینده رسمی در ایران')).toBeInTheDocument();
|
||||
expect(screen.getByText('تهران، جردن، خیابان سعیدی، ساختمان کانینا، طبقه ۵، واحد ۱۹')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('triggers onNavigate and onShopNavigate when quick links are clicked', () => {
|
||||
const handleNavigate = vi.fn();
|
||||
const handleShopNavigate = vi.fn();
|
||||
const handleB2BOpen = vi.fn();
|
||||
|
||||
render(
|
||||
<Footer
|
||||
onNavigate={handleNavigate}
|
||||
onShopNavigate={handleShopNavigate}
|
||||
onB2BOpen={handleB2BOpen}
|
||||
/>
|
||||
);
|
||||
|
||||
// Click quick link for shop
|
||||
const shopLink = screen.getByText('محصولات تخصصی ۲۰۲۴');
|
||||
fireEvent.click(shopLink);
|
||||
expect(handleShopNavigate).toHaveBeenCalled();
|
||||
|
||||
// Click quick link for blog
|
||||
const blogLink = screen.getByText('مجله سلامت پت (وبلاگ)');
|
||||
fireEvent.click(blogLink);
|
||||
expect(handleNavigate).toHaveBeenCalledWith('blog');
|
||||
|
||||
// Click B2B link
|
||||
const b2bLink = screen.getByText('پنل سفارش عمده (B2B)');
|
||||
fireEvent.click(b2bLink);
|
||||
expect(handleB2BOpen).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,111 @@
|
||||
"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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
"use client";
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import Hero from '../Hero';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
|
||||
vi.mock('../../store/settingsStore', () => ({
|
||||
useSettingsStore: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('Hero', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('renders title and description from settingsStore', () => {
|
||||
const mockGetText = vi.fn().mockImplementation((key, fallback) => {
|
||||
if (key === 'hero_title') return 'Test Title Line 1\nTest Title Line 2';
|
||||
if (key === 'hero_desc') return 'Test Description Text';
|
||||
return fallback;
|
||||
});
|
||||
|
||||
vi.mocked(useSettingsStore).mockImplementation((selector: any) => selector({ getText: mockGetText }));
|
||||
|
||||
render(<Hero onShopNavigate={vi.fn()} />);
|
||||
|
||||
expect(screen.getByText('Test Title Line 1')).toBeInTheDocument();
|
||||
expect(screen.getByText('Test Title Line 2')).toBeInTheDocument();
|
||||
expect(screen.getByText('Test Description Text')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onShopNavigate when "مشاهده محصولات" button is clicked', () => {
|
||||
const mockGetText = vi.fn().mockImplementation((key, fallback) => fallback);
|
||||
vi.mocked(useSettingsStore).mockImplementation((selector: any) => selector({ getText: mockGetText }));
|
||||
|
||||
const handleShopNavigate = vi.fn();
|
||||
render(<Hero onShopNavigate={handleShopNavigate} />);
|
||||
|
||||
const shopBtn = screen.getByText('مشاهده محصولات');
|
||||
fireEvent.click(shopBtn);
|
||||
expect(handleShopNavigate).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import Tooltip from '../Tooltip';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
|
||||
vi.mock('../../store/settingsStore', () => ({
|
||||
useSettingsStore: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('Tooltip', () => {
|
||||
it('should render children correctly', () => {
|
||||
vi.mocked(useSettingsStore).mockReturnValue({});
|
||||
|
||||
render(
|
||||
<Tooltip termKey="mussel">
|
||||
<span>Mussel Term</span>
|
||||
</Tooltip>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Mussel Term')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should display tooltip definition on hover', async () => {
|
||||
const mockTerms = {
|
||||
mussel: {
|
||||
key: 'mussel',
|
||||
term: 'Green Mussel',
|
||||
definition: 'Scientific definition of NZ green mussel',
|
||||
wikiId: 'green-mussel',
|
||||
},
|
||||
};
|
||||
vi.mocked(useSettingsStore).mockReturnValue(mockTerms);
|
||||
|
||||
render(
|
||||
<Tooltip termKey="mussel">
|
||||
<span>Hover Me</span>
|
||||
</Tooltip>
|
||||
);
|
||||
|
||||
const trigger = screen.getByText('Hover Me');
|
||||
fireEvent.mouseEnter(trigger);
|
||||
|
||||
expect(screen.getByText('Green Mussel')).toBeInTheDocument();
|
||||
expect(screen.getByText('Scientific definition of NZ green mussel')).toBeInTheDocument();
|
||||
|
||||
fireEvent.mouseLeave(trigger);
|
||||
// Tooltip should exit (though animations might delay actual removal, under standard RTL fireEvent it updates state immediately)
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user