import { flushPromises, mount } from '@vue/test-utils' import { defineComponent, h, KeepAlive, ref } from 'vue' import { beforeEach, describe, expect, it, vi } from 'vitest' import AppointmentCalendarView from './AppointmentCalendarView.vue' import { getCalendarSources, getWarehouseArrivals } from './scheduleApi' import { listTripCalendar } from './tripCalendar' vi.mock('vue-router', () => ({ useRoute: () => ({ query: {} }), useRouter: () => ({ push: vi.fn(), replace: vi.fn() }), onBeforeRouteLeave: vi.fn(), onBeforeRouteUpdate: vi.fn() })) vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can: () => true }) })) vi.mock('./scheduleApi', () => ({ getCalendarSources: vi.fn(), getWarehouseArrivals: vi.fn(), saveWarehouseArrival: vi.fn() })) vi.mock('./tripCalendar', async original => ({ ...await original(), listTripCalendar: vi.fn() })) describe('calendar cache lifecycle', () => { beforeEach(() => { vi.clearAllMocks() vi.mocked(getCalendarSources).mockResolvedValue({ containers: [], emptyReturns: [], emptyReturnsReadable: true }) vi.mocked(getWarehouseArrivals).mockResolvedValue([]) vi.mocked(listTripCalendar).mockResolvedValue({ records: [], unresolvedRecords: [] }) }) it('aborts reads when cached and reloads on return without creating background work polling', async () => { const active = ref(true) const host = defineComponent({ setup: () => () => h(KeepAlive, null, { default: () => active.value ? h(AppointmentCalendarView) : h('div') }) }) const wrapper = mount(host, { global: { stubs: { AdminPage: { template: '
' }, AppointmentCalendarPanel: { template: '
' }, AppDialog: true } } }) await flushPromises() expect(getCalendarSources).toHaveBeenCalledTimes(1) const signal = vi.mocked(getCalendarSources).mock.calls[0]![1]! active.value = false; await flushPromises() expect(signal.aborted).toBe(true) active.value = true; await flushPromises() expect(getCalendarSources).toHaveBeenCalledTimes(2) wrapper.unmount() }) })