ray-template/__test__/components/draggable-card.spec.tsx
XiaoDaiGua-Ray 15b1da763f
fix: address system-wide UI and data review findings
Restore keyboard navigation and fix page refresh, icons, and table interactions.

Guard concurrent edits and align Mock persistence with the API contracts.
2026-11-18 10:25:41 +08:00

83 lines
2.5 KiB
TypeScript

import { flushPromises, mount } from '@vue/test-utils'
import interact from 'interactjs'
import RDraggableCard from '../../packages/ui/src/RDraggableCard/DraggableCard'
const mocks = vi.hoisted(() => ({
instances: [] as Array<{
draggable: ReturnType<typeof vi.fn>
resizable: ReturnType<typeof vi.fn>
unset: ReturnType<typeof vi.fn>
}>,
}))
vi.mock('interactjs', () => ({
default: Object.assign(
vi.fn(() => {
const instance = {
draggable: vi.fn().mockReturnThis(),
resizable: vi.fn().mockReturnThis(),
unset: vi.fn(),
}
mocks.instances.push(instance)
return instance
}),
{ modifiers: { restrictRect: vi.fn((value) => value) } },
),
}))
beforeEach(() => {
mocks.instances.length = 0
vi.mocked(interact).mockClear()
})
it('uses the rendered body as the drag handle when the card has no title', async () => {
const wrapper = mount(RDraggableCard, { slots: { default: 'Body' } })
try {
await flushPromises()
const { allowFrom } = mocks.instances[0].draggable.mock.calls[0][0]
expect(
document.querySelector('.r-draggable-card')?.querySelector(allowFrom)
?.textContent,
).toContain('Body')
} finally {
wrapper.unmount()
}
})
it('releases drag listeners on disable/unmount and removes only the final shared container', async () => {
const first = mount(RDraggableCard, { props: { title: 'First' } })
const second = mount(RDraggableCard, { props: { title: 'Second' } })
await flushPromises()
expect(interact).toHaveBeenCalledTimes(2)
await first.setProps({ dad: false })
expect(mocks.instances[0].unset).toHaveBeenCalledOnce()
await first.setProps({ dad: true })
expect(interact).toHaveBeenCalledTimes(3)
first.unmount()
expect(mocks.instances[2].unset).toHaveBeenCalledOnce()
expect(document.getElementById('r-draggable-card-container')).not.toBeNull()
second.unmount()
expect(mocks.instances[1].unset).toHaveBeenCalledOnce()
expect(document.getElementById('r-draggable-card-container')).toBeNull()
})
it('repositions and rebinds after boundary changes without keeping the previous interaction', async () => {
const wrapper = mount(RDraggableCard, { props: { title: 'Test' } })
try {
await flushPromises()
await wrapper.setProps({ defaultPosition: { x: 24, y: 36 } })
expect(mocks.instances[0].unset).toHaveBeenCalledOnce()
expect(
document.querySelector<HTMLElement>('.r-draggable-card')?.style.transform,
).toBe('translate(24px, 36px)')
} finally {
wrapper.unmount()
}
})