-
Notifications
You must be signed in to change notification settings - Fork 4
v4 planning
Chuanqi Sun edited this page Mar 19, 2019
·
1 revision
- infinite de-reference:
mockObject.prop.func().prop.func().prop.func().prop
still returns a mock object. - decouple from jasmine: replace spy with a built-in journal that is test framework agnostic
- ergonomic interface:
- consumer interface is identical to original object that is being mocked.
- admin interface is a one-time entry, e.g.
mockObject._admin.prop1.func().prop2 = 42
would result inmockOjbect.prop1.func().prop2
to actually return42
, but also leaves a journal on all theget
andapply
actions along the chain.
const stubObject = Object.create(null);
const handler = {
get: function(obj, prop) {
console.log('get');
if (!(prop in stubObject)) {
stubObject[prop] = new Proxy(new Function(), handler);
}
return stubObject[prop];
},
apply: function(target, thisArg, argumentList) {
console.log('apply');
return new Proxy(Object.create(null), handler);
},
set: function(obj, prop, value) {
console.log('set');
stubObject[prop] = value;
}
};
var p = new Proxy({}, handler);