8
0

document.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'treemodel/document',
  9. 'treemodel/rootelement',
  10. 'treemodel/batch',
  11. 'ckeditorerror'
  12. );
  13. describe( 'Document', () => {
  14. let Document, RootElement, Batch, CKEditorError;
  15. before( () => {
  16. Document = modules[ 'treemodel/document' ];
  17. RootElement = modules[ 'treemodel/rootelement' ];
  18. Batch = modules[ 'treemodel/batch' ];
  19. CKEditorError = modules.ckeditorerror;
  20. } );
  21. let document;
  22. beforeEach( () => {
  23. document = new Document();
  24. } );
  25. describe( 'constructor', () => {
  26. it( 'should create Document with no data and empty graveyard', () => {
  27. expect( document ).to.have.property( 'roots' ).that.is.instanceof( Map );
  28. expect( document.roots.size ).to.equal( 1 );
  29. expect( document.graveyard ).to.be.instanceof( RootElement );
  30. expect( document.graveyard.getChildCount() ).to.equal( 0 );
  31. } );
  32. } );
  33. describe( 'createRoot', () => {
  34. it( 'should create a new RootElement, add it to roots map and return it', () => {
  35. let root = document.createRoot( 'root' );
  36. expect( document.roots.size ).to.equal( 2 );
  37. expect( root ).to.be.instanceof( RootElement );
  38. expect( root.getChildCount() ).to.equal( 0 );
  39. } );
  40. it( 'should throw an error when trying to create a second root with the same name', () => {
  41. document.createRoot( 'root' );
  42. expect(
  43. () => {
  44. document.createRoot( 'root' );
  45. }
  46. ).to.throw( CKEditorError, /document-createRoot-name-exists/ );
  47. } );
  48. } );
  49. describe( 'getRoot', () => {
  50. it( 'should return a RootElement previously created with given name', () => {
  51. let newRoot = document.createRoot( 'root' );
  52. let getRoot = document.getRoot( 'root' );
  53. expect( getRoot ).to.equal( newRoot );
  54. } );
  55. it( 'should throw an error when trying to get non-existent root', () => {
  56. expect(
  57. () => {
  58. document.getRoot( 'root' );
  59. }
  60. ).to.throw( CKEditorError, /document-createRoot-root-not-exist/ );
  61. } );
  62. } );
  63. describe( 'applyOperation', () => {
  64. it( 'should increase document version, execute operation and fire event with proper data', () => {
  65. const changeCallback = sinon.spy();
  66. const type = 't';
  67. const data = { data: 'x' };
  68. const batch = 'batch';
  69. let operation = {
  70. type: type,
  71. delta: { batch: batch },
  72. baseVersion: 0,
  73. _execute: sinon.stub().returns( data )
  74. };
  75. document.on( 'change', changeCallback );
  76. document.applyOperation( operation );
  77. expect( document.version ).to.equal( 1 );
  78. sinon.assert.calledOnce( operation._execute );
  79. sinon.assert.calledOnce( changeCallback );
  80. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  81. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  82. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  83. } );
  84. it( 'should throw an error on the operation base version and the document version is different', () => {
  85. let operation = {
  86. baseVersion: 1
  87. };
  88. expect(
  89. () => {
  90. document.applyOperation( operation );
  91. }
  92. ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
  93. } );
  94. } );
  95. describe( 'batch', () => {
  96. it( 'should create a new batch with the document property', () => {
  97. const batch = document.batch();
  98. expect( batch ).to.be.instanceof( Batch );
  99. expect( batch ).to.have.property( 'doc' ).that.equals( document );
  100. } );
  101. } );
  102. } );