Skip to content

Instantly share code, notes, and snippets.

@Colorfulstan
Created November 23, 2018 12:41
Show Gist options
  • Save Colorfulstan/2bc47d9813f1ab5773e86b7c3943f56a to your computer and use it in GitHub Desktop.
Save Colorfulstan/2bc47d9813f1ab5773e86b7c3943f56a to your computer and use it in GitHub Desktop.
Creating a mock for the overwolf object with function mocks
  1. open any overwolf window
  2. add stringify version that includes functions
function JSONstringifyWithFuncs(obj) {
  Object.prototype.toJSON = function() {
  var sobj = {}, i;
for (i in this)
if (this.hasOwnProperty(i))
sobj[i] = typeof this[i] == 'function' ?
this[i].toString() : this[i];

return sobj;
};
Array.prototype.toJSON = function() {
var sarr = [], i;
for (i = 0 ; i < this.length; i++)
sarr.push(typeof this[i] == 'function' ? this[i].toString() : this[i]);

return sarr;
};

var str = JSON.stringify(obj);

delete Object.prototype.toJSON;
delete Array.prototype.toJSON;

return str;
}
  1. run with overwolf object JSONstringifyWithFuncs(overwolf)
  2. copy output to JSON file (removing leading and trailing quote)
  3. run regExp replace /"function.*["|$]/g , replacing with your favorite spy/stub/mock base function ( e.g.sinon.stub() )
  4. adapt as needed

TODO: create configurable script that does this and enables to configure specific mock implementations for updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment