picker.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import Picker from '../../../ui/picker';
  2. describe('Picker', function() {
  3. beforeEach(function() {
  4. this.container.innerHTML =
  5. '<select><option selected>0</option><option value="1">1</option></select>';
  6. this.pickerSelectorInstance = new Picker(this.container.firstChild);
  7. this.pickerSelector = this.container.querySelector('.ql-picker');
  8. });
  9. it('initialization', function() {
  10. expect(this.container.querySelector('.ql-picker')).toBeTruthy();
  11. expect(this.container.querySelector('.ql-active')).toBeFalsy();
  12. expect(
  13. this.container.querySelector('.ql-picker-item.ql-selected').outerHTML,
  14. ).toEqualHTML(
  15. '<span tabindex="0" role="button" class="ql-picker-item ql-selected" data-label="0"></span>',
  16. );
  17. expect(
  18. this.container.querySelector('.ql-picker-item:not(.ql-selected)')
  19. .outerHTML,
  20. ).toEqualHTML(
  21. '<span tabindex="0" role="button" class="ql-picker-item" data-value="1" data-label="1"></span>',
  22. );
  23. });
  24. it('escape charcters', function() {
  25. const select = document.createElement('select');
  26. const option = document.createElement('option');
  27. this.container.appendChild(select);
  28. select.appendChild(option);
  29. let value = '"Helvetica Neue", \'Helvetica\', sans-serif';
  30. option.value = value;
  31. value = value.replace(/"/g, '\\"');
  32. expect(select.querySelector(`option[value="${value}"]`)).toEqual(option);
  33. });
  34. it('label is initialized with the correct aria attributes', function() {
  35. expect(
  36. this.pickerSelector
  37. .querySelector('.ql-picker-label')
  38. .getAttribute('aria-expanded'),
  39. ).toEqual('false');
  40. const optionsId = this.pickerSelector.querySelector('.ql-picker-options')
  41. .id;
  42. expect(
  43. this.pickerSelector
  44. .querySelector('.ql-picker-label')
  45. .getAttribute('aria-controls'),
  46. ).toEqual(optionsId);
  47. });
  48. it('options container is initialized with the correct aria attributes', function() {
  49. expect(
  50. this.pickerSelector
  51. .querySelector('.ql-picker-options')
  52. .getAttribute('aria-hidden'),
  53. ).toEqual('true');
  54. const ariaControlsLabel = this.pickerSelector
  55. .querySelector('.ql-picker-label')
  56. .getAttribute('aria-controls');
  57. expect(this.pickerSelector.querySelector('.ql-picker-options').id).toEqual(
  58. ariaControlsLabel,
  59. );
  60. expect(
  61. this.pickerSelector.querySelector('.ql-picker-options').tabIndex,
  62. ).toEqual(-1);
  63. });
  64. it('aria attributes toggle correctly when the picker is opened via enter key', function() {
  65. const pickerLabel = this.pickerSelector.querySelector('.ql-picker-label');
  66. pickerLabel.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
  67. expect(pickerLabel.getAttribute('aria-expanded')).toEqual('true');
  68. expect(
  69. this.pickerSelector
  70. .querySelector('.ql-picker-options')
  71. .getAttribute('aria-hidden'),
  72. ).toEqual('false');
  73. });
  74. it('aria attributes toggle correctly when the picker is opened via mousedown', function() {
  75. const pickerLabel = this.pickerSelector.querySelector('.ql-picker-label');
  76. pickerLabel.dispatchEvent(
  77. new Event('mousedown', {
  78. bubbles: true,
  79. cancelable: true,
  80. }),
  81. );
  82. expect(pickerLabel.getAttribute('aria-expanded')).toEqual('true');
  83. expect(
  84. this.pickerSelector
  85. .querySelector('.ql-picker-options')
  86. .getAttribute('aria-hidden'),
  87. ).toEqual('false');
  88. });
  89. it('aria attributes toggle correctly when an item is selected via click', function() {
  90. const pickerLabel = this.pickerSelector.querySelector('.ql-picker-label');
  91. pickerLabel.click();
  92. const pickerItem = this.pickerSelector.querySelector('.ql-picker-item');
  93. pickerItem.click();
  94. expect(pickerLabel.getAttribute('aria-expanded')).toEqual('false');
  95. expect(
  96. this.pickerSelector
  97. .querySelector('.ql-picker-options')
  98. .getAttribute('aria-hidden'),
  99. ).toEqual('true');
  100. expect(pickerLabel.textContent.trim()).toEqual(
  101. pickerItem.textContent.trim(),
  102. );
  103. });
  104. it('aria attributes toggle correctly when an item is selected via enter', function() {
  105. const pickerLabel = this.pickerSelector.querySelector('.ql-picker-label');
  106. pickerLabel.click();
  107. const pickerItem = this.pickerSelector.querySelector('.ql-picker-item');
  108. pickerItem.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
  109. expect(pickerLabel.getAttribute('aria-expanded')).toEqual('false');
  110. expect(
  111. this.pickerSelector
  112. .querySelector('.ql-picker-options')
  113. .getAttribute('aria-hidden'),
  114. ).toEqual('true');
  115. expect(pickerLabel.textContent.trim()).toEqual(
  116. pickerItem.textContent.trim(),
  117. );
  118. });
  119. it('aria attributes toggle correctly when the picker is closed via clicking on the label again', function() {
  120. const pickerLabel = this.pickerSelector.querySelector('.ql-picker-label');
  121. pickerLabel.click();
  122. pickerLabel.click();
  123. expect(pickerLabel.getAttribute('aria-expanded')).toEqual('false');
  124. expect(
  125. this.pickerSelector
  126. .querySelector('.ql-picker-options')
  127. .getAttribute('aria-hidden'),
  128. ).toEqual('true');
  129. });
  130. it('aria attributes toggle correctly when the picker is closed via escaping out of it', function() {
  131. const pickerLabel = this.pickerSelector.querySelector('.ql-picker-label');
  132. pickerLabel.click();
  133. pickerLabel.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
  134. expect(pickerLabel.getAttribute('aria-expanded')).toEqual('false');
  135. expect(
  136. this.pickerSelector
  137. .querySelector('.ql-picker-options')
  138. .getAttribute('aria-hidden'),
  139. ).toEqual('true');
  140. });
  141. afterEach(function() {
  142. this.pickerSelectorInstance = null;
  143. });
  144. });