import { describe, test, expect, vi, beforeAll, beforeEach } from 'vitest'; import { fetchProducts, CallObjects, CallMutation } from '@services/Amazon/ManagePage'; import { ApiClient } from '@services/Amazon/ApiClient'; import { useMutation } from '@tanstack/react-query'; import { Product } from '@openapi/index'; vi.mock('@openapi/index', () => { const mockConfiguration = { basePath: '[URL REMOVED] }; return { ApiClient: { amazonProductsCreate: vi.fn() }, AmazonApi: class { async amazonProductsList() { return Promise.resolve({data: "laptop"}); } async amazonProductsCreate() { return Promise.resolve(); } }, Configuration: vi.fn(() => mockConfiguration), }; }); vi.mock('@tanstack/react-query', () => { return { useQuery: vi.fn(() => ({ data: "laptop" })), useMutation: vi.fn(() => ({ mutate: vi.fn(async (formData: Product) => { await ApiClient.amazonProductsCreate(formData); window.location.reload(); }) })), }; }); Object.defineProperty(window, 'location', { value: { configurable: true, reload: vi.fn() }, }); describe('ManagePage services', () => { const pageId = 1; const searchItem = 'laptop'; const formData = { id: 1, name: 'laptop', price: 1000, quantity: 1, description: 'new laptop', owner: 'El_Bruno' }; beforeEach(() => { vi.clearAllMocks(); }); test('Should fetch products', async () => { const spyList = vi.spyOn(ApiClient, 'amazonProductsList'); const data = await fetchProducts(pageId, searchItem); expect(spyList).toHaveBeenCalledWith(pageId, searchItem); expect(data).toContain(searchItem); }); test('Should call fetchProducts', async () => { const { data } = CallObjects(pageId, searchItem); expect(data).toContain(searchItem); }); test('Should create product', async () => { const spyCreate = vi.spyOn(ApiClient, 'amazonProductsCreate'); const result = CallMutation(); await result.mutate(formData) expect(spyCreate).toHaveBeenCalledWith(formData); expect(window.location.reload).toHaveBeenCalled(); }); });