utils.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import testUtils from '../_utils/utils';
  6. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  7. describe( 'utils', () => {
  8. testUtils.createSinonSandbox();
  9. describe( 'checkAssertions()', () => {
  10. it( 'does not throw an error if at least one assertion passed', () => {
  11. const assertionRed = testUtils.sinon.stub().callsFake( () => {
  12. expect( 1 ).to.equal( 2 );
  13. } );
  14. const assertionGreen = testUtils.sinon.stub().callsFake( () => {
  15. expect( 1 ).to.equal( 1 );
  16. } );
  17. expect( () => {
  18. testUtils.checkAssertions( assertionRed, assertionGreen );
  19. } ).to.not.throw();
  20. } );
  21. it( 'throws all errors if any assertion did not pass', () => {
  22. const assertionRed = testUtils.sinon.stub().callsFake( () => {
  23. expect( 1 ).to.equal( 2 );
  24. } );
  25. const assertionGreen = testUtils.sinon.stub().callsFake( () => {
  26. expect( 2 ).to.equal( 1 );
  27. } );
  28. expect( () => {
  29. testUtils.checkAssertions( assertionRed, assertionGreen );
  30. } ).to.throw( Error, 'expected 1 to equal 2\n\nexpected 2 to equal 1' );
  31. } );
  32. it( 'does not execute all assertions if the first one passed', () => {
  33. const assertionRed = testUtils.sinon.stub().callsFake( () => {
  34. expect( 1 ).to.equal( 1 );
  35. } );
  36. const assertionGreen = testUtils.sinon.stub();
  37. testUtils.checkAssertions( assertionRed, assertionGreen );
  38. expect( assertionGreen.called ).to.equal( false );
  39. } );
  40. } );
  41. describe( 'isMixed()', () => {
  42. let mixin, CustomClass;
  43. beforeEach( () => {
  44. CustomClass = class {};
  45. mixin = {
  46. foo() {
  47. return 'bar';
  48. }
  49. };
  50. } );
  51. it( 'should return true when given mixin is mixed to target class', () => {
  52. mix( CustomClass, mixin );
  53. expect( testUtils.isMixed( CustomClass, mixin ) ).to.true;
  54. } );
  55. it( 'should return false when given mixin is not mixed to target class', () => {
  56. expect( testUtils.isMixed( CustomClass, mixin ) ).to.false;
  57. } );
  58. it( 'should return false when class has mixin like interface', () => {
  59. CustomClass = class {
  60. foo() {
  61. return 'biz';
  62. }
  63. };
  64. expect( testUtils.isMixed( CustomClass, mixin ) ).to.false;
  65. } );
  66. } );
  67. } );