8
0

document.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 'ckeditorerror'
  11. );
  12. describe( 'Document', () => {
  13. let Document, RootElement, CKEditorError;
  14. before( () => {
  15. Document = modules[ 'document/document' ];
  16. RootElement = modules[ 'document/rootelement' ];
  17. CKEditorError = modules.ckeditorerror;
  18. } );
  19. let document;
  20. beforeEach( () => {
  21. document = new Document();
  22. } );
  23. describe( 'constructor', () => {
  24. it( 'should create Document with no data', () => {
  25. expect( document ).to.have.property( 'roots' ).that.is.instanceof( Map );
  26. expect( document.roots.size ).to.equal( 1 );
  27. expect( document._graveyard ).to.be.instanceof( RootElement );
  28. expect( document._graveyard.getChildCount() ).to.equal( 0 );
  29. } );
  30. } );
  31. describe( 'createRoot', () => {
  32. it( 'should create a new RootElement, add it to roots map and return it', () => {
  33. let root = document.createRoot( 'root' );
  34. expect( document.roots.size ).to.equal( 2 );
  35. expect( root ).to.be.instanceof( RootElement );
  36. expect( root.getChildCount() ).to.equal( 0 );
  37. } );
  38. it( 'should throw an error when trying to create a second root with the same name', () => {
  39. document.createRoot( 'root' );
  40. expect(
  41. () => {
  42. document.createRoot( 'root' );
  43. }
  44. ).to.throw( CKEditorError, /document-createRoot-name-exists/ );
  45. } );
  46. } );
  47. describe( 'getRoot', () => {
  48. it( 'should return a RootElement previously created with given name', () => {
  49. let newRoot = document.createRoot( 'root' );
  50. let getRoot = document.getRoot( 'root' );
  51. expect( getRoot ).to.equal( newRoot );
  52. } );
  53. it( 'should throw an error when trying to get non-existent root', () => {
  54. expect(
  55. () => {
  56. document.getRoot( 'root' );
  57. }
  58. ).to.throw( CKEditorError, /document-createRoot-root-not-exist/ );
  59. } );
  60. } );
  61. describe( 'applyOperation', () => {
  62. it( 'should increase document version, execute operation and fire operationApplied', () => {
  63. let operationApplied = sinon.spy();
  64. let operation = {
  65. baseVersion: 0,
  66. _execute: sinon.spy()
  67. };
  68. document.on( 'operationApplied', operationApplied );
  69. document.applyOperation( operation );
  70. expect( document.version ).to.equal( 1 );
  71. sinon.assert.calledOnce( operationApplied );
  72. sinon.assert.calledOnce( operation._execute );
  73. } );
  74. it( 'should throw an error on the operation base version and the document version is different', () => {
  75. let operationApplied = sinon.spy();
  76. let operation = {
  77. baseVersion: 1
  78. };
  79. document.on( 'operationApplied', operationApplied );
  80. expect(
  81. () => {
  82. document.applyOperation( operation );
  83. }
  84. ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
  85. } );
  86. } );
  87. } );