Last active
January 11, 2019 06:24
-
-
Save iammerrick/feb26be0a6554507fa02a8f6cb74d265 to your computer and use it in GitHub Desktop.
Determine if a user called "then" on registration promise.
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
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