$id.spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { haveAttribute, haveText, html, test } from '../../utils'
  2. test('$id generates a unique id',
  3. html`
  4. <div x-data x-id="['foo']" id="1">
  5. <div>
  6. <h1 :id="$id('foo')"></h1>
  7. </div>
  8. <span :aria-labelledby="$id('foo')"></span>
  9. </div>
  10. <div x-data x-id="['foo']" id="2">
  11. <div>
  12. <h1 :id="$id('foo')"></h1>
  13. </div>
  14. <span :aria-labelledby="$id('foo')"></span>
  15. </div>
  16. `,
  17. ({ get }) => {
  18. get('#1 h1').should(haveAttribute('id', 'foo-1'))
  19. get('#1 span').should(haveAttribute('aria-labelledby', 'foo-1'))
  20. get('#2 h1').should(haveAttribute('id', 'foo-2'))
  21. get('#2 span').should(haveAttribute('aria-labelledby', 'foo-2'))
  22. }
  23. )
  24. test('$id works with keys and nested data scopes',
  25. html`
  26. <div x-data x-id="['foo']" id="1">
  27. <!-- foo-1-3 -->
  28. <span :aria-activedescendant="$id('foo', 3)"></span>
  29. <ul>
  30. <li x-data :id="$id('foo', 1)"></li> <!-- foo-1-1 -->
  31. <li x-data :id="$id('foo', 2)"></li> <!-- foo-1-2 -->
  32. <li x-data :id="$id('foo', 3)"></li> <!-- foo-1-3 -->
  33. </ul>
  34. </div>
  35. <div x-data x-id="['foo']" id="2">
  36. <!-- foo-2-3 -->
  37. <span :aria-activedescendant="$id('foo', 3)"></span>
  38. <ul>
  39. <li x-data :id="$id('foo', 1)"></li> <!-- foo-2-1 -->
  40. <li x-data :id="$id('foo', 2)"></li> <!-- foo-2-2 -->
  41. <li x-data :id="$id('foo', 3)"></li> <!-- foo-2-3 -->
  42. </ul>
  43. </div>
  44. `,
  45. ({ get }) => {
  46. get('#1 span').should(haveAttribute('aria-activedescendant', 'foo-1-3'))
  47. get('#1 li:nth-child(1)').should(haveAttribute('id', 'foo-1-1'))
  48. get('#1 li:nth-child(2)').should(haveAttribute('id', 'foo-1-2'))
  49. get('#1 li:nth-child(3)').should(haveAttribute('id', 'foo-1-3'))
  50. get('#2 span').should(haveAttribute('aria-activedescendant', 'foo-2-3'))
  51. get('#2 li:nth-child(1)').should(haveAttribute('id', 'foo-2-1'))
  52. get('#2 li:nth-child(2)').should(haveAttribute('id', 'foo-2-2'))
  53. get('#2 li:nth-child(3)').should(haveAttribute('id', 'foo-2-3'))
  54. }
  55. )
  56. test('$id scopes are grouped by name',
  57. html`
  58. <div x-data x-id="['foo']">
  59. <!-- foo-1 -->
  60. <span :aria-activedescendant="$id('foo')"></span>
  61. <ul>
  62. <li x-data x-id="['bar']" :id="$id('bar')"></li> <!-- bar-1 -->
  63. <li x-data x-id="['bar']" :id="$id('bar')"></li> <!-- bar-2 -->
  64. </ul>
  65. </div>
  66. `,
  67. ({ get }) => {
  68. get('span').should(haveAttribute('aria-activedescendant', 'foo-1'))
  69. get('li:nth-child(1)').should(haveAttribute('id', 'bar-1'))
  70. get('li:nth-child(2)').should(haveAttribute('id', 'bar-2'))
  71. }
  72. )
  73. test('$ids are globally unique when outside x-id',
  74. html`
  75. <div x-data>
  76. <h1 :id="$id('foo')"></h1>
  77. <h2 :id="$id('foo')"></h2>
  78. </div>
  79. `,
  80. ({ get }) => {
  81. get('h1').should(haveAttribute('id', 'foo-1'))
  82. get('h2').should(haveAttribute('id', 'foo-2'))
  83. }
  84. )
  85. test('$id scopes can be reset',
  86. html`
  87. <div x-data x-id="['foo', 'bar']">
  88. <!-- foo-1 -->
  89. <span :aria-labelledby="$id('foo')"></span>
  90. <div x-data>
  91. <h1 :id="$id('foo')"></h1>
  92. <h5 :id="$id('bar')"></h5>
  93. <div x-id="['foo']">
  94. <h2 :aria-labelledby="$id('foo')"></h2>
  95. <h6 :aria-labelledby="$id('bar')"></h6>
  96. <div x-data>
  97. <h3 :id="$id('foo')"></h3>
  98. </div>
  99. </div>
  100. <h4 :id="$id('foo')"></h4>
  101. </div>
  102. </div>
  103. `,
  104. ({ get }) => {
  105. get('span').should(haveAttribute('aria-labelledby', 'foo-1'))
  106. get('h1').should(haveAttribute('id', 'foo-1'))
  107. get('h2').should(haveAttribute('aria-labelledby', 'foo-2'))
  108. get('h3').should(haveAttribute('id', 'foo-2'))
  109. get('h4').should(haveAttribute('id', 'foo-1'))
  110. get('h5').should(haveAttribute('id', 'bar-1'))
  111. get('h6').should(haveAttribute('aria-labelledby', 'bar-1'))
  112. }
  113. )