aria.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { isFocusable, triggerEvent } from '../..'
  3. const CE = (tag: string) => document.createElement(tag)
  4. describe('Aria Utils', () => {
  5. describe('Trigger Event', () => {
  6. it('Util trigger event to trigger event correctly', () => {
  7. const div = document.createElement('div')
  8. vi.spyOn(div, 'dispatchEvent')
  9. const eventName = 'click'
  10. triggerEvent(div, eventName)
  11. expect(div.dispatchEvent).toHaveBeenCalled()
  12. })
  13. })
  14. describe('isFocusable', () => {
  15. it("should be focusable when element has tabindex attr, and it's value is greater than 0", () => {
  16. const $el = CE('div')
  17. $el.tabIndex = 1
  18. expect(isFocusable($el)).toBe(true)
  19. })
  20. it("should not be focusable when element has tabindex attr, and it's value is smaller than 0", () => {
  21. const $el = CE('div')
  22. $el.tabIndex = -1
  23. expect(isFocusable($el)).toBe(false)
  24. })
  25. it('should not be focusable when disbaled', () => {
  26. const $el = CE('div')
  27. $el.setAttribute('disabled', 'true')
  28. expect(isFocusable($el)).toBe(false)
  29. })
  30. it('should be focusable when target is anchor and rel is not set to ignore', () => {
  31. const $el = CE('a') as HTMLAnchorElement
  32. $el.href = '#anchor'
  33. $el.rel = 'noreferrer'
  34. expect(isFocusable($el)).toBe(true)
  35. })
  36. it('should not be focusable when target is anchor and rel is set to ignore', () => {
  37. const $el = CE('a') as HTMLAnchorElement
  38. $el.href = '#anchor'
  39. $el.rel = 'ignore'
  40. expect(isFocusable($el)).toBe(false)
  41. })
  42. it('should be focusable when target is input and type is not hidden or is not file', () => {
  43. const $el = CE('input') as HTMLInputElement
  44. $el.type = 'hidden'
  45. expect(isFocusable($el)).toBe(false)
  46. $el.type = 'file'
  47. expect(isFocusable($el)).toBe(false)
  48. $el.type = 'number'
  49. expect(isFocusable($el)).toBe(true)
  50. })
  51. it('should be focusable when the target is button/select/textarea', () => {
  52. let $el = CE('button')
  53. expect(isFocusable($el)).toBe(true)
  54. $el = CE('select')
  55. expect(isFocusable($el)).toBe(true)
  56. $el = CE('textarea')
  57. expect(isFocusable($el)).toBe(true)
  58. })
  59. })
  60. })