detachoperation.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @license Copyright (c) 2003-2019, 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( 'is()', () => {
  29. let operation;
  30. before( () => {
  31. const position = Position._createBefore( element );
  32. operation = new DetachOperation( position, 1 );
  33. } );
  34. it( 'should return true for all valid names of "detach" operation', () => {
  35. expect( operation.is( 'operation' ) ).to.be.true;
  36. expect( operation.is( 'model:operation' ) ).to.be.true;
  37. expect( operation.is( 'detachOperation' ) ).to.be.true;
  38. expect( operation.is( 'model:operation:detach' ) ).to.be.true;
  39. } );
  40. it( 'should return false for invalid parameters', () => {
  41. expect( operation.is( 'operation:detach' ) ).to.be.false;
  42. expect( operation.is( 'model:operation:insert' ) ).to.be.false;
  43. expect( operation.is( 'noOperation' ) ).to.be.false;
  44. expect( operation.is( 'attributeOperation' ) ).to.be.false;
  45. expect( operation.is( 'rootAttributeOperation' ) ).to.be.false;
  46. expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false;
  47. } );
  48. } );
  49. describe( '_validate()', () => {
  50. it( 'should throw when is executed on element from document', () => {
  51. const root = doc.createRoot();
  52. const element = new Element( 'element' );
  53. root._appendChild( [ element ] );
  54. const op = new DetachOperation( Position._createBefore( element ), 1 );
  55. expectToThrowCKEditorError( () => {
  56. op._validate();
  57. }, /^detach-operation-on-document-node/, model );
  58. } );
  59. } );
  60. it( 'should be not a document operation', () => {
  61. const op = new DetachOperation( Position._createBefore( element ), 1 );
  62. expect( op.isDocumentOperation ).to.false;
  63. } );
  64. describe( 'toJSON', () => {
  65. it( 'should create proper json object', () => {
  66. const position = Position._createBefore( element );
  67. const op = new DetachOperation( position, 1 );
  68. const serialized = op.toJSON();
  69. expect( serialized ).to.deep.equal( {
  70. __className: 'DetachOperation',
  71. baseVersion: null,
  72. sourcePosition: position.toJSON(),
  73. howMany: 1
  74. } );
  75. } );
  76. } );
  77. } );