import { describe, it, expect, vi, beforeEach } from 'vitest' import { DOMWrapper, flushPromises, mount } from '@vue/test-utils' import ElementPlus from 'element-plus' import MarkerChips from '@/components/MarkerChips.vue' import { listTagCandidates } from '@/api/markers' vi.mock('@/api/markers', () => ({ addUserTag: vi.fn(), deleteMarker: vi.fn(), filterByMarker: vi.fn(), getMarkersBatch: vi.fn(), listTagCandidates: vi.fn(), })) const systemMarker = { id: 1, objectType: 'FRAGMENT', objectNum: '7', kind: 'SYSTEM', markerName: '来源', markerValue: '学习引导', } const userTag = { id: 2, objectType: 'FRAGMENT', objectNum: '7', kind: 'USER', markerName: '英语', markerValue: null, } const mountChips = () => mount(MarkerChips, { props: { markers: [systemMarker, userTag] }, global: { plugins: [ElementPlus] }, }) beforeEach(() => { vi.clearAllMocks() vi.mocked(listTagCandidates).mockResolvedValue({ code: 200, data: ['英语', '数学'] } as never) }) describe('MarkerChips 打标签搜索选择(闭环-03)', () => { it('系统标记只读、用户标签可点筛选与删除', async () => { const wrapper = mountChips() expect(wrapper.find('.marker-system').text()).toContain('来源=学习引导') expect(wrapper.find('.marker-system').find('.marker-remove').exists()).toBe(false) await wrapper.find('.marker-user').trigger('click') expect(wrapper.emitted('filter')?.[0]?.[0]).toMatchObject({ markerName: '英语' }) await wrapper.find('.marker-remove').trigger('click') expect(wrapper.emitted('remove')?.[0]?.[0]).toMatchObject({ markerName: '英语' }) wrapper.unmount() }) it('打开输入框时取回候选标签(含自动任务名标签)', async () => { const wrapper = mountChips() await wrapper.find('.marker-add-toggle').trigger('click') await flushPromises() expect(listTagCandidates).toHaveBeenCalled() await wrapper.find('[aria-label="新标签名"]').trigger('click'); await flushPromises(); const options = Array.from(document.querySelectorAll('.el-select-dropdown__item')).map(node => node.textContent); expect(options).toContain('英语'); expect(options).toContain('数学'); wrapper.unmount() }) it('可以直接输入候选里没有的新标签', async () => { const wrapper = mountChips() await wrapper.find('.marker-add-toggle').trigger('click') await flushPromises() await wrapper.find('[aria-label="新标签名"]').setValue('物理'); await flushPromises(); const option = Array.from(document.querySelectorAll('.el-select-dropdown__item')).find(node => node.textContent === '物理'); await new DOMWrapper(option!).trigger('click'); await flushPromises(); const confirm = wrapper.find('.marker-add-confirm') await confirm.trigger('click') expect(wrapper.emitted('add')?.[0]?.[0]).toBe('物理') wrapper.unmount() }) })