element-plus/packages/utils/tests/aria.test.ts
三咲智子 55348b30b6
style: use prettier (#3228)
* style: use prettier

* style: just prettier format, no code changes

* style: eslint fix
object-shorthand, prefer-const

* style: fix no-void

* style: no-console
2021-09-04 19:29:28 +08:00

65 lines
2.2 KiB
TypeScript

import { triggerEvent, isFocusable } from '../aria'
const CE = (tag: string) => document.createElement(tag)
describe('Aria Utils', () => {
describe('Trigger Event', () => {
it('Util trigger event to trigger event correctly', () => {
const div = document.createElement('div')
jest.spyOn(div, 'dispatchEvent')
const eventName = 'click'
triggerEvent(div, eventName)
expect(div.dispatchEvent).toHaveBeenCalled()
})
})
describe('isFocusable', () => {
it("should be focusable when element has tabindex attr, and it's value is greater than 0", () => {
const $el = CE('div')
$el.tabIndex = 1
expect(isFocusable($el)).toBe(true)
})
it("should not be focusable when element has tabindex attr, and it's value is smaller than 0", () => {
const $el = CE('div')
$el.tabIndex = -1
expect(isFocusable($el)).toBe(false)
})
it('should not be focusable when disbaled', () => {
const $el = CE('div')
$el.setAttribute('disabled', 'true')
expect(isFocusable($el)).toBe(false)
})
it('should be focusable when target is anchor and rel is not set to ignore', () => {
const $el = CE('a') as HTMLAnchorElement
$el.href = '#anchor'
$el.rel = 'noreferrer'
expect(isFocusable($el)).toBe(true)
})
it('should not be focusable when target is anchor and rel is set to ignore', () => {
const $el = CE('a') as HTMLAnchorElement
$el.href = '#anchor'
$el.rel = 'ignore'
expect(isFocusable($el)).toBe(false)
})
it('should be focusable when target is input and type is not hidden or is not file', () => {
const $el = CE('input') as HTMLInputElement
$el.type = 'hidden'
expect(isFocusable($el)).toBe(false)
$el.type = 'file'
expect(isFocusable($el)).toBe(false)
$el.type = 'number'
expect(isFocusable($el)).toBe(true)
})
it('should be focusable when the target is button/select/textarea', () => {
let $el = CE('button')
expect(isFocusable($el)).toBe(true)
$el = CE('select')
expect(isFocusable($el)).toBe(true)
$el = CE('textarea')
expect(isFocusable($el)).toBe(true)
})
})
})