detachoperation.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { 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 );
  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 );
  26. model.applyOperation( wrapInDelta( op ) );
  27. expect( docFrag.childCount ).to.equal( 0 );
  28. } );
  29. describe( '_validate()', () => {
  30. it( 'should throw when is executed on element from document', () => {
  31. const root = doc.createRoot();
  32. const element = new Element( 'element' );
  33. root._appendChild( [ element ] );
  34. const op = new DetachOperation( Position.createBefore( element ), 1 );
  35. expect( () => {
  36. op._validate();
  37. } ).to.throw( CKEditorError, /^detach-operation-on-document-node/ );
  38. } );
  39. } );
  40. it( 'should be not a document operation', () => {
  41. const op = new DetachOperation( Position.createBefore( element ), 1 );
  42. expect( op.isDocumentOperation ).to.false;
  43. } );
  44. describe( 'toJSON', () => {
  45. it( 'should create proper json object', () => {
  46. const position = Position.createBefore( element );
  47. const op = new DetachOperation( position, 1 );
  48. const serialized = jsonParseStringify( op );
  49. expect( serialized ).to.deep.equal( {
  50. __className: 'engine.model.operation.DetachOperation',
  51. baseVersion: null,
  52. sourcePosition: jsonParseStringify( position ),
  53. howMany: 1
  54. } );
  55. } );
  56. } );
  57. } );