utils.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import testUtils from '/tests/core/_utils/utils.js';
  6. import utilsTestUtils from '/tests/utils/_utils/utils.js';
  7. import ObesrvableMixin from '/ckeditor5/utils/observablemixin.js';
  8. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  9. testUtils.createSinonSandbox();
  10. describe( 'utilsTestUtils.createObserver()', () => {
  11. let observable, observable2, observer;
  12. beforeEach( () => {
  13. observer = utilsTestUtils.createObserver();
  14. observable = Object.create( ObesrvableMixin );
  15. observable.set( { foo: 0, bar: 0 } );
  16. observable2 = Object.create( ObesrvableMixin );
  17. observable2.set( { foo: 0, bar: 0 } );
  18. } );
  19. it( 'should create an observer', () => {
  20. function Emitter() {}
  21. Emitter.prototype = EmitterMixin;
  22. expect( observer ).to.be.instanceof( Emitter );
  23. expect( observer.observe ).is.a( 'function' );
  24. expect( observer.stopListening ).is.a( 'function' );
  25. } );
  26. describe( 'Observer', () => {
  27. /* global console:false */
  28. it( 'logs changes in the observable', () => {
  29. const spy = testUtils.sinon.stub( console, 'log' );
  30. observer.observe( 'Some observable', observable );
  31. observer.observe( 'Some observable 2', observable2 );
  32. observable.foo = 1;
  33. expect( spy.callCount ).to.equal( 1 );
  34. observable.foo = 2;
  35. observable2.bar = 3;
  36. expect( spy.callCount ).to.equal( 3 );
  37. } );
  38. it( 'logs changes to specified properties', () => {
  39. const spy = testUtils.sinon.stub( console, 'log' );
  40. observer.observe( 'Some observable', observable, [ 'foo' ] );
  41. observable.foo = 1;
  42. expect( spy.callCount ).to.equal( 1 );
  43. observable.bar = 1;
  44. expect( spy.callCount ).to.equal( 1 );
  45. } );
  46. it( 'stops listening when asked to do so', () => {
  47. const spy = testUtils.sinon.stub( console, 'log' );
  48. observer.observe( 'Some observable', observable );
  49. observable.foo = 1;
  50. expect( spy.callCount ).to.equal( 1 );
  51. observer.stopListening();
  52. observable.foo = 2;
  53. expect( spy.callCount ).to.equal( 1 );
  54. } );
  55. } );
  56. } );