utils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  6. import ObservableMixin from '../../src/observablemixin';
  7. import EmitterMixin from '../../src/emittermixin';
  8. import { createObserver } from '../_utils/utils';
  9. describe( 'utils - testUtils', () => {
  10. afterEach( () => {
  11. sinon.restore();
  12. } );
  13. describe( 'createObserver()', () => {
  14. let observable, observable2, observer;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. observer = createObserver();
  18. observable = Object.create( ObservableMixin );
  19. observable.set( { foo: 0, bar: 0 } );
  20. observable2 = Object.create( ObservableMixin );
  21. observable2.set( { foo: 0, bar: 0 } );
  22. } );
  23. it( 'should create an observer', () => {
  24. function Emitter() { }
  25. Emitter.prototype = EmitterMixin;
  26. expect( observer ).to.be.instanceof( Emitter );
  27. expect( observer.observe ).is.a( 'function' );
  28. expect( observer.stopListening ).is.a( 'function' );
  29. } );
  30. describe( 'Observer', () => {
  31. /* global console:false */
  32. it( 'logs changes in the observable', () => {
  33. const spy = sinon.stub( console, 'log' );
  34. observer.observe( 'Some observable', observable );
  35. observer.observe( 'Some observable 2', observable2 );
  36. observable.foo = 1;
  37. expect( spy.callCount ).to.equal( 1 );
  38. observable.foo = 2;
  39. observable2.bar = 3;
  40. expect( spy.callCount ).to.equal( 3 );
  41. } );
  42. it( 'logs changes to specified properties', () => {
  43. const spy = sinon.stub( console, 'log' );
  44. observer.observe( 'Some observable', observable, [ 'foo' ] );
  45. observable.foo = 1;
  46. expect( spy.callCount ).to.equal( 1 );
  47. observable.bar = 1;
  48. expect( spy.callCount ).to.equal( 1 );
  49. } );
  50. it( 'stops listening when asked to do so', () => {
  51. const spy = sinon.stub( console, 'log' );
  52. observer.observe( 'Some observable', observable );
  53. observable.foo = 1;
  54. expect( spy.callCount ).to.equal( 1 );
  55. observer.stopListening();
  56. observable.foo = 2;
  57. expect( spy.callCount ).to.equal( 1 );
  58. } );
  59. } );
  60. } );
  61. } );