deletecommand.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import DeleteCommand from '../src/deletecommand';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. describe( 'DeleteCommand', () => {
  10. let editor, doc;
  11. testUtils.createSinonSandbox();
  12. beforeEach( () => {
  13. return ModelTestEditor.create( )
  14. .then( newEditor => {
  15. editor = newEditor;
  16. doc = editor.document;
  17. const command = new DeleteCommand( editor, 'backward' );
  18. editor.commands.set( 'delete', command );
  19. doc.schema.registerItem( 'p', '$block' );
  20. } );
  21. } );
  22. it( 'has direction', () => {
  23. const command = new DeleteCommand( editor, 'forward' );
  24. expect( command ).to.have.property( 'direction', 'forward' );
  25. } );
  26. describe( 'execute', () => {
  27. it( 'uses enqueueChanges', () => {
  28. setData( doc, '<p>foo[]bar</p>' );
  29. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  30. editor.execute( 'delete' );
  31. expect( spy.calledOnce ).to.be.true;
  32. } );
  33. it( 'locks buffer when executing', () => {
  34. setData( doc, '<p>foo[]bar</p>' );
  35. const buffer = editor.commands.get( 'delete' )._buffer;
  36. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  37. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  38. editor.execute( 'delete' );
  39. expect( lockSpy.calledOnce ).to.be.true;
  40. expect( unlockSpy.calledOnce ).to.be.true;
  41. } );
  42. it( 'deletes previous character when selection is collapsed', () => {
  43. setData( doc, '<p>foo[]bar</p>' );
  44. editor.execute( 'delete' );
  45. expect( getData( doc, { selection: true } ) ).to.equal( '<p>fo[]bar</p>' );
  46. } );
  47. it( 'deletes selection contents', () => {
  48. setData( doc, '<p>fo[ob]ar</p>' );
  49. editor.execute( 'delete' );
  50. expect( getData( doc, { selection: true } ) ).to.equal( '<p>fo[]ar</p>' );
  51. } );
  52. it( 'merges elements', () => {
  53. setData( doc, '<p>foo</p><p>[]bar</p>' );
  54. editor.execute( 'delete' );
  55. expect( getData( doc, { selection: true } ) ).to.equal( '<p>foo[]bar</p>' );
  56. } );
  57. it( 'does not try to delete when selection is at the boundary', () => {
  58. const spy = sinon.spy();
  59. editor.data.on( 'deleteContent', spy );
  60. setData( doc, '<p>[]foo</p>' );
  61. editor.execute( 'delete' );
  62. expect( getData( doc, { selection: true } ) ).to.equal( '<p>[]foo</p>' );
  63. expect( spy.callCount ).to.equal( 0 );
  64. } );
  65. it( 'passes options to modifySelection', () => {
  66. const spy = sinon.spy();
  67. editor.data.on( 'modifySelection', spy );
  68. setData( doc, '<p>foo[]bar</p>' );
  69. editor.commands.get( 'delete' ).direction = 'forward';
  70. editor.execute( 'delete', { unit: 'word' } );
  71. expect( spy.callCount ).to.equal( 1 );
  72. const modifyOpts = spy.args[ 0 ][ 1 ].options;
  73. expect( modifyOpts ).to.have.property( 'direction', 'forward' );
  74. expect( modifyOpts ).to.have.property( 'unit', 'word' );
  75. } );
  76. } );
  77. } );