document.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  7. import Document from '../../src/view/document';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import count from '@ckeditor/ckeditor5-utils/src/count';
  10. import createViewRoot from './_utils/createroot';
  11. describe( 'Document', () => {
  12. let domRoot, viewDocument;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. domRoot = createElement( document, 'div', {
  16. id: 'editor',
  17. contenteditable: 'true'
  18. } );
  19. document.body.appendChild( domRoot );
  20. viewDocument = new Document();
  21. } );
  22. afterEach( () => {
  23. domRoot.remove();
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'should create Document with all properties', () => {
  27. expect( count( viewDocument.roots ) ).to.equal( 0 );
  28. expect( viewDocument ).to.have.property( 'isReadOnly' ).to.false;
  29. } );
  30. } );
  31. describe( 'is()', () => {
  32. it( 'should return true for "document"', () => {
  33. expect( viewDocument.is( 'document' ) ).to.be.true;
  34. expect( viewDocument.is( 'view:document' ) ).to.be.true;
  35. } );
  36. it( 'should return false for incorrect values', () => {
  37. expect( viewDocument.is( 'model' ) ).to.be.false;
  38. expect( viewDocument.is( 'model:document' ) ).to.be.false;
  39. expect( viewDocument.is( 'node' ) ).to.be.false;
  40. expect( viewDocument.is( 'view:node' ) ).to.be.false;
  41. expect( viewDocument.is( 'element', 'text' ) ).to.be.false;
  42. } );
  43. } );
  44. describe( 'getRoot()', () => {
  45. it( 'should return "main" root', () => {
  46. createViewRoot( viewDocument, 'div', 'main' );
  47. expect( count( viewDocument.roots ) ).to.equal( 1 );
  48. expect( viewDocument.getRoot() ).to.equal( viewDocument.roots.get( 'main' ) );
  49. } );
  50. it( 'should return named root', () => {
  51. createViewRoot( viewDocument, 'h1', 'header' );
  52. expect( count( viewDocument.roots ) ).to.equal( 1 );
  53. expect( viewDocument.getRoot( 'header' ) ).to.equal( viewDocument.roots.get( 'header' ) );
  54. } );
  55. it( 'should return null when trying to get non-existent root', () => {
  56. expect( viewDocument.getRoot( 'not-existing' ) ).to.null;
  57. } );
  58. } );
  59. describe( 'post-fixers', () => {
  60. it( 'should add a callback that is called on _callPostFixers', () => {
  61. const spy1 = sinon.spy();
  62. const spy2 = sinon.spy();
  63. const writerMock = {};
  64. viewDocument.registerPostFixer( spy1 );
  65. viewDocument.registerPostFixer( spy2 );
  66. sinon.assert.notCalled( spy1 );
  67. sinon.assert.notCalled( spy2 );
  68. viewDocument._callPostFixers( writerMock );
  69. sinon.assert.calledOnce( spy1 );
  70. sinon.assert.calledOnce( spy2 );
  71. sinon.assert.calledWithExactly( spy1, writerMock );
  72. sinon.assert.calledWithExactly( spy2, writerMock );
  73. } );
  74. it( 'should call post-fixer until all returns false', () => {
  75. let calls = 0;
  76. const spy1 = sinon.spy( () => calls++ < 2 );
  77. const spy2 = sinon.spy( () => calls++ < 2 );
  78. viewDocument.registerPostFixer( spy1 );
  79. viewDocument.registerPostFixer( spy2 );
  80. viewDocument._callPostFixers();
  81. expect( calls ).to.equal( 4 );
  82. } );
  83. } );
  84. } );