8
0

detachoperation.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  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. import DocumentFragment from '../../../src/model/documentfragment';
  11. import Element from '../../../src/model/element';
  12. describe( 'DetachOperation', () => {
  13. let model, doc, docFrag, element;
  14. beforeEach( () => {
  15. model = new Model();
  16. doc = model.document;
  17. element = new Element( 'element' );
  18. docFrag = new DocumentFragment( [ element ] );
  19. } );
  20. it( 'should have type equal to detach', () => {
  21. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  22. expect( op.type ).to.equal( 'detach' );
  23. } );
  24. it( 'should remove given element from parent', () => {
  25. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  26. model.applyOperation( wrapInDelta( op ) );
  27. expect( docFrag.childCount ).to.equal( 0 );
  28. } );
  29. it( 'should throw when is executed on element from document', () => {
  30. const root = doc.createRoot();
  31. const element = new Element( 'element' );
  32. root.appendChildren( [ element ] );
  33. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  34. expect( () => {
  35. op._execute();
  36. } ).to.throw( CKEditorError, /^detach-operation-on-document-node/ );
  37. } );
  38. it( 'should be not a document operation', () => {
  39. const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
  40. expect( op.isDocumentOperation ).to.false;
  41. } );
  42. describe( 'toJSON', () => {
  43. it( 'should create proper json object', () => {
  44. const position = Position.createBefore( element );
  45. const op = new DetachOperation( position, 1, doc.version );
  46. const serialized = jsonParseStringify( op );
  47. expect( serialized ).to.deep.equal( {
  48. __className: 'engine.model.operation.DetachOperation',
  49. baseVersion: 0,
  50. sourcePosition: jsonParseStringify( position ),
  51. howMany: 1
  52. } );
  53. } );
  54. } );
  55. } );