Skip to content

Commit

Permalink
Proviede possibility to bind also arguments to function not only cont…
Browse files Browse the repository at this point in the history
…ext with CKEDITOR.tools.bind
  • Loading branch information
Mateusz Samsel committed Feb 25, 2019
1 parent 7270eda commit 7383b2d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,14 @@
*
* @param {Function} func The function to be executed.
* @param {Object} obj The object to which the execution context will be bound.
* @param {*} [args] Other arguments passed after object, will be bind to executed function.
* @returns {Function} The function that can be used to execute the
* `func` function in the context of `obj`.
*/
bind: function( func, obj ) {
var args = Array.prototype.slice.call( arguments, 2 );
return function() {
return func.apply( obj, arguments );
return func.apply( obj, args.concat( Array.prototype.slice.call( arguments ) ) );
};
},

Expand Down
58 changes: 58 additions & 0 deletions tests/core/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,64 @@
CKEDITOR.tools.array.forEach( conversionArray, function( item ) {
assert.areSame( item.output, CKEDITOR.tools.convertToPx( item.input ), 'Value ' + item.input + ' should be converted to ' + item.output );
} );
},

'test bind without context and without arguments': function() {
var testSpy = sinon.spy(),
bindedFn = CKEDITOR.tools.bind( testSpy );

bindedFn( 'foo' );
assert.areSame( 1, testSpy.callCount );
assert.isTrue( testSpy.calledWithExactly( 'foo' ) );

bindedFn( 'bar' );
assert.areSame( 2, testSpy.callCount );
assert.isTrue( testSpy.calledWithExactly( 'bar' ) );
},

'text bind with context and without arguments': function() {
var testSpy = sinon.spy(),
testObj = {},
bindedFn = CKEDITOR.tools.bind( testSpy, testObj );

bindedFn( 'foo' );
assert.areSame( 1, testSpy.callCount );
assert.areSame( testObj, testSpy.getCall( 0 ).thisValue );
assert.isTrue( testSpy.calledWithExactly( 'foo' ) );

bindedFn( 'bar' );
assert.areSame( 2, testSpy.callCount );
assert.areSame( testObj, testSpy.getCall( 1 ).thisValue );
assert.isTrue( testSpy.calledWithExactly( 'bar' ) );
},

'test bind without context and with arguments': function() {
var testSpy = sinon.spy(),
bindedFn = CKEDITOR.tools.bind( testSpy, null, 'baz', 100 );

bindedFn( 'foo' );
assert.areSame( 1, testSpy.callCount );
assert.isTrue( testSpy.calledWithExactly( 'baz', 100, 'foo' ) );

bindedFn( 'bar' );
assert.areSame( 2, testSpy.callCount );
assert.isTrue( testSpy.calledWithExactly( 'baz', 100, 'bar' ) );
},

'text bind with context and with arguments': function() {
var testSpy = sinon.spy(),
testObj = {},
bindedFn = CKEDITOR.tools.bind( testSpy, testObj, 'baz', 100 );

bindedFn( 'foo' );
assert.areSame( 1, testSpy.callCount );
assert.areSame( testObj, testSpy.getCall( 0 ).thisValue );
assert.isTrue( testSpy.calledWithExactly( 'baz', 100, 'foo' ) );

bindedFn( 'bar' );
assert.areSame( 2, testSpy.callCount );
assert.areSame( testObj, testSpy.getCall( 0 ).thisValue );
assert.isTrue( testSpy.calledWithExactly( 'baz', 100, 'bar' ) );
}
} );
} )();

0 comments on commit 7383b2d

Please sign in to comment.