8
0

utils.js 2.7 KB

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