This commit is contained in:
1 parent
8ef98cb415
commit
e2b6d2acae
3 files changed
+30
-9
No files matched your search
@@ -26,6 +26,11 @@ vi.mock('../../lib/useCatalogMode',()=>({useCatalogMode:()=>state.catalog}));
|
||||
const product={id:'p1',slug:'joint',artNo:'001',name:'Joint Support',scientificTagline:'Daily care',description:'Clinical description',price:'100 تومان',priceValue:100,category:'joints',unit:'tablets',packageSize:100,main_ingredients:[],dosage_logic:'Daily',benefits:'Mobility',symptoms:[],suitableFor:'سگ',analysis:{},feedingAdvice:'With food',image:'/p.png'};
|
||||
beforeEach(()=>{vi.clearAllMocks();Element.prototype.scrollIntoView=vi.fn();state.catalog={isCatalogOnly:false,allowCart:true,showPrices:true,showPreorderBtn:false};vi.mocked(productService.getProductBySlug).mockResolvedValue(product as any);vi.mocked(productService.getProducts).mockResolvedValue({data:[]} as any);});
|
||||
describe('ProductPage',()=>{
|
||||
it('renders server-provided product without waiting for a client product fetch',async()=>{
|
||||
render(<ProductPage productSlug="joint" initialProduct={product as any}/>);
|
||||
expect((await screen.findAllByText('Joint Support')).length).toBeGreaterThan(0);
|
||||
expect(productService.getProductBySlug).not.toHaveBeenCalled();
|
||||
});
|
||||
it('shows loading state until product and related catalog settle',async()=>{let resolve!:(v:any)=>void;vi.mocked(productService.getProductBySlug).mockReturnValueOnce(new Promise(r=>{resolve=r;}));const {container}=render(<ProductPage productSlug="joint"/>);expect(container.querySelector('.animate-pulse')).toBeInTheDocument();resolve(product);await screen.findByTestId('product-desktop-add-to-cart-btn');expect(container.querySelector('.animate-pulse')).not.toBeInTheDocument();expect(productService.getProductBySlug).toHaveBeenCalledWith('joint');});
|
||||
it.each([null,undefined])('handles absent product %s',async value=>{vi.mocked(productService.getProductBySlug).mockResolvedValueOnce(value as any);render(<ProductPage productSlug="missing"/>);expect(await screen.findByText('محصول یافت نشد')).toBeInTheDocument();expect(state.addItem).not.toHaveBeenCalled();});
|
||||
it('handles rejected product request',async()=>{const err=vi.spyOn(console,'error').mockImplementation(()=>{});vi.mocked(productService.getProductBySlug).mockRejectedValueOnce(new Error('offline'));render(<ProductPage productSlug="joint"/>);await screen.findByText('محصول یافت نشد');expect(err).toHaveBeenCalled();err.mockRestore();});
|
||||
|
||||
Reference in new issue
Block a user