8
0

rootattributeoperation.js 6.7 KB

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