239 lines
6.2 KiB
TypeScript
239 lines
6.2 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|||
|
|
import { createApp } from 'vue'
|
||
|
|
import { useGlobalConfirmEnter } from './useConfirmEnter'
|
||
|
|
|
||
|
|
const visibleRects = [{
|
||
|
|
bottom: 100,
|
||
|
|
height: 100,
|
||
|
|
left: 0,
|
||
|
|
right: 100,
|
||
|
|
top: 0,
|
||
|
|
width: 100,
|
||
|
|
x: 0,
|
||
|
|
y: 0,
|
||
|
|
toJSON: () => ({})
|
||
|
|
}] as DOMRect[]
|
||
|
|
|
||
|
|
const makeVisible = (element: HTMLElement) => {
|
||
|
|
vi.spyOn(element, 'getClientRects').mockReturnValue(visibleRects as unknown as DOMRectList)
|
||
|
|
}
|
||
|
|
|
||
|
|
const setDesktopViewport = () => {
|
||
|
|
Object.defineProperty(window, 'innerWidth', {
|
||
|
|
configurable: true,
|
||
|
|
value: 1280
|
||
|
|
})
|
||
|
|
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
|
||
|
|
matches: query === '(pointer: coarse)' ? false : false,
|
||
|
|
media: query,
|
||
|
|
onchange: null,
|
||
|
|
addEventListener: vi.fn(),
|
||
|
|
removeEventListener: vi.fn(),
|
||
|
|
addListener: vi.fn(),
|
||
|
|
removeListener: vi.fn(),
|
||
|
|
dispatchEvent: vi.fn()
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
const setMobileViewport = () => {
|
||
|
|
Object.defineProperty(window, 'innerWidth', {
|
||
|
|
configurable: true,
|
||
|
|
value: 640
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
const setCoarsePointer = () => {
|
||
|
|
Object.defineProperty(window, 'innerWidth', {
|
||
|
|
configurable: true,
|
||
|
|
value: 1280
|
||
|
|
})
|
||
|
|
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
|
||
|
|
matches: query === '(pointer: coarse)',
|
||
|
|
media: query,
|
||
|
|
onchange: null,
|
||
|
|
addEventListener: vi.fn(),
|
||
|
|
removeEventListener: vi.fn(),
|
||
|
|
addListener: vi.fn(),
|
||
|
|
removeListener: vi.fn(),
|
||
|
|
dispatchEvent: vi.fn()
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
const mountGlobalConfirmEnter = () => {
|
||
|
|
const host = document.createElement('div')
|
||
|
|
document.body.appendChild(host)
|
||
|
|
|
||
|
|
const app = createApp({
|
||
|
|
setup() {
|
||
|
|
useGlobalConfirmEnter()
|
||
|
|
return () => null
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
app.mount(host)
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
app.unmount()
|
||
|
|
host.remove()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const createModal = ({
|
||
|
|
zIndex = 1000,
|
||
|
|
buttonClass = '',
|
||
|
|
disabled = false,
|
||
|
|
ariaDisabled = false,
|
||
|
|
footerClass = 'ant-modal-footer'
|
||
|
|
}: {
|
||
|
|
zIndex?: number
|
||
|
|
buttonClass?: string
|
||
|
|
disabled?: boolean
|
||
|
|
ariaDisabled?: boolean
|
||
|
|
footerClass?: 'ant-modal-footer' | 'ant-modal-confirm-btns'
|
||
|
|
} = {}) => {
|
||
|
|
const wrap = document.createElement('div')
|
||
|
|
wrap.className = 'ant-modal-wrap'
|
||
|
|
wrap.style.zIndex = String(zIndex)
|
||
|
|
|
||
|
|
const footer = document.createElement('div')
|
||
|
|
footer.className = footerClass
|
||
|
|
|
||
|
|
const button = document.createElement('button')
|
||
|
|
button.type = 'button'
|
||
|
|
button.className = `ant-btn ant-btn-primary ${buttonClass}`.trim()
|
||
|
|
button.disabled = disabled
|
||
|
|
if (ariaDisabled) {
|
||
|
|
button.setAttribute('aria-disabled', 'true')
|
||
|
|
}
|
||
|
|
|
||
|
|
const onClick = vi.fn()
|
||
|
|
const click = vi.spyOn(button, 'click')
|
||
|
|
button.addEventListener('click', onClick)
|
||
|
|
|
||
|
|
footer.appendChild(button)
|
||
|
|
wrap.appendChild(footer)
|
||
|
|
document.body.appendChild(wrap)
|
||
|
|
makeVisible(wrap)
|
||
|
|
makeVisible(button)
|
||
|
|
|
||
|
|
return { wrap, button, click, onClick }
|
||
|
|
}
|
||
|
|
|
||
|
|
const createKeyboardEvent = () => new KeyboardEvent('keydown', {
|
||
|
|
key: 'Enter',
|
||
|
|
bubbles: true,
|
||
|
|
cancelable: true
|
||
|
|
})
|
||
|
|
|
||
|
|
const pressEnter = (target: EventTarget = document) => {
|
||
|
|
const event = createKeyboardEvent()
|
||
|
|
target.dispatchEvent(event)
|
||
|
|
return event
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('useGlobalConfirmEnter', () => {
|
||
|
|
let unmount: (() => void) | undefined
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
document.body.innerHTML = ''
|
||
|
|
setDesktopViewport()
|
||
|
|
unmount = mountGlobalConfirmEnter()
|
||
|
|
})
|
||
|
|
|
||
|
|
afterEach(() => {
|
||
|
|
unmount?.()
|
||
|
|
document.body.innerHTML = ''
|
||
|
|
vi.restoreAllMocks()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('clicks the visible top modal primary ok button when PC users press Enter', () => {
|
||
|
|
const { onClick } = createModal()
|
||
|
|
|
||
|
|
const event = pressEnter()
|
||
|
|
|
||
|
|
expect(onClick).toHaveBeenCalledTimes(1)
|
||
|
|
expect(event.defaultPrevented).toBe(true)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('does not click the ok button when Enter starts inside textarea or contenteditable elements', () => {
|
||
|
|
const { onClick } = createModal()
|
||
|
|
|
||
|
|
const textarea = document.createElement('textarea')
|
||
|
|
document.body.appendChild(textarea)
|
||
|
|
pressEnter(textarea)
|
||
|
|
|
||
|
|
const editable = document.createElement('div')
|
||
|
|
editable.setAttribute('contenteditable', 'true')
|
||
|
|
document.body.appendChild(editable)
|
||
|
|
pressEnter(editable)
|
||
|
|
|
||
|
|
expect(onClick).not.toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
|
||
|
|
it.each([
|
||
|
|
'ant-select-dropdown',
|
||
|
|
'ant-picker-dropdown',
|
||
|
|
'ant-dropdown',
|
||
|
|
'ant-popover'
|
||
|
|
])('does not click the ok button while %s is open', (dropdownClass) => {
|
||
|
|
const { onClick } = createModal()
|
||
|
|
const popup = document.createElement('div')
|
||
|
|
popup.className = dropdownClass
|
||
|
|
document.body.appendChild(popup)
|
||
|
|
|
||
|
|
pressEnter()
|
||
|
|
|
||
|
|
expect(onClick).not.toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('does not click disabled or loading ok buttons', () => {
|
||
|
|
const disabledModal = createModal({ disabled: true })
|
||
|
|
pressEnter()
|
||
|
|
expect(disabledModal.click).not.toHaveBeenCalled()
|
||
|
|
expect(disabledModal.onClick).not.toHaveBeenCalled()
|
||
|
|
disabledModal.wrap.remove()
|
||
|
|
|
||
|
|
const ariaDisabledModal = createModal({ ariaDisabled: true })
|
||
|
|
pressEnter()
|
||
|
|
expect(ariaDisabledModal.click).not.toHaveBeenCalled()
|
||
|
|
expect(ariaDisabledModal.onClick).not.toHaveBeenCalled()
|
||
|
|
ariaDisabledModal.wrap.remove()
|
||
|
|
|
||
|
|
const loadingModal = createModal({ buttonClass: 'ant-btn-loading' })
|
||
|
|
pressEnter()
|
||
|
|
expect(loadingModal.click).not.toHaveBeenCalled()
|
||
|
|
expect(loadingModal.onClick).not.toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('does not click the ok button on mobile width or coarse pointer devices', () => {
|
||
|
|
const mobileModal = createModal()
|
||
|
|
setMobileViewport()
|
||
|
|
pressEnter()
|
||
|
|
expect(mobileModal.onClick).not.toHaveBeenCalled()
|
||
|
|
mobileModal.wrap.remove()
|
||
|
|
|
||
|
|
const coarsePointerModal = createModal()
|
||
|
|
setCoarsePointer()
|
||
|
|
pressEnter()
|
||
|
|
expect(coarsePointerModal.onClick).not.toHaveBeenCalled()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('clicks only the z-index and DOM-top modal when multiple modals are visible', () => {
|
||
|
|
const firstModal = createModal({ zIndex: 1000 })
|
||
|
|
const secondModal = createModal({ zIndex: 1000 })
|
||
|
|
const topZIndexModal = createModal({ zIndex: 1200 })
|
||
|
|
|
||
|
|
pressEnter()
|
||
|
|
|
||
|
|
expect(firstModal.onClick).not.toHaveBeenCalled()
|
||
|
|
expect(secondModal.onClick).not.toHaveBeenCalled()
|
||
|
|
expect(topZIndexModal.onClick).toHaveBeenCalledTimes(1)
|
||
|
|
|
||
|
|
topZIndexModal.wrap.remove()
|
||
|
|
pressEnter()
|
||
|
|
|
||
|
|
expect(firstModal.onClick).not.toHaveBeenCalled()
|
||
|
|
expect(secondModal.onClick).toHaveBeenCalledTimes(1)
|
||
|
|
})
|
||
|
|
})
|