-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fabien Motte
committed
Nov 15, 2017
1 parent
ba4bb24
commit bc9d8fb
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Bind methods on a given context | ||
* | ||
* @param {object} ctx Context | ||
* @param {array} methodNames Method names | ||
*/ | ||
export function scope (ctx, methodNames) { | ||
for (let i = 0, l = methodNames.length; i < l; i++) { | ||
ctx[methodNames[i]] = ctx[methodNames[i]].bind(ctx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { scope } from '../../src/common' | ||
|
||
describe('common.scope()', () => { | ||
it('Check if method is binded', () => { | ||
const o = { | ||
test () { return this } | ||
} | ||
|
||
const testCall = o.test() | ||
scope(o, ['test']) | ||
|
||
expect(testCall.hasOwnProperty('prototype')).toBeFalsy() | ||
}) | ||
|
||
it('Check if methods are binded', () => { | ||
const o = { | ||
test1 () { return this }, | ||
test2 () { return this } | ||
} | ||
|
||
const test1Call = o.test1() | ||
const test2Call = o.test2() | ||
scope(o, ['test1', 'test2']) | ||
|
||
expect(test1Call.hasOwnProperty('prototype')).toBeFalsy() | ||
expect(test2Call.hasOwnProperty('prototype')).toBeFalsy() | ||
}) | ||
}) |