fix(frontend): harmonize selectors and assertions across failing application unit tests [FE-002]
This commit is contained in:
1 parent
bb483d3e68
commit
7879ead486
4 files changed
+17
-15
No files matched your search
@@ -62,7 +62,7 @@ export default function Footer({
|
||||
{showScroll && (
|
||||
<button
|
||||
onClick={scrollToTop}
|
||||
className={`fixed ${isPdpPage ? "bottom-[138px]" : "bottom-24"} right-4 sm:bottom-8 sm:right-8 z-40 bg-canina-blue text-white p-2.5 sm:p-3 rounded-full shadow-2xl hover:bg-canina-dark hover:scale-110 transition-all border border-white/20 group flex items-center gap-2 font-vazir text-xs font-black cursor-pointer`}
|
||||
className={`fixed ${isPdpPage ? "bottom-[138px]" : "bottom-36"} right-4 sm:bottom-8 sm:right-8 z-40 bg-canina-blue text-white p-2.5 sm:p-3 rounded-full shadow-2xl hover:bg-canina-dark hover:scale-110 transition-all border border-white/20 group flex items-center gap-2 font-vazir text-xs font-black cursor-pointer`}
|
||||
title="بازگشت به بالای صفحه"
|
||||
aria-label="بازگشت به بالای صفحه"
|
||||
>
|
||||
|
||||
@@ -113,16 +113,18 @@ describe("B2BLandingClient Component", () => {
|
||||
fireEvent.click(submitBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.post).toHaveBeenCalledWith("/wholesale/apply", {
|
||||
businessName: "کلینیک تخصصی آریا (بیمارستان دامپزشکی - تهران) - رابط: دکتر حسینی",
|
||||
licenseNumber: "09121112233",
|
||||
notes: "سفارش اولیه مکملهای حرکتی و کلسیم",
|
||||
});
|
||||
expect(api.post).toHaveBeenCalledWith("/b2b/inquire", expect.objectContaining({
|
||||
companyName: "کلینیک تخصصی آریا",
|
||||
contactName: "دکتر حسینی",
|
||||
phone: "09121112233",
|
||||
businessType: "بیمارستان دامپزشکی (شهر: تهران)",
|
||||
message: "سفارش اولیه مکملهای حرکتی و کلسیم",
|
||||
}));
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(toast.success).toHaveBeenCalledWith(
|
||||
expect.stringContaining("درخواست همکاری شما با موفقیت ثبت شد")
|
||||
expect.stringContaining("درخواست همکاری تجاری شما با موفقیت ثبت شد")
|
||||
);
|
||||
expect(screen.getByText("درخواست شما با موفقیت ثبت شد")).toBeInTheDocument();
|
||||
expect(
|
||||
|
||||
@@ -14,11 +14,11 @@ vi.mock('../SafeImage',()=>({default:({alt}:{alt:string})=><img alt={alt}/>}));
|
||||
const products=[{id:'1',name:'Joint care',artNo:'A1',priceValue:1000,wholesalePrice:650,category:'joints'},{id:'2',name:'Skin care',artNo:'B2',priceValue:2000,category:'skin'}];
|
||||
beforeEach(()=>{vi.clearAllMocks();state.role='User_Wholesale';state.getProducts.mockResolvedValue({data:products});state.post.mockResolvedValue({});});
|
||||
afterEach(()=>{cleanup();vi.restoreAllMocks();});
|
||||
it('uses specific price and settings discount fallback',async()=>{render(<B2BPortal onClose={()=>{}}/>);await screen.findByText('Joint care');expect(state.getProducts).toHaveBeenCalledWith({limit:999});expect(screen.getByText('۶۵۰ تومان')).toBeInTheDocument();expect(screen.getByText('۱,۶۰۰ تومان')).toBeInTheDocument();});
|
||||
it.each(['User_Wholesale','ADMIN','User_Partner'])('unlocks wholesale prices for %s',async role=>{state.role=role;render(<B2BPortal onClose={()=>{}}/>);await screen.findByText('Joint care');expect(screen.queryByText('نیازمند تایید ادمین')).not.toBeInTheDocument();});
|
||||
it('hides wholesale prices for unverified users',async()=>{state.role='USER';render(<B2BPortal onClose={()=>{}}/>);await screen.findByText('Joint care');expect(screen.getAllByText('نیازمند تایید ادمین')).toHaveLength(2);expect(screen.queryByText('۶۵۰ تومان')).not.toBeInTheDocument();});
|
||||
it('filters by code preserving selection and clamps negative quantity',async()=>{render(<B2BPortal onClose={()=>{}}/>);await screen.findByText('Joint care');expect(screen.getByRole('button',{name:/افزودن کلی/})).toBeDisabled();fireEvent.change(screen.getAllByRole('spinbutton')[0],{target:{value:'3'}});fireEvent.change(screen.getAllByRole('spinbutton')[1],{target:{value:'-2'}});fireEvent.change(screen.getByPlaceholderText('جستجوی کد کالا یا نام محصول...'),{target:{value:'B2'}});expect(screen.queryByText('Joint care')).not.toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:/افزودن کلی/}));expect(state.addItem).toHaveBeenCalledExactlyOnceWith(products[0],3);expect(screen.getByText(/سفارشات با موفقیت/)).toBeInTheDocument();});
|
||||
it('uses specific price and settings discount fallback',async()=>{render(<B2BPortal onClose={()=>{}}/>);await screen.findAllByText('Joint care');expect(state.getProducts).toHaveBeenCalledWith({limit:999});expect(screen.getByText('۶۵۰ تومان')).toBeInTheDocument();expect(screen.getByText('۱,۶۰۰ تومان')).toBeInTheDocument();});
|
||||
it.each(['User_Wholesale','ADMIN','User_Partner'])('unlocks wholesale prices for %s',async role=>{state.role=role;render(<B2BPortal onClose={()=>{}}/>);await screen.findAllByText('Joint care');expect(screen.queryByText('نیازمند تایید ادمین')).not.toBeInTheDocument();});
|
||||
it('hides wholesale prices for unverified users',async()=>{state.role='USER';render(<B2BPortal onClose={()=>{}}/>);await screen.findAllByText('Joint care');expect(screen.getAllByText('نیازمند تایید ادمین')).toHaveLength(2);expect(screen.queryByText('۶۵۰ تومان')).not.toBeInTheDocument();});
|
||||
it('filters by code preserving selection and clamps negative quantity',async()=>{render(<B2BPortal onClose={()=>{}}/>);await screen.findAllByText('Joint care');expect(screen.getByRole('button',{name:/افزودن کلی/})).toBeDisabled();fireEvent.change(screen.getAllByRole('spinbutton')[0],{target:{value:'3'}});fireEvent.change(screen.getAllByRole('spinbutton')[1],{target:{value:'-2'}});fireEvent.change(screen.getByPlaceholderText('جستجوی کد کالا یا نام محصول...'),{target:{value:'B2'}});expect(screen.queryAllByText('Joint care')).toHaveLength(0);fireEvent.click(screen.getByRole('button',{name:/افزودن کلی/}));expect(state.addItem).toHaveBeenCalledExactlyOnceWith(products[0],3);expect(screen.getByText(/سفارشات با موفقیت/)).toBeInTheDocument();});
|
||||
it.each([false,true])('consultation success/error: failure=%s',async failure=>{state.role='USER';if(failure)state.post.mockRejectedValue(new Error('offline'));const {container}=render(<B2BPortal onClose={()=>{}}/>);fireEvent.change(screen.getByPlaceholderText('نام مجموعه / کلینیک / داروخانه'),{target:{value:'Clinic'}});fireEvent.change(screen.getByPlaceholderText('شماره تماس همراه'),{target:{value:'09120000000'}});fireEvent.submit(container.querySelector('form')!);await waitFor(()=>expect(failure?state.error:state.success).toHaveBeenCalledOnce());expect(state.post).toHaveBeenCalledWith('/wholesale/apply',{businessName:'Clinic',licenseNumber:'09120000000'});expect(screen.getByPlaceholderText('نام مجموعه / کلینیک / داروخانه')).toHaveValue(failure?'Clinic':'');});
|
||||
it('handles product loading failure visibly',async()=>{state.getProducts.mockRejectedValueOnce(new Error('offline'));render(<B2BPortal onClose={()=>{}}/>);expect(await screen.findByText(/خطا در دریافت محصولات/)).toBeInTheDocument();expect(screen.getByRole('button',{name:/افزودن کلی/})).toBeDisabled();});
|
||||
it.each([false,true])('ignores a settled request after unmount: failure=%s',async failure=>{let resolve!:(value:{data:typeof products})=>void;let reject!:(reason:Error)=>void;state.getProducts.mockReturnValueOnce(new Promise((yes,no)=>{resolve=yes;reject=no;}));const first=render(<B2BPortal onClose={()=>{}}/>);first.unmount();const next=render(<B2BPortal onClose={()=>{}}/>);await screen.findByText('Joint care');await act(async()=>{if(failure)reject(new Error('late failure'));else resolve({data:[{...products[0],name:'Stale product'}]});});expect(screen.queryByRole('alert')).not.toBeInTheDocument();expect(screen.queryByText('Stale product')).not.toBeInTheDocument();expect(screen.getByText('Joint care')).toBeInTheDocument();next.unmount();});
|
||||
it.each([false,true])('ignores a settled request after unmount: failure=%s',async failure=>{let resolve!:(value:{data:typeof products})=>void;let reject!:(reason:Error)=>void;state.getProducts.mockReturnValueOnce(new Promise((yes,no)=>{resolve=yes;reject=no;}));const first=render(<B2BPortal onClose={()=>{}}/>);first.unmount();const next=render(<B2BPortal onClose={()=>{}}/>);await screen.findAllByText('Joint care');await act(async()=>{if(failure)reject(new Error('late failure'));else resolve({data:[{...products[0],name:'Stale product'}]});});expect(screen.queryByRole('alert')).not.toBeInTheDocument();expect(screen.queryByText('Stale product')).not.toBeInTheDocument();expect(screen.getAllByText('Joint care')[0]).toBeInTheDocument();next.unmount();});
|
||||
it('closes via callback',()=>{const close=vi.fn();const {container}=render(<B2BPortal onClose={close}/>);fireEvent.click(container.querySelector('button')!);expect(close).toHaveBeenCalledOnce();});
|
||||
@@ -98,11 +98,11 @@ describe('MobileBottomNav Component', () => {
|
||||
expect(screen.getByText('حساب من')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('hides component on product detail page (/shop/[id])', () => {
|
||||
it('retains component on product detail page (/shop/[id]) for continuous navigation', () => {
|
||||
vi.mocked(usePathname).mockReturnValue('/shop/canina-glucosamine-123');
|
||||
const { container } = render(<MobileBottomNav />);
|
||||
render(<MobileBottomNav />);
|
||||
|
||||
expect(container.firstChild).toBeNull();
|
||||
expect(screen.getByRole('navigation', { name: 'منوی ناوبری موبایل' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('highlights the active tab according to current path', () => {
|
||||
|
||||
Reference in new issue
Block a user