8
0

rootattributeoperation.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. describe( 'isDocumentOperation', () => {
  48. it( 'should be true when root is in the document', () => {
  49. const operation = new RootAttributeOperation(
  50. root,
  51. 'isNew',
  52. null,
  53. true,
  54. doc.version
  55. );
  56. expect( operation.isDocumentOperation ).to.true;
  57. } );
  58. it( 'should be false when root is not in the document', () => {
  59. const element = doc.batch().createElement( 'element' );
  60. const operation = new RootAttributeOperation(
  61. element,
  62. 'isNew',
  63. null,
  64. true,
  65. doc.version
  66. );
  67. expect( operation.isDocumentOperation ).to.false;
  68. } );
  69. } );
  70. it( 'should add attribute on the root element', () => {
  71. doc.applyOperation( wrapInDelta(
  72. new RootAttributeOperation(
  73. root,
  74. 'isNew',
  75. null,
  76. true,
  77. doc.version
  78. )
  79. ) );
  80. expect( doc.version ).to.equal( 1 );
  81. expect( root.hasAttribute( 'isNew' ) ).to.be.true;
  82. } );
  83. it( 'should change attribute on the root element', () => {
  84. root.setAttribute( 'isNew', false );
  85. doc.applyOperation( wrapInDelta(
  86. new RootAttributeOperation(
  87. root,
  88. 'isNew',
  89. false,
  90. true,
  91. doc.version
  92. )
  93. ) );
  94. expect( doc.version ).to.equal( 1 );
  95. expect( root.getAttribute( 'isNew' ) ).to.be.true;
  96. } );
  97. it( 'should remove attribute from the root element', () => {
  98. root.setAttribute( 'x', true );
  99. doc.applyOperation( wrapInDelta(
  100. new RootAttributeOperation(
  101. root,
  102. 'x',
  103. true,
  104. null,
  105. doc.version
  106. )
  107. ) );
  108. expect( doc.version ).to.equal( 1 );
  109. expect( root.hasAttribute( 'x' ) ).to.be.false;
  110. } );
  111. it( 'should create a RootAttributeOperation as a reverse', () => {
  112. const operation = new RootAttributeOperation( root, 'x', 'old', 'new', doc.version );
  113. const reverse = operation.getReversed();
  114. expect( reverse ).to.be.an.instanceof( RootAttributeOperation );
  115. expect( reverse.baseVersion ).to.equal( 1 );
  116. expect( reverse.root ).to.equal( root );
  117. expect( reverse.key ).to.equal( 'x' );
  118. expect( reverse.oldValue ).to.equal( 'new' );
  119. expect( reverse.newValue ).to.equal( 'old' );
  120. } );
  121. it( 'should undo adding attribute by applying reverse operation', () => {
  122. const operation = new RootAttributeOperation(
  123. root,
  124. 'isNew',
  125. null,
  126. true,
  127. doc.version
  128. );
  129. const reverse = operation.getReversed();
  130. doc.applyOperation( wrapInDelta( operation ) );
  131. doc.applyOperation( wrapInDelta( reverse ) );
  132. expect( doc.version ).to.equal( 2 );
  133. expect( root.hasAttribute( 'x' ) ).to.be.false;
  134. } );
  135. it( 'should undo changing attribute by applying reverse operation', () => {
  136. root.setAttribute( 'isNew', false );
  137. const operation = new RootAttributeOperation(
  138. root,
  139. 'isNew',
  140. false,
  141. true,
  142. doc.version
  143. );
  144. const reverse = operation.getReversed();
  145. doc.applyOperation( wrapInDelta( operation ) );
  146. doc.applyOperation( wrapInDelta( reverse ) );
  147. expect( doc.version ).to.equal( 2 );
  148. expect( root.getAttribute( 'isNew' ) ).to.be.false;
  149. } );
  150. it( 'should undo remove attribute by applying reverse operation', () => {
  151. root.setAttribute( 'foo', true );
  152. const operation = new RootAttributeOperation(
  153. root,
  154. 'foo',
  155. true,
  156. null,
  157. doc.version
  158. );
  159. const reverse = operation.getReversed();
  160. doc.applyOperation( wrapInDelta( operation ) );
  161. doc.applyOperation( wrapInDelta( reverse ) );
  162. expect( doc.version ).to.equal( 2 );
  163. expect( root.getAttribute( 'foo' ) ).to.be.true;
  164. } );
  165. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  166. expect( () => {
  167. doc.applyOperation( wrapInDelta(
  168. new RootAttributeOperation(
  169. root,
  170. 'foo',
  171. true,
  172. null,
  173. doc.version
  174. )
  175. ) );
  176. } ).to.throw( CKEditorError, /rootattribute-operation-wrong-old-value/ );
  177. } );
  178. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  179. root.setAttribute( 'x', 1 );
  180. expect( () => {
  181. doc.applyOperation( wrapInDelta(
  182. new RootAttributeOperation(
  183. root,
  184. 'x',
  185. null,
  186. 2,
  187. doc.version
  188. )
  189. ) );
  190. } ).to.throw( CKEditorError, /rootattribute-operation-attribute-exists/ );
  191. } );
  192. it( 'should create a RootAttributeOperation with the same parameters when cloned', () => {
  193. const baseVersion = doc.version;
  194. const op = new RootAttributeOperation( root, 'foo', 'old', 'new', baseVersion );
  195. const clone = op.clone();
  196. // New instance rather than a pointer to the old instance.
  197. expect( clone ).not.to.be.equal( op );
  198. expect( clone ).to.be.instanceof( RootAttributeOperation );
  199. expect( clone.root ).to.equal( root );
  200. expect( clone.key ).to.equal( 'foo' );
  201. expect( clone.oldValue ).to.equal( 'old' );
  202. expect( clone.newValue ).to.equal( 'new' );
  203. expect( clone.baseVersion ).to.equal( baseVersion );
  204. } );
  205. describe( 'toJSON', () => {
  206. it( 'should create proper serialized object', () => {
  207. const op = new RootAttributeOperation(
  208. root,
  209. 'key',
  210. null,
  211. 'newValue',
  212. doc.version
  213. );
  214. const serialized = jsonParseStringify( op );
  215. expect( serialized.__className ).to.equal( 'engine.model.operation.RootAttributeOperation' );
  216. expect( serialized ).to.deep.equal( {
  217. __className: 'engine.model.operation.RootAttributeOperation',
  218. baseVersion: 0,
  219. key: 'key',
  220. newValue: 'newValue',
  221. oldValue: null,
  222. root: 'main'
  223. } );
  224. } );
  225. } );
  226. describe( 'fromJSON', () => {
  227. it( 'should create proper RootAttributeOperation from json object', () => {
  228. const op = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  229. const serialized = jsonParseStringify( op );
  230. const deserialized = RootAttributeOperation.fromJSON( serialized, doc );
  231. expect( deserialized ).to.deep.equal( op );
  232. } );
  233. it( 'should throw an error when root does not exists', () => {
  234. const op = new RootAttributeOperation(
  235. root,
  236. 'key',
  237. null,
  238. 'newValue',
  239. doc.version
  240. );
  241. const serialized = jsonParseStringify( op );
  242. serialized.root = 'no-root';
  243. expect( () => {
  244. RootAttributeOperation.fromJSON( serialized, doc );
  245. } ).to.throw( CKEditorError, /rootattribute-operation-fromjson-no-roo/ );
  246. } );
  247. } );
  248. } );