specialcharacters.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  8. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  9. import SpecialCharacters from '../src/specialcharacters';
  10. import SpecialCharactersMathematical from '../src/specialcharactersmathematical';
  11. import SpecialCharactersArrows from '../src/specialcharactersarrows';
  12. import SpecialCharactersNavigationView from '../src/ui/specialcharactersnavigationview';
  13. import CharacterGridView from '../src/ui/charactergridview';
  14. import specialCharactersIcon from '../theme/icons/specialcharacters.svg';
  15. describe( 'SpecialCharacters', () => {
  16. let plugin;
  17. beforeEach( () => {
  18. plugin = new SpecialCharacters( {} );
  19. } );
  20. it( 'should require proper plugins', () => {
  21. expect( SpecialCharacters.requires ).to.deep.equal( [ Typing ] );
  22. } );
  23. it( 'should be named', () => {
  24. expect( SpecialCharacters.pluginName ).to.equal( 'SpecialCharacters' );
  25. } );
  26. describe( 'init()', () => {
  27. let editor, command, element;
  28. beforeEach( () => {
  29. element = document.createElement( 'div' );
  30. document.body.appendChild( element );
  31. return ClassicTestEditor
  32. .create( element, {
  33. plugins: [
  34. SpecialCharacters,
  35. SpecialCharactersMathematical,
  36. SpecialCharactersArrows
  37. ]
  38. } )
  39. .then( newEditor => {
  40. editor = newEditor;
  41. command = editor.commands.get( 'input' );
  42. } );
  43. } );
  44. afterEach( () => {
  45. element.remove();
  46. return editor.destroy();
  47. } );
  48. describe( '"specialCharacters" dropdown', () => {
  49. let dropdown;
  50. beforeEach( () => {
  51. dropdown = editor.ui.componentFactory.create( 'specialCharacters' );
  52. } );
  53. afterEach( () => {
  54. dropdown.destroy();
  55. } );
  56. it( 'has a navigation view', () => {
  57. expect( dropdown.panelView.children.first ).to.be.instanceOf( SpecialCharactersNavigationView );
  58. } );
  59. it( 'has a grid view', () => {
  60. expect( dropdown.panelView.children.last ).to.be.instanceOf( CharacterGridView );
  61. } );
  62. describe( '#buttonView', () => {
  63. it( 'should get basic properties', () => {
  64. expect( dropdown.buttonView.label ).to.equal( 'Special characters' );
  65. expect( dropdown.buttonView.icon ).to.equal( specialCharactersIcon );
  66. expect( dropdown.buttonView.tooltip ).to.be.true;
  67. } );
  68. it( 'should bind #isEnabled to the command', () => {
  69. expect( dropdown.isEnabled ).to.be.true;
  70. command.isEnabled = false;
  71. expect( dropdown.isEnabled ).to.be.false;
  72. command.isEnabled = true;
  73. } );
  74. } );
  75. it( 'executes a command and focuses the editing view', () => {
  76. const grid = dropdown.panelView.children.last;
  77. const executeSpy = sinon.stub( editor, 'execute' );
  78. const focusSpy = sinon.stub( editor.editing.view, 'focus' );
  79. grid.tiles.get( 2 ).fire( 'execute' );
  80. sinon.assert.calledOnce( executeSpy );
  81. sinon.assert.calledOnce( focusSpy );
  82. sinon.assert.calledWithExactly( executeSpy.firstCall, 'input', {
  83. text: '≤'
  84. } );
  85. } );
  86. describe( 'grid view', () => {
  87. let grid;
  88. beforeEach( () => {
  89. grid = dropdown.panelView.children.last;
  90. } );
  91. it( 'delegates #execute to the dropdown', () => {
  92. const spy = sinon.spy();
  93. dropdown.on( 'execute', spy );
  94. grid.fire( 'execute', { name: 'foo' } );
  95. sinon.assert.calledOnce( spy );
  96. } );
  97. it( 'has default contents', () => {
  98. expect( grid.tiles ).to.have.length.greaterThan( 10 );
  99. } );
  100. it( 'is updated when navigation view fires #execute', () => {
  101. const navigation = dropdown.panelView.children.first;
  102. expect( grid.tiles.get( 0 ).label ).to.equal( '<' );
  103. navigation.groupDropdownView.fire( new EventInfo( { label: 'Arrows' }, 'execute' ) );
  104. expect( grid.tiles.get( 0 ).label ).to.equal( '⇐' );
  105. } );
  106. } );
  107. } );
  108. } );
  109. describe( 'addItems()', () => {
  110. it( 'adds special characters to the available symbols', () => {
  111. plugin.addItems( 'Arrows', [
  112. { title: 'arrow left', character: '←' },
  113. { title: 'arrow right', character: '→' }
  114. ] );
  115. expect( plugin._groups.size ).to.equal( 1 );
  116. expect( plugin._groups.has( 'Arrows' ) ).to.equal( true );
  117. expect( plugin._characters.size ).to.equal( 2 );
  118. expect( plugin._characters.has( 'arrow left' ) ).to.equal( true );
  119. expect( plugin._characters.has( 'arrow right' ) ).to.equal( true );
  120. } );
  121. } );
  122. describe( 'getGroups()', () => {
  123. it( 'returns iterator of defined groups', () => {
  124. plugin.addItems( 'Arrows', [
  125. { title: 'arrow left', character: '←' }
  126. ] );
  127. plugin.addItems( 'Mathematical', [
  128. { title: 'precedes', character: '≺' },
  129. { title: 'succeeds', character: '≻' }
  130. ] );
  131. const groups = [ ...plugin.getGroups() ];
  132. expect( groups ).to.deep.equal( [ 'Arrows', 'Mathematical' ] );
  133. } );
  134. } );
  135. describe( 'addItems()', () => {
  136. it( 'works with subsequent calls to the same group', () => {
  137. plugin.addItems( 'Mathematical', [ {
  138. title: 'dot',
  139. character: '.'
  140. } ] );
  141. plugin.addItems( 'Mathematical', [ {
  142. title: ',',
  143. character: 'comma'
  144. } ] );
  145. const groups = [ ...plugin.getGroups() ];
  146. expect( groups ).to.deep.equal( [ 'Mathematical' ] );
  147. } );
  148. } );
  149. describe( 'getCharactersForGroup()', () => {
  150. it( 'returns a collection of defined special characters names', () => {
  151. plugin.addItems( 'Mathematical', [
  152. { title: 'precedes', character: '≺' },
  153. { title: 'succeeds', character: '≻' }
  154. ] );
  155. const characters = plugin.getCharactersForGroup( 'Mathematical' );
  156. expect( characters.size ).to.equal( 2 );
  157. expect( characters.has( 'precedes' ) ).to.equal( true );
  158. expect( characters.has( 'succeeds' ) ).to.equal( true );
  159. } );
  160. it( 'returns undefined for non-existing group', () => {
  161. plugin.addItems( 'Mathematical', [
  162. { title: 'precedes', character: '≺' },
  163. { title: 'succeeds', character: '≻' }
  164. ] );
  165. const characters = plugin.getCharactersForGroup( 'Foo' );
  166. expect( characters ).to.be.undefined;
  167. } );
  168. } );
  169. describe( 'getCharacter()', () => {
  170. it( 'returns a collection of defined special characters names', () => {
  171. plugin.addItems( 'Mathematical', [
  172. { title: 'precedes', character: '≺' },
  173. { title: 'succeeds', character: '≻' }
  174. ] );
  175. expect( plugin.getCharacter( 'succeeds' ) ).to.equal( '≻' );
  176. } );
  177. it( 'returns undefined for non-existing character', () => {
  178. expect( plugin.getCharacter( 'succeeds' ) ).to.be.undefined;
  179. } );
  180. } );
  181. } );