Bladeren bron

Added isMixied util for checking if mixin is mixed to class.

Oskar Wróbel 8 jaren geleden
bovenliggende
commit
64e56023bf
2 gewijzigde bestanden met toevoegingen van 55 en 0 verwijderingen
  1. 34 0
      packages/ckeditor5-core/tests/_utils-tests/utils.js
  2. 21 0
      packages/ckeditor5-core/tests/_utils/utils.js

+ 34 - 0
packages/ckeditor5-core/tests/_utils-tests/utils.js

@@ -4,6 +4,7 @@
  */
 
 import testUtils from '../_utils/utils';
+import mix from '@ckeditor/ckeditor5-utils/src/mix';
 
 testUtils.createSinonSandbox();
 
@@ -45,4 +46,37 @@ describe( 'utils', () => {
 			expect( assertionGreen.called ).to.equal( false );
 		} );
 	} );
+
+	describe( 'isMixed()', () => {
+		let mixin, CustomClass;
+
+		beforeEach( () => {
+			CustomClass = class {};
+			mixin = {
+				foo() {
+					return 'bar';
+				}
+			};
+		} );
+
+		it( 'should return true when given mixin is mixed to target class', () => {
+			mix( CustomClass, mixin );
+
+			expect( testUtils.isMixed( CustomClass, mixin ) ).to.true;
+		} );
+
+		it( 'should return false when given mixin is not mixed to target class', () => {
+			expect( testUtils.isMixed( CustomClass, mixin ) ).to.false;
+		} );
+
+		it( 'should return false when class has mixin like interface', () => {
+			CustomClass = class {
+				foo() {
+					return 'biz';
+				}
+			};
+
+			expect( testUtils.isMixed( CustomClass, mixin ) ).to.false;
+		} );
+	} );
 } );

+ 21 - 0
packages/ckeditor5-core/tests/_utils/utils.js

@@ -73,6 +73,27 @@ const utils = {
 		}
 
 		throw new Error( errors.join( '\n\n' ) );
+	},
+
+	/**
+	 * Checks if given mixin i mixed to given class using {@link module:utils/mix mix} util.
+	 *
+	 * @param {Function} targetClass Class to check.
+	 * @param {Object} mixin Mixin to check.
+	 * @returns {Boolean} `True` when mixin is mixed to to target class, `false` otherwise.
+	 */
+	isMixed( targetClass, mixin ) {
+		let isValid = true;
+
+		for ( const property in mixin ) {
+			if ( mixin.hasOwnProperty( property ) ) {
+				if ( targetClass.prototype[ property ] !== mixin[ property ] ) {
+					isValid = false;
+				}
+			}
+		}
+
+		return isValid;
 	}
 };