toolbar.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import Quill from '../../../core/quill';
  2. import { addControls } from '../../../modules/toolbar';
  3. describe('Toolbar', function() {
  4. describe('add controls', function() {
  5. it('single level', function() {
  6. addControls(this.container, ['bold', 'italic']);
  7. expect(this.container).toEqualHTML(`
  8. <span class="ql-formats">
  9. <button type="button" class="ql-bold"></button>
  10. <button type="button" class="ql-italic"></button>
  11. </span>
  12. `);
  13. });
  14. it('nested group', function() {
  15. addControls(this.container, [['bold', 'italic'], ['underline', 'strike']]);
  16. expect(this.container).toEqualHTML(`
  17. <span class="ql-formats">
  18. <button type="button" class="ql-bold"></button>
  19. <button type="button" class="ql-italic"></button>
  20. </span>
  21. <span class="ql-formats">
  22. <button type="button" class="ql-underline"></button>
  23. <button type="button" class="ql-strike"></button>
  24. </span>
  25. `);
  26. });
  27. it('button value', function() {
  28. addControls(this.container, ['bold', { header: '2' }]);
  29. expect(this.container).toEqualHTML(`
  30. <span class="ql-formats">
  31. <button type="button" class="ql-bold"></button>
  32. <button type="button" class="ql-header" value="2"></button>
  33. </span>
  34. `);
  35. });
  36. it('select', function() {
  37. addControls(this.container, [{ size: ['10px', false, '18px', '32px'] }]);
  38. expect(this.container).toEqualHTML(`
  39. <span class="ql-formats">
  40. <select class="ql-size">
  41. <option value="10px"></option>
  42. <option selected="selected"></option>
  43. <option value="18px"></option>
  44. <option value="32px"></option>
  45. </select>
  46. </span>
  47. `);
  48. });
  49. it('everything', function() {
  50. addControls(this.container, [
  51. [{ font: [false, 'sans-serif', 'monospace']}, { size: ['10px', false, '18px', '32px'] }],
  52. ['bold', 'italic', 'underline', 'strike'],
  53. [{ list: 'ordered' }, { list: 'bullet' }, { align: [false, 'center', 'right', 'justify'] }],
  54. ['link', 'image']
  55. ]);
  56. expect(this.container).toEqualHTML(`
  57. <span class="ql-formats">
  58. <select class="ql-font">
  59. <option selected="selected"></option>
  60. <option value="sans-serif"></option>
  61. <option value="monospace"></option>
  62. </select>
  63. <select class="ql-size">
  64. <option value="10px"></option>
  65. <option selected="selected"></option>
  66. <option value="18px"></option>
  67. <option value="32px"></option>
  68. </select>
  69. </span>
  70. <span class="ql-formats">
  71. <button type="button" class="ql-bold"></button>
  72. <button type="button" class="ql-italic"></button>
  73. <button type="button" class="ql-underline"></button>
  74. <button type="button" class="ql-strike"></button>
  75. </span>
  76. <span class="ql-formats">
  77. <button type="button" class="ql-list" value="ordered"></button>
  78. <button type="button" class="ql-list" value="bullet"></button>
  79. <select class="ql-align">
  80. <option selected="selected"></option>
  81. <option value="center"></option>
  82. <option value="right"></option>
  83. <option value="justify"></option>
  84. </select>
  85. </span>
  86. <span class="ql-formats">
  87. <button type="button" class="ql-link"></button>
  88. <button type="button" class="ql-image"></button>
  89. </span>
  90. `);
  91. });
  92. });
  93. describe('active', function() {
  94. beforeEach(function() {
  95. let container = this.initialize(HTMLElement, `
  96. <p>0123</p>
  97. <p><strong>5678</strong></p>
  98. <p><a href="http://quilljs.com/">0123</a></p>
  99. <p class="ql-align-center">5678</p>
  100. <p><span class="ql-size-small">01</span><span class="ql-size-large">23</span></p>
  101. `);
  102. this.quill = new Quill(container, {
  103. modules: {
  104. toolbar: [
  105. ['bold', 'link'],
  106. [{ 'size': ['small', false, 'large'] }],
  107. [{ 'align': '' }, { 'align': 'center' }]
  108. ]
  109. },
  110. theme: 'snow'
  111. });
  112. });
  113. it('toggle button', function() {
  114. let boldButton = this.container.parentNode.querySelector('button.ql-bold');
  115. this.quill.setSelection(7);
  116. expect(boldButton.classList.contains('ql-active')).toBe(true);
  117. this.quill.setSelection(2);
  118. expect(boldButton.classList.contains('ql-active')).toBe(false);
  119. });
  120. it('link', function() {
  121. let linkButton = this.container.parentNode.querySelector('button.ql-link');
  122. this.quill.setSelection(12);
  123. expect(linkButton.classList.contains('ql-active')).toBe(true);
  124. this.quill.setSelection(2);
  125. expect(linkButton.classList.contains('ql-active')).toBe(false);
  126. });
  127. it('dropdown', function() {
  128. let sizeSelect = this.container.parentNode.querySelector('select.ql-size');
  129. this.quill.setSelection(21);
  130. expect(sizeSelect.selectedIndex).toEqual(0);
  131. this.quill.setSelection(23);
  132. expect(sizeSelect.selectedIndex).toEqual(2);
  133. this.quill.setSelection(21, 2);
  134. expect(sizeSelect.selectedIndex).toBeLessThan(0);
  135. this.quill.setSelection(2);
  136. expect(sizeSelect.selectedIndex).toEqual(1);
  137. });
  138. it('custom button', function() {
  139. let centerButton = this.container.parentNode.querySelector('button.ql-align[value="center"]');
  140. let leftButton = this.container.parentNode.querySelector('button.ql-align[value]');
  141. this.quill.setSelection(17);
  142. expect(centerButton.classList.contains('ql-active')).toBe(true);
  143. expect(leftButton.classList.contains('ql-active')).toBe(false);
  144. this.quill.setSelection(2);
  145. expect(centerButton.classList.contains('ql-active')).toBe(false);
  146. expect(leftButton.classList.contains('ql-active')).toBe(true);
  147. this.quill.blur();
  148. expect(centerButton.classList.contains('ql-active')).toBe(false);
  149. expect(leftButton.classList.contains('ql-active')).toBe(false);
  150. });
  151. });
  152. });