import { describe, it, expect, beforeEach, vi } from 'vitest'; import { mount } from '@vue/test-utils'; import { createPinia, setActivePinia } from 'pinia'; import { createVuetify } from 'vuetify'; import { reactive, ref } from 'vue'; vi.mock('../../resources/js/api/autopodbor'); import AutoFormScreen from '../../resources/js/views/autopodbor/screens/AutoFormScreen.vue'; import ManualFormScreen from '../../resources/js/views/autopodbor/screens/ManualFormScreen.vue'; import { useAutopodborStore } from '../../resources/js/stores/autopodborStore'; const vuetify = createVuetify(); function makeNav() { return { go: vi.fn(), ctx: reactive({ runId: null, competitorId: null, selectedSourceIds: [], loadMsg: '', loadSub: '' }), screen: ref('autoform') }; } function mountScreen(Comp: any, nav: any) { return mount(Comp, { global: { plugins: [vuetify], provide: { autopodborNav: nav } } }); } describe('AutoFormScreen', () => { beforeEach(() => { setActivePinia(createPinia()); vi.clearAllMocks(); }); it('подбор: собирает примеры и зовёт store.search, затем go(list)', async () => { const store = useAutopodborStore(); vi.spyOn(store, 'search').mockResolvedValue({ id: 7, kind: 'search', status: 'queued' } as any); vi.spyOn(store, 'pollRun').mockResolvedValue({ id: 7, kind: 'search', status: 'done' } as any); const nav = makeNav(); const w = mountScreen(AutoFormScreen, nav); const textInputs = w.findAll('input'); await textInputs[0].setValue('okna-kazan.ru'); (w.vm as any).regionCode = 16; const submitBtn = w.findAll('button').find(b => b.text().includes('Подобрать')); await submitBtn!.trigger('click'); await new Promise(r => setTimeout(r, 0)); expect(store.search).toHaveBeenCalled(); const arg = (store.search as any).mock.calls[0][0]; expect(arg.examples).toContain('okna-kazan.ru'); expect(nav.go).toHaveBeenCalledWith('list'); }); }); describe('ManualFormScreen', () => { beforeEach(() => { setActivePinia(createPinia()); vi.clearAllMocks(); }); it('свой конкурент по сайту: зовёт manualStudy и go(detail)', async () => { const store = useAutopodborStore(); vi.spyOn(store, 'manualStudy').mockResolvedValue({ id: 9, kind: 'study', status: 'queued', competitor_id: 3 } as any); vi.spyOn(store, 'pollRun').mockResolvedValue({ id: 9, kind: 'study', status: 'done', competitor_id: 3 } as any); const nav = makeNav(); const w = mountScreen(ManualFormScreen, nav); (w.vm as any).regionCode = 16; const inputs = w.findAll('input'); await inputs[0].setValue('okna-komfort-kzn.ru'); const btn = w.findAll('button').find(b => b.text().includes('Собрать источники')); await btn!.trigger('click'); await new Promise(r => setTimeout(r, 0)); expect(store.manualStudy).toHaveBeenCalled(); expect(nav.go).toHaveBeenCalledWith('detail'); expect(nav.ctx.competitorId).toBe(3); }); });