document.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. 'document/document',
  9. 'document/rootelement',
  10. 'document/transaction',
  11. 'ckeditorerror'
  12. );
  13. describe( 'Document', () => {
  14. let Document, RootElement, Transaction, CKEditorError;
  15. before( () => {
  16. Document = modules[ 'document/document' ];
  17. RootElement = modules[ 'document/rootelement' ];
  18. Transaction = modules[ 'document/transaction' ];
  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', () => {
  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 operationApplied', () => {
  65. let operationApplied = sinon.spy();
  66. let operation = {
  67. baseVersion: 0,
  68. _execute: sinon.spy()
  69. };
  70. document.on( 'operationApplied', operationApplied );
  71. document.applyOperation( operation );
  72. expect( document.version ).to.equal( 1 );
  73. sinon.assert.calledOnce( operationApplied );
  74. sinon.assert.calledOnce( operation._execute );
  75. } );
  76. it( 'should throw an error on the operation base version and the document version is different', () => {
  77. let operationApplied = sinon.spy();
  78. let operation = {
  79. baseVersion: 1
  80. };
  81. document.on( 'operationApplied', operationApplied );
  82. expect(
  83. () => {
  84. document.applyOperation( operation );
  85. }
  86. ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
  87. } );
  88. } );
  89. describe( 'createTransaction', () => {
  90. it( 'should create a new transaction with the document property', () => {
  91. const transaction = document.createTransaction();
  92. expect( transaction ).to.be.instanceof( Transaction );
  93. expect( transaction ).to.have.property( 'doc' ).that.equals( document );
  94. } );
  95. } );
  96. } );