Files
interview-demo-code/frontend/spa/tests/unit/utils/ftch.test.js
Nikita Kiselev 5439e8ef9a
Some checks failed
Telegram Mini App Shop Builder / Compute version metadata (push) Has been cancelled
Telegram Mini App Shop Builder / Run Frontend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run Backend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run PHP_CodeSniffer (push) Has been cancelled
Telegram Mini App Shop Builder / Build module. (push) Has been cancelled
Telegram Mini App Shop Builder / release (push) Has been cancelled
Squashed commit message
2026-03-11 22:08:52 +03:00

80 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { apiFetch, getCart, addToCart, setCoupon, setVoucher } from '@/utils/ftch.js';
import { ofetch } from 'ofetch';
// Мокаем ofetch
vi.mock('ofetch', () => ({
ofetch: {
create: vi.fn(() => vi.fn()),
},
}));
describe('ftch.js', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('apiFetch', () => {
it('должен создавать экземпляр ofetch с правильной конфигурацией', () => {
const fetchInstance = apiFetch;
expect(fetchInstance).toBeDefined();
});
it('должен быть определен', () => {
// Проверяем, что apiFetch экспортирован и определен
expect(apiFetch).toBeDefined();
expect(typeof apiFetch).toBe('function');
});
});
describe('getCart', () => {
it('должен вызывать API с правильным action', async () => {
const mockResponse = { data: { products: [] } };
const mockFetch = vi.fn().mockResolvedValue(mockResponse);
// Мокаем apiFetch
vi.doMock('@/utils/ftch.js', async () => {
const actual = await vi.importActual('@/utils/ftch.js');
return {
...actual,
apiFetch: mockFetch,
};
});
// В реальном тесте нужно будет мокать apiFetch по-другому
// или использовать MSW (Mock Service Worker)
expect(true).toBe(true); // Placeholder
});
});
describe('addToCart', () => {
it('должен отправлять POST запрос с FormData', async () => {
const formData = new FormData();
formData.append('product_id', '1');
formData.append('quantity', '2');
// В реальном тесте нужно мокать apiFetch
expect(true).toBe(true); // Placeholder
});
});
describe('setCoupon', () => {
it('должен отправлять купон в правильном формате', async () => {
const coupon = 'DISCOUNT10';
// В реальном тесте нужно мокать apiFetch
expect(true).toBe(true); // Placeholder
});
});
describe('setVoucher', () => {
it('должен отправлять ваучер в правильном формате', async () => {
const voucher = 'VOUCHER123';
// В реальном тесте нужно мокать apiFetch
expect(true).toBe(true); // Placeholder
});
});
});