import { defineComponent, h, reactive } from 'vue' import { flushPromises, mount } from '@vue/test-utils' import { beforeEach, describe, expect, it, vi } from 'vitest' import InventoryView from './InventoryView.vue' const mocks = vi.hoisted(() => ({ route: { query: {} as Record }, push: vi.fn() })) vi.mock('vue-router', () => ({ useRoute: () => mocks.route, useRouter: () => ({ push: mocks.push }) })) const button = defineComponent({ emits: ['click'], setup(_, { slots, emit }) { return () => h('button', { onClick: () => emit('click') }, slots.default?.()) } }) const options = { global: { stubs: { AnalyticsView: defineComponent({ name: 'AnalyticsView', props: ['mode'], setup(_, { slots }) { return () => h('section', { class: 'search-region' }, slots['view-tabs']?.()) } }), InventoryTurnoverView: defineComponent({ name: 'InventoryTurnoverView', setup(_, { slots }) { return () => h('section', { class: 'search-region' }, slots['view-tabs']?.()) } }), 'el-button': button } } } beforeEach(() => { mocks.route = reactive({ query: {} }); mocks.push.mockReset() }) describe('inventory view entry', () => { it('defaults to turnover and switches to detail without reusing turnover filters', async () => { mocks.route.query = { warehouseCode: 'US02', focus: 'FROZEN' } const w = mount(InventoryView, options) expect(w.findComponent({ name: 'InventoryTurnoverView' }).exists()).toBe(true) expect(w.find('.search-region .inventory-views').exists()).toBe(true) expect(w.find('button[aria-pressed="true"]').text()).toBe('库存周转') await w.findAll('button').find(b => b.text() === '库存明细')!.trigger('click') expect(mocks.push).toHaveBeenCalledWith({ path: '/self-operated/inventory', query: { view: 'DETAIL', warehouseCode: 'US02' } }) mocks.route.query = { view: 'DETAIL', warehouseCode: 'US02' }; await flushPromises() expect(w.find('button[aria-pressed="true"]').text()).toBe('库存明细') w.findComponent({ name: 'AnalyticsView' }).vm.$emit('warehouseSelected', 'US01') await w.findAll('button').find(b => b.text() === '库存周转')!.trigger('click') expect(mocks.push).toHaveBeenLastCalledWith({ path: '/self-operated/inventory', query: { view: 'TURNOVER', warehouseCode: 'US01' } }) w.unmount() }) it('preserves original strict detail links and rejects contradictory parameters', async () => { mocks.route.query = { from: 'overview', context: 'fixed-version' } const w = mount(InventoryView, options) expect(w.findComponent({ name: 'AnalyticsView' }).props('mode')).toBe('inventory') mocks.route.query = { from: 'overview', view: 'TURNOVER' }; await flushPromises() expect(w.find('[role="alert"]').text()).toContain('不能混用') expect(w.findComponent({ name: 'AnalyticsView' }).exists()).toBe(false) expect(w.findComponent({ name: 'InventoryTurnoverView' }).exists()).toBe(false) w.unmount() }) })