utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import AssertionError from 'assertion-error';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import ObservableMixin from '../../src/observablemixin';
  8. import EmitterMixin from '../../src/emittermixin';
  9. import { assertEqualMarkup, createObserver } from '../_utils/utils';
  10. describe( 'utils - testUtils', () => {
  11. afterEach( () => {
  12. sinon.restore();
  13. } );
  14. describe( 'createObserver()', () => {
  15. let observable, observable2, observer;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. observer = createObserver();
  19. observable = Object.create( ObservableMixin );
  20. observable.set( { foo: 0, bar: 0 } );
  21. observable2 = Object.create( ObservableMixin );
  22. observable2.set( { foo: 0, bar: 0 } );
  23. } );
  24. it( 'should create an observer', () => {
  25. function Emitter() {}
  26. Emitter.prototype = EmitterMixin;
  27. expect( observer ).to.be.instanceof( Emitter );
  28. expect( observer.observe ).is.a( 'function' );
  29. expect( observer.stopListening ).is.a( 'function' );
  30. } );
  31. describe( 'Observer', () => {
  32. /* global console:false */
  33. it( 'logs changes in the observable', () => {
  34. const spy = sinon.stub( console, 'log' );
  35. observer.observe( 'Some observable', observable );
  36. observer.observe( 'Some observable 2', observable2 );
  37. observable.foo = 1;
  38. expect( spy.callCount ).to.equal( 1 );
  39. observable.foo = 2;
  40. observable2.bar = 3;
  41. expect( spy.callCount ).to.equal( 3 );
  42. } );
  43. it( 'logs changes to specified properties', () => {
  44. const spy = sinon.stub( console, 'log' );
  45. observer.observe( 'Some observable', observable, [ 'foo' ] );
  46. observable.foo = 1;
  47. expect( spy.callCount ).to.equal( 1 );
  48. observable.bar = 1;
  49. expect( spy.callCount ).to.equal( 1 );
  50. } );
  51. it( 'stops listening when asked to do so', () => {
  52. const spy = sinon.stub( console, 'log' );
  53. observer.observe( 'Some observable', observable );
  54. observable.foo = 1;
  55. expect( spy.callCount ).to.equal( 1 );
  56. observer.stopListening();
  57. observable.foo = 2;
  58. expect( spy.callCount ).to.equal( 1 );
  59. } );
  60. } );
  61. } );
  62. describe( 'assertEqualMarkup()', () => {
  63. it( 'should not throw for equal strings', () => {
  64. expect( assertEqualMarkup( 'foo', 'foo' ) ).to.not.throw;
  65. } );
  66. it( 'should throw AssertionError for not equal strings', () => {
  67. try {
  68. assertEqualMarkup( 'foo', 'bar' );
  69. } catch ( assertionError ) {
  70. expect( assertionError ).to.be.instanceOf( AssertionError );
  71. }
  72. } );
  73. it( 'should throw with default (short) message', () => {
  74. try {
  75. assertEqualMarkup( 'foo', 'bar' );
  76. } catch ( assertionError ) {
  77. expect( assertionError.message ).to.equal( 'Expected markup strings to be equal' );
  78. }
  79. } );
  80. it( 'should throw with passed message', () => {
  81. try {
  82. assertEqualMarkup( 'foo', 'bar', 'baz' );
  83. } catch ( assertionError ) {
  84. expect( assertionError.message ).to.equal( 'baz' );
  85. }
  86. } );
  87. it( 'should format actual string', () => {
  88. try {
  89. assertEqualMarkup( '<div><p><span>foo</span></p></div>', 'bar' );
  90. } catch ( assertionError ) {
  91. expect( assertionError.actual ).to.equal(
  92. '<div>\n' +
  93. ' <p><span>foo</span></p>\n' +
  94. '</div>'
  95. );
  96. }
  97. } );
  98. it( 'should format expected string', () => {
  99. try {
  100. assertEqualMarkup( 'foo', '<div><p><span>foo</span></p></div>' );
  101. } catch ( assertionError ) {
  102. expect( assertionError.expected ).to.equal(
  103. '<div>\n' +
  104. ' <p><span>foo</span></p>\n' +
  105. '</div>'
  106. );
  107. }
  108. } );
  109. it( 'should format model text node with attributes as inline', () => {
  110. try {
  111. assertEqualMarkup( 'foo', '<paragraph><$text bold="true">foo</$text></paragraph>' );
  112. } catch ( assertionError ) {
  113. expect( assertionError.expected ).to.equal(
  114. '<paragraph><$text bold="true">foo</$text></paragraph>'
  115. );
  116. }
  117. } );
  118. it( 'should format nested model structure properly', () => {
  119. try {
  120. assertEqualMarkup( 'foo',
  121. '<blockQuote>' +
  122. '<table>' +
  123. '<tableRow>' +
  124. '<tableCell>' +
  125. '<paragraph><$text bold="true">foo</$text></paragraph>' +
  126. '</tableCell>' +
  127. '<tableCell>' +
  128. '<paragraph><$text bold="true">bar</$text></paragraph>' +
  129. '<paragraph><$text bold="true">baz</$text></paragraph>' +
  130. '</tableCell>' +
  131. '</tableRow>' +
  132. '</table>' +
  133. '</blockQuote>'
  134. );
  135. } catch ( assertionError ) {
  136. expect( assertionError.expected ).to.equal(
  137. '<blockQuote>\n' +
  138. ' <table>\n' +
  139. ' <tableRow>\n' +
  140. ' <tableCell>\n' +
  141. ' <paragraph><$text bold="true">foo</$text></paragraph>\n' +
  142. ' </tableCell>\n' +
  143. ' <tableCell>\n' +
  144. ' <paragraph><$text bold="true">bar</$text></paragraph>\n' +
  145. ' <paragraph><$text bold="true">baz</$text></paragraph>\n' +
  146. ' </tableCell>\n' +
  147. ' </tableRow>\n' +
  148. ' </table>\n' +
  149. '</blockQuote>'
  150. );
  151. }
  152. } );
  153. } );
  154. } );