parent
3ec41ee520
commit
6da9fb14c0
@ -0,0 +1,14 @@
|
||||
import { index } from '@/api/calendars/index'
|
||||
import request from '@/utils/request'
|
||||
|
||||
jest.mock('@/utils/request', () => jest.fn(config => config))
|
||||
|
||||
test('encodes title keywords without introducing extra query parameters', () => {
|
||||
const config = index({ keyword: '科技&A+B#活动', is_publish: 0, page: 2 })
|
||||
expect(request).toHaveBeenCalled()
|
||||
const params = new URLSearchParams(config.paramsSerializer(config.params))
|
||||
expect(params.get('keyword')).toBe('科技&A+B#活动')
|
||||
expect(params.get('is_publish')).toBe('0')
|
||||
expect(params.get('page')).toBe('2')
|
||||
expect(Array.from(params.keys())).toHaveLength(3)
|
||||
})
|
||||
@ -0,0 +1,82 @@
|
||||
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)
|
||||
})
|
||||
Loading…
Reference in new issue