Skip to content

Instantly share code, notes, and snippets.

@iammerrick
Last active January 11, 2019 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iammerrick/feb26be0a6554507fa02a8f6cb74d265 to your computer and use it in GitHub Desktop.
Save iammerrick/feb26be0a6554507fa02a8f6cb74d265 to your computer and use it in GitHub Desktop.
Determine if a user called "then" on registration promise.
const spyInstance = (on, replace, spy) => {
const _real = on[replace];
on[replace] = function(...args) {
const ret = _real.apply(this, args);
const newRet = spy(args, ret, this);
if (newRet) return newRet;
return ret;
};
};
const spy = (on, replace, spy) => {
const _real = on.prototype[replace];
on.prototype[replace] = function(...args) {
const ret = _real.apply(this, args);
const newRet = spy(args, ret, this);
if (newRet) return newRet;
return ret;
};
};
spyInstance(console, "log", ([maybeRegistration]) => {
if (maybeRegistration instanceof ServiceWorkerRegistration) {
console.log("User logged registration!");
}
});
spy(ServiceWorkerContainer, "register", (register, userReturned) => {
console.log("User called register!");
spy(Promise, "then", ([registration], returned, instance) => {
if (userReturned === instance) {
console.log("User called then on promise returned by register!");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment