Procházet zdrojové kódy

Made decorate() throw when a method does not exist.

Piotrek Koszuliński před 8 roky
rodič
revize
bef68cefa9

+ 14 - 0
packages/ckeditor5-utils/src/observablemixin.js

@@ -309,6 +309,20 @@ const ObservableMixin = {
 		methodNames.forEach( methodName => {
 			const originalMethod = this[ methodName ];
 
+			if ( !originalMethod ) {
+				/**
+				 * Cannot decorate an undefined method.
+				 *
+				 * @error observablemixin-cannot-decorate-undefined
+				 * @param {Object} object The object on which you try to decorate a method.
+				 * @param {String} methodName Name of the method which does not exist.
+				 */
+				throw new CKEditorError(
+					'observablemixin-cannot-decorate-undefined: Cannot decorate an undefined method.',
+					{ object: this, methodName }
+				);
+			}
+
 			this.on( methodName, ( evt, args ) => {
 				evt.return = originalMethod.apply( this, args );
 			} );

+ 10 - 0
packages/ckeditor5-utils/tests/observablemixin.js

@@ -908,5 +908,15 @@ describe( 'Observable', () => {
 
 			foo.method();
 		} );
+
+		it( 'throws when trying to decorate non existing method', () => {
+			class Foo extends Observable {}
+
+			const foo = new Foo();
+
+			expect( () => {
+				foo.decorate( 'method' );
+			} ).to.throw( CKEditorError, /^observablemixin-cannot-decorate-undefined:/ );
+		} );
 	} );
 } );