utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 moduleTestUtils from '/tests/_utils/module.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 = moduleTestUtils.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. expect( TestCreator3.prototype ).to.have.property( 'create', createFn3 );
  46. expect( TestCreator3.prototype ).to.have.property( 'destroy', destroyFn3 );
  47. } );
  48. } );
  49. describe( 'coreTestUtils.getIteratorCount()', () => {
  50. it( 'should returns number of editable items', () => {
  51. const count = coreTestUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] );
  52. expect( count ).to.equal( 5 );
  53. } );
  54. } );
  55. describe( 'coreTestUtils.createObserver()', () => {
  56. let observable, observable2, observer;
  57. beforeEach( () => {
  58. observer = coreTestUtils.createObserver();
  59. observable = new Model( { foo: 0, bar: 0 } );
  60. observable2 = new Model( { foo: 0, bar: 0 } );
  61. } );
  62. it( 'should create an observer', () => {
  63. function Emitter() {}
  64. Emitter.prototype = EmitterMixin;
  65. expect( observer ).to.be.instanceof( Emitter );
  66. expect( observer.observe ).is.a( 'function' );
  67. expect( observer.stopListening ).is.a( 'function' );
  68. } );
  69. describe( 'Observer', () => {
  70. /* global console:false */
  71. it( 'logs changes in the observable', () => {
  72. const spy = testUtils.sinon.stub( console, 'log' );
  73. observer.observe( 'Some observable', observable );
  74. observer.observe( 'Some observable 2', observable2 );
  75. observable.foo = 1;
  76. expect( spy.callCount ).to.equal( 1 );
  77. observable.foo = 2;
  78. observable2.bar = 3;
  79. expect( spy.callCount ).to.equal( 3 );
  80. } );
  81. it( 'stops listening when asked to do so', () => {
  82. const spy = testUtils.sinon.stub( console, 'log' );
  83. observer.observe( 'Some observable', observable );
  84. observable.foo = 1;
  85. expect( spy.callCount ).to.equal( 1 );
  86. observer.stopListening();
  87. observable.foo = 2;
  88. expect( spy.callCount ).to.equal( 1 );
  89. } );
  90. } );
  91. } );