detachoperation.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import DetachOperation from '../../../src/model/operation/detachoperation';
  7. import { wrapInDelta } from '../../../tests/model/_utils/utils';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Position from '../../../src/model/position';
  10. describe( 'DetachOperation', () => {
  11. let doc, batch, docFrag, element;
  12. beforeEach( () => {
  13. doc = new Document();
  14. batch = doc.batch();
  15. docFrag = batch.createDocumentFragment();
  16. element = batch.createElement( 'element' );
  17. batch.append( element, docFrag );
  18. } );
  19. it( 'should have type equal to detach', () => {
  20. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  21. expect( op.type ).to.equal( 'detach' );
  22. } );
  23. it( 'should remove given element from parent', () => {
  24. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  25. doc.applyOperation( wrapInDelta( op ) );
  26. expect( docFrag.childCount ).to.equal( 0 );
  27. } );
  28. it( 'should throw when is executed on element from document', () => {
  29. const root = doc.createRoot();
  30. const element = batch.createElement( 'element' );
  31. batch.append( element, root );
  32. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  33. expect( () => {
  34. op._execute();
  35. } ).to.throw( CKEditorError, /^detach-operation-on-document-node/ );
  36. } );
  37. it( 'should be not a document operation', () => {
  38. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  39. expect( op.isDocumentOperation ).to.false;
  40. } );
  41. } );