utils.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 {Array.<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. export default utils;