You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
3.4 KiB
83 lines
3.4 KiB
|
2 days ago
|
import Calendar from '@/views/calendar/index.vue'
|
||
|
|
import { index, destroy } from '@/api/calendars/index'
|
||
|
|
|
||
|
|
jest.mock('@/views/calendar/components/addCalendar.vue', () => ({ name: 'AddCalendar' }))
|
||
|
|
jest.mock('@/api/calendars/index', () => ({ index: jest.fn(), destroy: jest.fn() }))
|
||
|
|
jest.mock('@/utils/auth', () => ({ getToken: jest.fn() }))
|
||
|
|
|
||
|
|
function state() {
|
||
|
|
return { ...Calendar.data(), ...Calendar.methods }
|
||
|
|
}
|
||
|
|
|
||
|
|
beforeEach(() => jest.clearAllMocks())
|
||
|
|
|
||
|
|
test('queries all months with filters, including unpublished events, and resets paging', async() => {
|
||
|
|
const vm = state()
|
||
|
|
vm.allEventsPage = 4
|
||
|
|
vm.allEventsFilters = { keyword: ' 科技 ', type: 1, is_publish: 0, dateRange: ['2026-10-01', '2026-10-31'] }
|
||
|
|
index.mockResolvedValue({ list: [{ id: 244 }], total: 1 })
|
||
|
|
await vm.searchAllEvents()
|
||
|
|
expect(index).toHaveBeenCalledWith({ paginate: 1, page: 1, page_size: 20, keyword: '科技', type: 1, is_publish: 0, start_date: '2026-10-01', end_date: '2026-10-31' })
|
||
|
|
expect(vm.allEvents).toEqual([{ id: 244 }])
|
||
|
|
await vm.resetAllEventsFilters()
|
||
|
|
expect(index).toHaveBeenLastCalledWith({ paginate: 1, page: 1, page_size: 20 })
|
||
|
|
})
|
||
|
|
|
||
|
|
test('returns to previous page when deleting the last item on the last page', async() => {
|
||
|
|
const vm = state()
|
||
|
|
vm.allEventsPage = 2
|
||
|
|
index.mockResolvedValueOnce({ list: [], total: 20 }).mockResolvedValueOnce({ list: [{ id: 1 }], total: 20 })
|
||
|
|
await vm.loadAllEvents()
|
||
|
|
expect(vm.allEventsPage).toBe(1)
|
||
|
|
expect(vm.allEvents).toEqual([{ id: 1 }])
|
||
|
|
expect(vm.allEventsLoading).toBe(false)
|
||
|
|
})
|
||
|
|
|
||
|
|
test('keeps the latest query when earlier responses arrive late', async() => {
|
||
|
|
const vm = state()
|
||
|
|
let finishFirst
|
||
|
|
index.mockReturnValueOnce(new Promise(resolve => { finishFirst = resolve })).mockResolvedValueOnce({ list: [{ id: 2 }], total: 1 })
|
||
|
|
const first = vm.loadAllEvents()
|
||
|
|
await vm.loadAllEvents()
|
||
|
|
finishFirst({ list: [{ id: 1 }], total: 1 })
|
||
|
|
await first
|
||
|
|
expect(vm.allEvents).toEqual([{ id: 2 }])
|
||
|
|
})
|
||
|
|
|
||
|
|
test('requires deletion confirmation and refreshes both lists after deletion', async() => {
|
||
|
|
const vm = state()
|
||
|
|
vm.$confirm = jest.fn().mockRejectedValue('cancel')
|
||
|
|
vm.$message = { success: jest.fn() }
|
||
|
|
vm.getList = jest.fn().mockResolvedValue()
|
||
|
|
vm.loadAllEvents = jest.fn().mockResolvedValue()
|
||
|
|
await vm.deleteEvent({ id: 244, title: '科技大讲堂' })
|
||
|
|
expect(destroy).not.toHaveBeenCalled()
|
||
|
|
vm.$confirm.mockResolvedValue()
|
||
|
|
destroy.mockResolvedValue({})
|
||
|
|
await vm.deleteEvent({ id: 244, title: '科技大讲堂' })
|
||
|
|
expect(destroy).toHaveBeenCalledWith({ id: 244 })
|
||
|
|
expect(vm.getList).toHaveBeenCalledTimes(1)
|
||
|
|
expect(vm.loadAllEvents).toHaveBeenCalledTimes(1)
|
||
|
|
expect(vm.deletingEventId).toBeNull()
|
||
|
|
})
|
||
|
|
|
||
|
|
test('editing from either list opens the existing editor with its ID', () => {
|
||
|
|
const vm = state()
|
||
|
|
vm.openCreateModal = jest.fn()
|
||
|
|
vm.allEventsDialogVisible = true
|
||
|
|
vm.editEvent({ id: 244 })
|
||
|
|
expect(vm.openCreateModal).toHaveBeenCalledWith('editor', 244)
|
||
|
|
expect(vm.allEventsDialogVisible).toBe(false)
|
||
|
|
})
|
||
|
|
|
||
|
|
test('renders a Sunday morning event and splits events across weeks', () => {
|
||
|
|
const vm = state()
|
||
|
|
vm.calendarDate = new Date(2026, 9, 1)
|
||
|
|
vm.list = [{ id: 244, start_time: '2026-10-11 09:00:00', end_time: '2026-10-11 12:00:00' }]
|
||
|
|
const segments = vm.getCalendarSegments()
|
||
|
|
expect(segments).toHaveLength(1)
|
||
|
|
expect(segments[0].startCol).toBe(6)
|
||
|
|
vm.list[0].end_time = '2026-10-12 12:00:00'
|
||
|
|
expect(vm.getCalendarSegments()).toHaveLength(2)
|
||
|
|
})
|