Created
August 19, 2019 16:28
-
-
Save copperwall/31b3f2b9c9341186133721de76dc7dd2 to your computer and use it in GitHub Desktop.
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
const test = { | |
spy: function(fn) { | |
const calls = []; | |
return new Proxy(stuff, { | |
apply(target, thisArg, args) { | |
const result = target.apply(thisArg, args); | |
calls.push([args, result]); | |
}, | |
get(target, p){ | |
if (p === 'calls') { | |
return calls; | |
} | |
return target[p]; | |
} | |
}); | |
} | |
}; | |
function stuff(arg1, arg2) { | |
return `${arg1} ${arg2} cool`; | |
} | |
function doSomeStuff(fn) { | |
return stuff(1, 2); | |
} | |
const spyStuff = test.spy(stuff); | |
spyStuff("hello", "cool"); | |
doSomeStuff(spyStuff); | |
const calls = spyStuff.calls; | |
assert(calls[0][0][0] === "hello"); | |
assert(calls[1][1] === "hello cool cool"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment