detachoperation.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../../src/model/model';
  6. import DetachOperation from '../../../src/model/operation/detachoperation';
  7. import Position from '../../../src/model/position';
  8. import DocumentFragment from '../../../src/model/documentfragment';
  9. import Element from '../../../src/model/element';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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. expectToThrowCKEditorError( () => {
  35. op._validate();
  36. }, /^detach-operation-on-document-node/, model );
  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: 'DetachOperation',
  50. baseVersion: null,
  51. sourcePosition: position.toJSON(),
  52. howMany: 1
  53. } );
  54. } );
  55. } );
  56. } );