utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import testUtils from '/tests/_utils/utils.js';
  7. import amdTestUtils from '/tests/_utils/amd.js';
  8. import coreTestUtils from '/tests/core/_utils/utils.js';
  9. import Model from '/ckeditor5/core/ui/model.js';
  10. import Creator from '/ckeditor5/core/creator.js';
  11. import EmitterMixin from '/ckeditor5/core/emittermixin.js';
  12. let createFn3 = () => {};
  13. let destroyFn3 = () => {};
  14. testUtils.createSinonSandbox();
  15. coreTestUtils.defineEditorCreatorMock( 'test1' );
  16. coreTestUtils.defineEditorCreatorMock( 'test2', {
  17. foo: 1,
  18. bar: 2
  19. } );
  20. coreTestUtils.defineEditorCreatorMock( 'test3', {
  21. create: createFn3,
  22. destroy: destroyFn3
  23. } );
  24. const modules = amdTestUtils.require( {
  25. testCreator1: 'creator-test1/creator-test1',
  26. testCreator2: 'creator-test2/creator-test2',
  27. testCreator3: 'creator-test3/creator-test3'
  28. } );
  29. ///////////////////
  30. let TestCreator1, TestCreator2, TestCreator3;
  31. before( () => {
  32. TestCreator1 = modules.testCreator1;
  33. TestCreator2 = modules.testCreator2;
  34. TestCreator3 = modules.testCreator3;
  35. } );
  36. describe( 'coreTestUtils.defineEditorCreatorMock()', () => {
  37. it( 'should register all creators', () => {
  38. expect( TestCreator1.prototype ).to.be.instanceof( Creator );
  39. expect( TestCreator2.prototype ).to.be.instanceof( Creator );
  40. expect( TestCreator3.prototype ).to.be.instanceof( Creator );
  41. } );
  42. it( 'should copy properties from the second argument', () => {
  43. expect( TestCreator2.prototype ).to.have.property( 'foo', 1 );
  44. expect( TestCreator2.prototype ).to.have.property( 'bar', 2 );
  45. } );
  46. it( 'should create spies for create() and destroy() if not defined', () => {
  47. expect( TestCreator1.prototype.create ).to.have.property( 'called', false, 'test1.create' );
  48. expect( TestCreator1.prototype.destroy ).to.have.property( 'called', false, 'test1.destroy' );
  49. expect( TestCreator2.prototype.create ).to.have.property( 'called', false, 'test2.create' );
  50. expect( TestCreator2.prototype.destroy ).to.have.property( 'called', false, 'test2.destroy' );
  51. // Not spies:
  52. expect( TestCreator3.prototype ).to.have.property( 'create', createFn3 );
  53. expect( TestCreator3.prototype ).to.have.property( 'destroy', destroyFn3 );
  54. } );
  55. } );
  56. describe( 'coreTestUtils.getIteratorCount()', () => {
  57. it( 'should returns number of editable items', () => {
  58. const count = coreTestUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] );
  59. expect( count ).to.equal( 5 );
  60. } );
  61. } );
  62. describe( 'coreTestUtils.createObserver()', () => {
  63. let observable, observable2, observer;
  64. beforeEach( () => {
  65. observer = coreTestUtils.createObserver();
  66. observable = new Model( { foo: 0, bar: 0 } );
  67. observable2 = new Model( { foo: 0, bar: 0 } );
  68. } );
  69. it( 'should create an observer', () => {
  70. function Emitter() {}
  71. Emitter.prototype = EmitterMixin;
  72. expect( observer ).to.be.instanceof( Emitter );
  73. expect( observer.observe ).is.a( 'function' );
  74. expect( observer.stopListening ).is.a( 'function' );
  75. } );
  76. describe( 'Observer', () => {
  77. /* global console:false */
  78. it( 'logs changes in the observable', () => {
  79. const spy = testUtils.sinon.stub( console, 'log' );
  80. observer.observe( 'Some observable', observable );
  81. observer.observe( 'Some observable 2', observable2 );
  82. observable.foo = 1;
  83. expect( spy.callCount ).to.equal( 1 );
  84. observable.foo = 2;
  85. observable2.bar = 3;
  86. expect( spy.callCount ).to.equal( 3 );
  87. } );
  88. it( 'stops listening when asked to do so', () => {
  89. const spy = testUtils.sinon.stub( console, 'log' );
  90. observer.observe( 'Some observable', observable );
  91. observable.foo = 1;
  92. expect( spy.callCount ).to.equal( 1 );
  93. observer.stopListening();
  94. observable.foo = 2;
  95. expect( spy.callCount ).to.equal( 1 );
  96. } );
  97. } );
  98. } );