detachoperation.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. import Position from '../../../src/model/position';
  9. import DocumentFragment from '../../../src/model/documentfragment';
  10. import Element from '../../../src/model/element';
  11. describe( 'DetachOperation', () => {
  12. let model, doc, docFrag, element;
  13. beforeEach( () => {
  14. model = new Model();
  15. doc = model.document;
  16. element = new Element( 'element' );
  17. docFrag = new DocumentFragment( [ element ] );
  18. } );
  19. it( 'should have type equal to detach', () => {
  20. const op = new DetachOperation( Position.createBefore( element ), 1 );
  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 );
  25. model.applyOperation( op );
  26. expect( docFrag.childCount ).to.equal( 0 );
  27. } );
  28. describe( '_validate()', () => {
  29. it( 'should throw when is executed on element from document', () => {
  30. const root = doc.createRoot();
  31. const element = new Element( 'element' );
  32. root._appendChild( [ element ] );
  33. const op = new DetachOperation( Position.createBefore( element ), 1 );
  34. expect( () => {
  35. op._validate();
  36. } ).to.throw( CKEditorError, /^detach-operation-on-document-node/ );
  37. } );
  38. } );
  39. it( 'should be not a document operation', () => {
  40. const op = new DetachOperation( Position.createBefore( element ), 1 );
  41. expect( op.isDocumentOperation ).to.false;
  42. } );
  43. describe( 'toJSON', () => {
  44. it( 'should create proper json object', () => {
  45. const position = Position.createBefore( element );
  46. const op = new DetachOperation( position, 1 );
  47. const serialized = op.toJSON();
  48. expect( serialized ).to.deep.equal( {
  49. __className: 'engine.model.operation.DetachOperation',
  50. baseVersion: null,
  51. sourcePosition: position.toJSON(),
  52. howMany: 1
  53. } );
  54. } );
  55. } );
  56. } );