2022-04-12 01:10:25 +08:00
|
|
|
import { describe, expect, it, vi } from 'vitest'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { isFocusable, triggerEvent } from '../..'
|
2020-07-28 20:31:26 +08:00
|
|
|
|
|
|
|
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')
|
2022-04-12 01:10:25 +08:00
|
|
|
vi.spyOn(div, 'dispatchEvent')
|
2020-07-28 20:31:26 +08:00
|
|
|
const eventName = 'click'
|
|
|
|
triggerEvent(div, eventName)
|
|
|
|
expect(div.dispatchEvent).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('isFocusable', () => {
|
2021-09-04 19:29:28 +08:00
|
|
|
it("should be focusable when element has tabindex attr, and it's value is greater than 0", () => {
|
2020-07-28 20:31:26 +08:00
|
|
|
const $el = CE('div')
|
|
|
|
$el.tabIndex = 1
|
|
|
|
expect(isFocusable($el)).toBe(true)
|
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
it("should not be focusable when element has tabindex attr, and it's value is smaller than 0", () => {
|
2020-07-28 20:31:26 +08:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|