detachoperation.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { jsonParseStringify, 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. describe( 'toJSON', () => {
  42. it( 'should create proper json object', () => {
  43. const position = Position.createBefore( element );
  44. const op = new DetachOperation( position, 1, doc.version );
  45. const serialized = jsonParseStringify( op );
  46. expect( serialized ).to.deep.equal( {
  47. __className: 'engine.model.operation.DetachOperation',
  48. baseVersion: 0,
  49. sourcePosition: jsonParseStringify( position ),
  50. howMany: 1
  51. } );
  52. } );
  53. } );
  54. } );