8
0

utils.js 2.4 KB

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