Last active
September 18, 2015 11:09
-
-
Save PavelVanecek/b01ac50935f92a2e19bb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
suite 'Constructor binding', -> | |
ConstructorFn = null | |
boundFn = null | |
spy = null | |
teardown -> | |
# cleanup global state after any failed tests | |
delete global.instanceProperty | |
setup -> | |
ConstructorFn = (param) -> | |
this.instanceProperty = param | |
return | |
boundFn = ConstructorFn.bind null, 'foo' | |
console.log 'boundFn name', boundFn.name, ConstructorFn.name | |
suite 'with new', -> | |
result = null | |
setup -> | |
result = new boundFn() | |
test 'should return object', -> | |
assert.isObject result | |
test 'should produce correct instance', -> | |
assert.instanceOf result, ConstructorFn | |
test 'should not pollute global namespace', -> | |
assert.notProperty global, 'instanceProperty' | |
test 'should set correct instanceProperty', -> | |
assert.equal result.instanceProperty, 'foo' | |
suite 'without new', -> | |
result = null | |
setup -> | |
result = boundFn() | |
test 'should return undefined', -> | |
assert.isUndefined result | |
test 'should pollute global namespace', -> | |
assert.property global, 'instanceProperty' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example test suite confirms the behaviour documented on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind