Last active
September 27, 2017 09:54
-
-
Save AaronHarris/9dfea333975351467cb9dde4b3e43248 to your computer and use it in GitHub Desktop.
JavaScript Stump - the Ultimate Singleton: This describes a function object that no matter whether you call it, instantiate it, or access or modify a property, it always returns itself.
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
var proxy, handler; | |
handler = { | |
has(/*target, prop*/) { | |
return false; | |
}, | |
get(target, prop, receiver) { | |
if (prop in target) return target[prop]; | |
return receiver; | |
}, | |
set(/*target, prop, value, receiver*/) { | |
return true; | |
}, | |
defineProperty(/*target, prop, descriptor*/) { | |
return true; | |
}, | |
apply(/*target, thisArg, argumentsList*/) { | |
return proxy; | |
}, | |
construct(/*target, argumentsList, newTarget*/) { | |
return proxy; | |
}, | |
deleteProperty(/*target, prop*/) { | |
return true; | |
} | |
}; | |
proxy = new Proxy(function() {}, handler); | |
module.exports = proxy; |
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
import test from 'ava'; | |
import dummy from './dummy'; | |
test('All property accesses return a value', t => { | |
t.truthy(dummy); | |
t.truthy(dummy.foo); | |
t.truthy(dummy.foo.bar); | |
t.truthy(dummy.foo.bar()); | |
t.truthy(dummy.foo()); | |
t.truthy(dummy.foo().bar); | |
t.truthy(dummy.foo().bar()); | |
t.truthy(dummy.foo().bar().baz); | |
t.truthy(dummy.foo().bar().baz()); | |
}); | |
test('All function calls return a value', t => { | |
t.truthy(dummy()); | |
t.truthy(dummy().foo); | |
t.truthy(dummy().foo()); | |
t.truthy(dummy().foo.bar()); | |
}); | |
test('All instantiations return a value', t => { | |
t.truthy(new dummy); | |
t.truthy(new dummy()); | |
t.truthy(new dummy.foo); | |
t.truthy(new dummy.foo()); | |
}); | |
test('Properties cannot be set on Dummy', t => { | |
let before = dummy(); | |
dummy.prop = 5555; | |
t.pass('Would need to verify nothing is defined on the target object'); | |
}); | |
test('`in` operator always returns false', t => { | |
t.false('prop' in dummy); | |
t.false('length' in dummy); | |
t.false('name' in dummy); | |
t.false('prototype' in dummy); | |
}); | |
test('Properties cannot be deleted from dummy', t => { | |
let before = dummy.prop; | |
t.true(delete dummy.prop); | |
t.is(dummy.prop, before, 'Expected prop to be same before and after deleting'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment