document.js 2.9 KB

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