specialcharacters.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. SpecialCharactersMathematical,
  35. SpecialCharactersArrows
  36. ]
  37. } )
  38. .then( newEditor => {
  39. editor = newEditor;
  40. command = editor.commands.get( 'input' );
  41. } );
  42. } );
  43. afterEach( () => {
  44. element.remove();
  45. return editor.destroy();
  46. } );
  47. describe( '"specialCharacters" dropdown', () => {
  48. let dropdown;
  49. beforeEach( () => {
  50. dropdown = editor.ui.componentFactory.create( 'specialCharacters' );
  51. } );
  52. afterEach( () => {
  53. dropdown.destroy();
  54. } );
  55. it( 'has a navigation view', () => {
  56. expect( dropdown.panelView.children.first ).to.be.instanceOf( SpecialCharactersNavigationView );
  57. } );
  58. it( 'has a grid view', () => {
  59. expect( dropdown.panelView.children.last ).to.be.instanceOf( CharacterGridView );
  60. } );
  61. describe( '#buttonView', () => {
  62. it( 'should get basic properties', () => {
  63. expect( dropdown.buttonView.label ).to.equal( 'Special characters' );
  64. expect( dropdown.buttonView.icon ).to.equal( specialCharactersIcon );
  65. expect( dropdown.buttonView.tooltip ).to.be.true;
  66. } );
  67. it( 'should bind #isEnabled to the command', () => {
  68. expect( dropdown.isEnabled ).to.be.true;
  69. command.isEnabled = false;
  70. expect( dropdown.isEnabled ).to.be.false;
  71. command.isEnabled = true;
  72. } );
  73. } );
  74. it( 'executes a command and focuses the editing view', () => {
  75. const grid = dropdown.panelView.children.last;
  76. const executeSpy = sinon.stub( editor, 'execute' );
  77. const focusSpy = sinon.stub( editor.editing.view, 'focus' );
  78. grid.tiles.get( 2 ).fire( 'execute' );
  79. sinon.assert.calledOnce( executeSpy );
  80. sinon.assert.calledOnce( focusSpy );
  81. sinon.assert.calledWithExactly( executeSpy.firstCall, 'input', {
  82. text: '≤'
  83. } );
  84. } );
  85. describe( 'grid view', () => {
  86. let grid;
  87. beforeEach( () => {
  88. grid = dropdown.panelView.children.last;
  89. } );
  90. it( 'delegates #execute to the dropdown', () => {
  91. const spy = sinon.spy();
  92. dropdown.on( 'execute', spy );
  93. grid.fire( 'execute', { name: 'foo' } );
  94. sinon.assert.calledOnce( spy );
  95. } );
  96. it( 'has default contents', () => {
  97. expect( grid.tiles ).to.have.length.greaterThan( 10 );
  98. } );
  99. it( 'is updated when navigation view fires #execute', () => {
  100. const navigation = dropdown.panelView.children.first;
  101. expect( grid.tiles.get( 0 ).label ).to.equal( '<' );
  102. navigation.groupDropdownView.fire( new EventInfo( { label: 'Arrows' }, 'execute' ) );
  103. expect( grid.tiles.get( 0 ).label ).to.equal( '⇐' );
  104. } );
  105. } );
  106. } );
  107. } );
  108. describe( 'addItems()', () => {
  109. it( 'adds special characters to the available symbols', () => {
  110. plugin.addItems( 'Arrows', [
  111. { title: 'arrow left', character: '←' },
  112. { title: 'arrow right', character: '→' }
  113. ] );
  114. expect( plugin._groups.size ).to.equal( 1 );
  115. expect( plugin._groups.has( 'Arrows' ) ).to.equal( true );
  116. expect( plugin._characters.size ).to.equal( 2 );
  117. expect( plugin._characters.has( 'arrow left' ) ).to.equal( true );
  118. expect( plugin._characters.has( 'arrow right' ) ).to.equal( true );
  119. } );
  120. } );
  121. describe( 'getGroups()', () => {
  122. it( 'returns iterator of defined groups', () => {
  123. plugin.addItems( 'Arrows', [
  124. { title: 'arrow left', character: '←' }
  125. ] );
  126. plugin.addItems( 'Mathematical', [
  127. { title: 'precedes', character: '≺' },
  128. { title: 'succeeds', character: '≻' }
  129. ] );
  130. const groups = [ ...plugin.getGroups() ];
  131. expect( groups ).to.deep.equal( [ 'Arrows', 'Mathematical' ] );
  132. } );
  133. } );
  134. describe( 'addItems()', () => {
  135. it( 'works with subsequent calls to the same group', () => {
  136. plugin.addItems( 'Mathematical', [ {
  137. title: 'dot',
  138. character: '.'
  139. } ] );
  140. plugin.addItems( 'Mathematical', [ {
  141. title: ',',
  142. character: 'comma'
  143. } ] );
  144. const groups = [ ...plugin.getGroups() ];
  145. expect( groups ).to.deep.equal( [ 'Mathematical' ] );
  146. } );
  147. } );
  148. describe( 'getCharactersForGroup()', () => {
  149. it( 'returns a collection of defined special characters names', () => {
  150. plugin.addItems( 'Mathematical', [
  151. { title: 'precedes', character: '≺' },
  152. { title: 'succeeds', character: '≻' }
  153. ] );
  154. const characters = plugin.getCharactersForGroup( 'Mathematical' );
  155. expect( characters.size ).to.equal( 2 );
  156. expect( characters.has( 'precedes' ) ).to.equal( true );
  157. expect( characters.has( 'succeeds' ) ).to.equal( true );
  158. } );
  159. it( 'returns undefined for non-existing group', () => {
  160. plugin.addItems( 'Mathematical', [
  161. { title: 'precedes', character: '≺' },
  162. { title: 'succeeds', character: '≻' }
  163. ] );
  164. const characters = plugin.getCharactersForGroup( 'Foo' );
  165. expect( characters ).to.be.undefined;
  166. } );
  167. } );
  168. describe( 'getCharacter()', () => {
  169. it( 'returns a collection of defined special characters names', () => {
  170. plugin.addItems( 'Mathematical', [
  171. { title: 'precedes', character: '≺' },
  172. { title: 'succeeds', character: '≻' }
  173. ] );
  174. expect( plugin.getCharacter( 'succeeds' ) ).to.equal( '≻' );
  175. } );
  176. it( 'returns undefined for non-existing character', () => {
  177. expect( plugin.getCharacter( 'succeeds' ) ).to.be.undefined;
  178. } );
  179. } );
  180. } );