insertspecialcharactercommand.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import SpecialCharacters from '../src/specialcharacters';
  11. describe( 'InsertSpecialCharacterCommand', () => {
  12. let editor, model, editorElement, command;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicTestEditor
  18. .create( editorElement, {
  19. plugins: [ Paragraph, SpecialCharacters ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. command = editor.commands.get( 'insertSpecialCharacter' );
  25. editor.plugins.get( 'SpecialCharacters' ).addItems( 'Arrows', [
  26. { title: 'arrow left', character: '←' },
  27. { title: 'arrow right', character: '→' }
  28. ] );
  29. } );
  30. } );
  31. afterEach( () => {
  32. return editor.destroy()
  33. .then( () => {
  34. editorElement.remove();
  35. } );
  36. } );
  37. describe( 'isEnabled', () => {
  38. it( 'should be bound to InputCommand#isEnables', () => {
  39. const inputCommand = editor.commands.get( 'input' );
  40. inputCommand.isEnabled = true;
  41. expect( command.isEnabled ).to.equal( true );
  42. inputCommand.isEnabled = false;
  43. expect( command.isEnabled ).to.equal( false );
  44. } );
  45. } );
  46. describe( 'execute()', () => {
  47. it( 'should create a single batch', () => {
  48. setModelData( model, '<paragraph>foo[]</paragraph>' );
  49. const spy = sinon.spy();
  50. model.document.on( 'change', spy );
  51. command.execute( { item: 'arrow left' } );
  52. sinon.assert.calledOnce( spy );
  53. } );
  54. it( 'executes InputCommand#execute()', () => {
  55. const inputCommand = editor.commands.get( 'input' );
  56. setModelData( model, '<paragraph>foo[]</paragraph>' );
  57. const spy = sinon.stub( inputCommand, 'execute' );
  58. command.execute( { item: 'arrow left' } );
  59. sinon.assert.calledWithExactly( spy, { text: '←' } );
  60. spy.restore();
  61. } );
  62. it( 'does nothing if specified object is invalid', () => {
  63. setModelData( model, '<paragraph>foo[]</paragraph>' );
  64. const spy = sinon.spy();
  65. model.document.on( 'change', spy );
  66. command.execute( { foo: 'arrow left' } );
  67. sinon.assert.notCalled( spy );
  68. } );
  69. it( 'does nothing if specified item name does not exist', () => {
  70. setModelData( model, '<paragraph>foo[]</paragraph>' );
  71. const spy = sinon.spy();
  72. model.document.on( 'change', spy );
  73. command.execute( { item: 'arrow up' } );
  74. sinon.assert.notCalled( spy );
  75. } );
  76. } );
  77. } );