deletecommand.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.add( 'delete', command );
  19. doc.schema.registerItem( 'p', '$block' );
  20. } );
  21. } );
  22. afterEach( () => {
  23. return editor.destroy();
  24. } );
  25. it( 'has direction', () => {
  26. const command = new DeleteCommand( editor, 'forward' );
  27. expect( command ).to.have.property( 'direction', 'forward' );
  28. } );
  29. describe( 'execute()', () => {
  30. it( 'uses enqueueChanges', () => {
  31. setData( doc, '<p>foo[]bar</p>' );
  32. const spy = testUtils.sinon.spy( doc, 'enqueueChanges' );
  33. editor.execute( 'delete' );
  34. expect( spy.calledOnce ).to.be.true;
  35. } );
  36. it( 'locks buffer when executing', () => {
  37. setData( doc, '<p>foo[]bar</p>' );
  38. const buffer = editor.commands.get( 'delete' )._buffer;
  39. const lockSpy = testUtils.sinon.spy( buffer, 'lock' );
  40. const unlockSpy = testUtils.sinon.spy( buffer, 'unlock' );
  41. editor.execute( 'delete' );
  42. expect( lockSpy.calledOnce ).to.be.true;
  43. expect( unlockSpy.calledOnce ).to.be.true;
  44. } );
  45. it( 'deletes previous character when selection is collapsed', () => {
  46. setData( doc, '<p>foo[]bar</p>' );
  47. editor.execute( 'delete' );
  48. expect( getData( doc, { selection: true } ) ).to.equal( '<p>fo[]bar</p>' );
  49. } );
  50. it( 'deletes selection contents', () => {
  51. setData( doc, '<p>fo[ob]ar</p>' );
  52. editor.execute( 'delete' );
  53. expect( getData( doc, { selection: true } ) ).to.equal( '<p>fo[]ar</p>' );
  54. } );
  55. it( 'merges elements', () => {
  56. setData( doc, '<p>foo</p><p>[]bar</p>' );
  57. editor.execute( 'delete' );
  58. expect( getData( doc, { selection: true } ) ).to.equal( '<p>foo[]bar</p>' );
  59. } );
  60. it( 'does not try to delete when selection is at the boundary', () => {
  61. const spy = sinon.spy();
  62. editor.data.on( 'deleteContent', spy );
  63. setData( doc, '<p>[]foo</p>' );
  64. editor.execute( 'delete' );
  65. expect( getData( doc, { selection: true } ) ).to.equal( '<p>[]foo</p>' );
  66. expect( spy.callCount ).to.equal( 0 );
  67. } );
  68. it( 'passes options to modifySelection', () => {
  69. const spy = sinon.spy();
  70. editor.data.on( 'modifySelection', spy );
  71. setData( doc, '<p>foo[]bar</p>' );
  72. editor.commands.get( 'delete' ).direction = 'forward';
  73. editor.execute( 'delete', { unit: 'word' } );
  74. expect( spy.callCount ).to.equal( 1 );
  75. const modifyOpts = spy.args[ 0 ][ 1 ][ 1 ];
  76. expect( modifyOpts ).to.have.property( 'direction', 'forward' );
  77. expect( modifyOpts ).to.have.property( 'unit', 'word' );
  78. } );
  79. it( 'passes options to deleteContent #1', () => {
  80. const spy = sinon.spy();
  81. editor.data.on( 'deleteContent', spy );
  82. setData( doc, '<p>foo[]bar</p>' );
  83. editor.execute( 'delete' );
  84. expect( spy.callCount ).to.equal( 1 );
  85. const deleteOpts = spy.args[ 0 ][ 1 ][ 2 ];
  86. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', true );
  87. } );
  88. it( 'passes options to deleteContent #2', () => {
  89. const spy = sinon.spy();
  90. editor.data.on( 'deleteContent', spy );
  91. setData( doc, '<p>[foobar]</p>' );
  92. editor.execute( 'delete' );
  93. expect( spy.callCount ).to.equal( 1 );
  94. const deleteOpts = spy.args[ 0 ][ 1 ][ 2 ];
  95. expect( deleteOpts ).to.have.property( 'doNotResetEntireContent', false );
  96. } );
  97. } );
  98. } );