2026-05-16 08:16:08 +03:00
|
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
|
|
import { mount, flushPromises } from '@vue/test-utils';
|
|
|
|
|
|
import { createVuetify } from 'vuetify';
|
|
|
|
|
|
import TopupDialog from '../../resources/js/components/billing/TopupDialog.vue';
|
|
|
|
|
|
import * as billingApi from '../../resources/js/api/billing';
|
2026-06-22 21:50:13 +03:00
|
|
|
|
import * as redirectUtil from '../../resources/js/utils/redirect';
|
2026-05-16 08:16:08 +03:00
|
|
|
|
|
|
|
|
|
|
vi.mock('../../resources/js/api/billing');
|
2026-06-22 21:50:13 +03:00
|
|
|
|
vi.mock('../../resources/js/utils/redirect');
|
2026-05-16 08:16:08 +03:00
|
|
|
|
|
|
|
|
|
|
const vuetify = createVuetify();
|
|
|
|
|
|
|
|
|
|
|
|
const factory = () =>
|
|
|
|
|
|
mount(TopupDialog, {
|
|
|
|
|
|
global: { plugins: [vuetify] },
|
|
|
|
|
|
props: { modelValue: true },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('TopupDialog.vue', () => {
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
vi.mocked(billingApi.topup).mockResolvedValue({
|
|
|
|
|
|
transaction: {
|
|
|
|
|
|
id: 1,
|
|
|
|
|
|
type: 'topup',
|
|
|
|
|
|
amount_rub: '5000.00',
|
|
|
|
|
|
balance_rub_after: '5000.00',
|
|
|
|
|
|
created_at: '2026-05-16T00:00:00Z',
|
|
|
|
|
|
},
|
|
|
|
|
|
balance_rub: '5000.00',
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('блокирует submit при сумме ниже 100 ₽', async () => {
|
|
|
|
|
|
const wrapper = factory();
|
|
|
|
|
|
(wrapper.vm as unknown as { amount: number | null }).amount = 50;
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
expect((wrapper.vm as unknown as { canSubmit: boolean }).canSubmit).toBe(false);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('разрешает submit при валидной сумме', async () => {
|
|
|
|
|
|
const wrapper = factory();
|
|
|
|
|
|
(wrapper.vm as unknown as { amount: number | null }).amount = 5000;
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
expect((wrapper.vm as unknown as { canSubmit: boolean }).canSubmit).toBe(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('submit вызывает topup и эмитит success с новым балансом', async () => {
|
|
|
|
|
|
const wrapper = factory();
|
|
|
|
|
|
(wrapper.vm as unknown as { amount: number | null }).amount = 5000;
|
|
|
|
|
|
await (wrapper.vm as unknown as { submit: () => Promise<void> }).submit();
|
2026-05-16 08:29:05 +03:00
|
|
|
|
await flushPromises();
|
2026-05-16 08:16:08 +03:00
|
|
|
|
expect(billingApi.topup).toHaveBeenCalledWith(5000);
|
|
|
|
|
|
expect(wrapper.emitted('success')?.[0]).toEqual(['5000.00']);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-05-16 08:29:05 +03:00
|
|
|
|
it('блокирует submit при нечисловом значении (очищенное поле)', async () => {
|
|
|
|
|
|
const wrapper = factory();
|
|
|
|
|
|
(wrapper.vm as unknown as { amount: number }).amount = NaN;
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
expect((wrapper.vm as unknown as { canSubmit: boolean }).canSubmit).toBe(false);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-22 21:50:13 +03:00
|
|
|
|
it('при ответе с confirmation_url делает редирект на оплату, без success', async () => {
|
|
|
|
|
|
vi.mocked(billingApi.topup).mockResolvedValue({
|
|
|
|
|
|
confirmation_url: 'https://yoomoney.ru/checkout/pay_x',
|
|
|
|
|
|
});
|
|
|
|
|
|
const wrapper = factory();
|
|
|
|
|
|
(wrapper.vm as unknown as { amount: number | null }).amount = 5000;
|
|
|
|
|
|
await (wrapper.vm as unknown as { submit: () => Promise<void> }).submit();
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
expect(redirectUtil.redirectTo).toHaveBeenCalledWith('https://yoomoney.ru/checkout/pay_x');
|
|
|
|
|
|
expect(wrapper.emitted('success')).toBeUndefined();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-05-16 08:16:08 +03:00
|
|
|
|
it('показывает ошибку при отказе backend', async () => {
|
|
|
|
|
|
vi.mocked(billingApi.topup).mockRejectedValue(new Error('fail'));
|
|
|
|
|
|
const wrapper = factory();
|
|
|
|
|
|
(wrapper.vm as unknown as { amount: number | null }).amount = 5000;
|
|
|
|
|
|
await (wrapper.vm as unknown as { submit: () => Promise<void> }).submit();
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
expect((wrapper.vm as unknown as { errorMsg: string | null }).errorMsg).not.toBeNull();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|