rootattributeoperation.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, operation */
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import RootAttributeOperation from '/ckeditor5/engine/model/operation/rootattributeoperation.js';
  8. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  9. import { jsonParseStringify, wrapInDelta } from '/tests/engine/model/_utils/utils.js';
  10. describe( 'RootAttributeOperation', () => {
  11. let doc, root;
  12. beforeEach( () => {
  13. doc = new Document();
  14. root = doc.createRoot();
  15. } );
  16. describe( 'type', () => {
  17. it( 'should be addRootAttribute for adding attribute', () => {
  18. const op = new RootAttributeOperation(
  19. root,
  20. 'key',
  21. null,
  22. 'newValue',
  23. doc.version
  24. );
  25. expect( op.type ).to.equal( 'addRootAttribute' );
  26. } );
  27. it( 'should be removeRootAttribute for removing attribute', () => {
  28. const op = new RootAttributeOperation(
  29. root,
  30. 'key',
  31. 'oldValue',
  32. null,
  33. doc.version
  34. );
  35. expect( op.type ).to.equal( 'removeRootAttribute' );
  36. } );
  37. it( 'should be changeRootAttribute for removing attribute', () => {
  38. const op = new RootAttributeOperation(
  39. root,
  40. 'key',
  41. 'oldValue',
  42. 'newValue',
  43. doc.version
  44. );
  45. expect( op.type ).to.equal( 'changeRootAttribute' );
  46. } );
  47. } );
  48. it( 'should add attribute on the root element', () => {
  49. doc.applyOperation( wrapInDelta(
  50. new RootAttributeOperation(
  51. root,
  52. 'isNew',
  53. null,
  54. true,
  55. doc.version
  56. )
  57. ) );
  58. expect( doc.version ).to.equal( 1 );
  59. expect( root.hasAttribute( 'isNew' ) ).to.be.true;
  60. } );
  61. it( 'should change attribute on the root element', () => {
  62. root.setAttribute( 'isNew', false );
  63. doc.applyOperation( wrapInDelta(
  64. new RootAttributeOperation(
  65. root,
  66. 'isNew',
  67. false,
  68. true,
  69. doc.version
  70. )
  71. ) );
  72. expect( doc.version ).to.equal( 1 );
  73. expect( root.getAttribute( 'isNew' ) ).to.be.true;
  74. } );
  75. it( 'should remove attribute from the root element', () => {
  76. root.setAttribute( 'x', true );
  77. doc.applyOperation( wrapInDelta(
  78. new RootAttributeOperation(
  79. root,
  80. 'x',
  81. true,
  82. null,
  83. doc.version
  84. )
  85. ) );
  86. expect( doc.version ).to.equal( 1 );
  87. expect( root.hasAttribute( 'x' ) ).to.be.false;
  88. } );
  89. it( 'should create a RootAttributeOperation as a reverse', () => {
  90. let operation = new RootAttributeOperation( root, 'x', 'old', 'new', doc.version );
  91. let reverse = operation.getReversed();
  92. expect( reverse ).to.be.an.instanceof( RootAttributeOperation );
  93. expect( reverse.baseVersion ).to.equal( 1 );
  94. expect( reverse.root ).to.equal( root );
  95. expect( reverse.key ).to.equal( 'x' );
  96. expect( reverse.oldValue ).to.equal( 'new' );
  97. expect( reverse.newValue ).to.equal( 'old' );
  98. } );
  99. it( 'should undo adding attribute by applying reverse operation', () => {
  100. let operation = new RootAttributeOperation(
  101. root,
  102. 'isNew',
  103. null,
  104. true,
  105. doc.version
  106. );
  107. let reverse = operation.getReversed();
  108. doc.applyOperation( wrapInDelta( operation ) );
  109. doc.applyOperation( wrapInDelta( reverse ) );
  110. expect( doc.version ).to.equal( 2 );
  111. expect( root.hasAttribute( 'x' ) ).to.be.false;
  112. } );
  113. it( 'should undo changing attribute by applying reverse operation', () => {
  114. root.setAttribute( 'isNew', false );
  115. let operation = new RootAttributeOperation(
  116. root,
  117. 'isNew',
  118. false,
  119. true,
  120. doc.version
  121. );
  122. let reverse = operation.getReversed();
  123. doc.applyOperation( wrapInDelta( operation ) );
  124. doc.applyOperation( wrapInDelta( reverse ) );
  125. expect( doc.version ).to.equal( 2 );
  126. expect( root.getAttribute( 'isNew' ) ).to.be.false;
  127. } );
  128. it( 'should undo remove attribute by applying reverse operation', () => {
  129. root.setAttribute( 'foo', true );
  130. let operation = new RootAttributeOperation(
  131. root,
  132. 'foo',
  133. true,
  134. null,
  135. doc.version
  136. );
  137. let reverse = operation.getReversed();
  138. doc.applyOperation( wrapInDelta( operation ) );
  139. doc.applyOperation( wrapInDelta( reverse ) );
  140. expect( doc.version ).to.equal( 2 );
  141. expect( root.getAttribute( 'foo' ) ).to.be.true;
  142. } );
  143. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  144. expect( () => {
  145. doc.applyOperation( wrapInDelta(
  146. new RootAttributeOperation(
  147. root,
  148. 'foo',
  149. true,
  150. null,
  151. doc.version
  152. )
  153. ) );
  154. } ).to.throw( CKEditorError, /rootattribute-operation-wrong-old-value/ );
  155. } );
  156. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  157. root.setAttribute( 'x', 1 );
  158. expect( () => {
  159. doc.applyOperation( wrapInDelta(
  160. new RootAttributeOperation(
  161. root,
  162. 'x',
  163. null,
  164. 2,
  165. doc.version
  166. )
  167. ) );
  168. } ).to.throw( CKEditorError, /rootattribute-operation-attribute-exists/ );
  169. } );
  170. it( 'should create a RootAttributeOperation with the same parameters when cloned', () => {
  171. let baseVersion = doc.version;
  172. let op = new RootAttributeOperation( root, 'foo', 'old', 'new', baseVersion );
  173. let clone = op.clone();
  174. // New instance rather than a pointer to the old instance.
  175. expect( clone ).not.to.be.equal( op );
  176. expect( clone ).to.be.instanceof( RootAttributeOperation );
  177. expect( clone.root ).to.equal( root );
  178. expect( clone.key ).to.equal( 'foo' );
  179. expect( clone.oldValue ).to.equal( 'old' );
  180. expect( clone.newValue ).to.equal( 'new' );
  181. expect( clone.baseVersion ).to.equal( baseVersion );
  182. } );
  183. describe( 'toJSON', () => {
  184. it( 'should create proper serialized object', () => {
  185. const op = new RootAttributeOperation(
  186. root,
  187. 'key',
  188. null,
  189. 'newValue',
  190. doc.version
  191. );
  192. const serialized = jsonParseStringify( op );
  193. expect( serialized.__className ).to.equal( 'engine.model.operation.RootAttributeOperation' );
  194. expect( serialized ).to.deep.equal( {
  195. __className: 'engine.model.operation.RootAttributeOperation',
  196. baseVersion: 0,
  197. key: 'key',
  198. newValue: 'newValue',
  199. oldValue: null,
  200. root: 'main'
  201. } );
  202. } );
  203. } );
  204. describe( 'fromJSON', () => {
  205. it( 'should create proper RootAttributeOperation from json object', () => {
  206. const op = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  207. const serialized = jsonParseStringify( op );
  208. const deserialized = RootAttributeOperation.fromJSON( serialized, doc );
  209. expect( deserialized ).to.deep.equal( op );
  210. } );
  211. it( 'should throw an error when root does not exists', () => {
  212. const op = new RootAttributeOperation(
  213. root,
  214. 'key',
  215. null,
  216. 'newValue',
  217. doc.version
  218. );
  219. const serialized = jsonParseStringify( op );
  220. serialized.root = 'no-root';
  221. expect( () => {
  222. RootAttributeOperation.fromJSON( serialized, doc );
  223. } ).to.throw( CKEditorError, /rootattribute-operation-fromjson-no-roo/ );
  224. } );
  225. } );
  226. } );