utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * General test utils for CKEditor.
  7. */
  8. const utils = {
  9. sinon,
  10. /**
  11. * Creates Sinon sandbox in {@link utils#sinon} and plugs `afterEach()` callback which
  12. * restores all spies and stubs created in this sandbox.
  13. *
  14. * See https://github.com/ckeditor/ckeditor5-design/issues/72 and http://sinonjs.org/docs/#sinon-sandbox
  15. *
  16. * Usage:
  17. *
  18. * import testUtils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  19. *
  20. * describe( 'MyClass', () => {
  21. * // Create Sinon sandbox inside top-level describe block:
  22. * testUtils.createSinonSandbox();
  23. *
  24. * // Then inside tests you can use testUtils.sinon:
  25. * it( 'does something', () => {
  26. * testUtils.sinon.spy( obj, 'method' );
  27. * } );
  28. * }
  29. *
  30. * **Note**: Do not use `testUtils.createSinonSandbox()` outside `describe()` block as it will attach `afterEach()` calls
  31. * to all test - not only those in current file.
  32. */
  33. createSinonSandbox() {
  34. afterEach( () => {
  35. utils.sinon.restore();
  36. } );
  37. },
  38. /**
  39. * Executes specified assertions. It expects that at least one function will not throw an error.
  40. *
  41. * Some of the tests fail because different browsers renders selection differently when it comes to element boundaries.
  42. * Using this method we can check few scenarios.
  43. *
  44. * See https://github.com/ckeditor/ckeditor5-core/issues/107.
  45. *
  46. * Usage:
  47. *
  48. * it( 'test', () => {
  49. * // Test bootstrapping...
  50. *
  51. * const assertEdge = () => {
  52. * // expect();
  53. * };
  54. *
  55. * const assertAll = () => {
  56. * // expect();
  57. * };
  58. *
  59. * testUtils.checkAssertions( assertEdge, assertAll );
  60. * } );
  61. *
  62. * @param {...Function} assertions Functions that will be executed.
  63. */
  64. checkAssertions( ...assertions ) {
  65. const errors = [];
  66. for ( const assertFn of assertions ) {
  67. try {
  68. assertFn();
  69. return;
  70. } catch ( err ) {
  71. errors.push( err.message );
  72. }
  73. }
  74. throw new Error( errors.join( '\n\n' ) );
  75. },
  76. /**
  77. * Checks if given mixin i mixed to given class using {@link module:utils/mix mix} util.
  78. *
  79. * @param {Function} targetClass Class to check.
  80. * @param {Object} mixin Mixin to check.
  81. * @returns {Boolean} `True` when mixin is mixed to to target class, `false` otherwise.
  82. */
  83. isMixed( targetClass, mixin ) {
  84. let isValid = true;
  85. for ( const property in mixin ) {
  86. if ( mixin.hasOwnProperty( property ) ) {
  87. if ( targetClass.prototype[ property ] !== mixin[ property ] ) {
  88. isValid = false;
  89. }
  90. }
  91. }
  92. return isValid;
  93. }
  94. };
  95. export default utils;