github.com/outbrain/consul@v1.4.5/ui-v2/tests/helpers/stub-super.js (about) 1 /** 2 * super stubber 3 * Ember's `_super` functionality is a little challenging to stub. 4 * The following will essentially let you stub `_super`, letting 5 * you test single units of code that use `_super` 6 * The return value is a function with the same signature as 7 * traditional `test` or `it` test functions. Any test code 8 * used within the cb 'sandbox' will use the stubbed `_super`. 9 * It's done this way to attempt to make it easy to reuse for various tests 10 * using the same stub in a recognisable way. 11 * 12 * @param {object} obj - The instance with the that `_super` belongs to 13 * @param {object} [stub=function(){return arguments}] - 14 * The stub to use in place of `_super`, if not specified `_super` will 15 * simply return the `arguments` passed to it 16 * 17 * @returns {function} 18 */ 19 export default function(obj, stub) { 20 const _super = 21 typeof stub === 'undefined' 22 ? function() { 23 return arguments; 24 } 25 : stub; 26 /** 27 * @param {string} message - Message to accompany the test concept (currently unused) 28 * @param {function} cb - Callback that performs the test, will use the stubbed `_super` 29 * @returns The result of `cb`, and therefore maintain the same API 30 */ 31 return function(message, _cb) { 32 const cb = typeof message === 'function' ? message : _cb; 33 34 let orig = obj._super; 35 Object.defineProperty(Object.getPrototypeOf(obj), '_super', { 36 set: function() {}, 37 get: function() { 38 return _super; 39 }, 40 }); 41 // TODO: try/catch this? 42 const actual = cb(); 43 Object.defineProperty(Object.getPrototypeOf(obj), '_super', { 44 set: function(val) { 45 orig = val; 46 }, 47 get: function() { 48 return orig; 49 }, 50 }); 51 return actual; 52 }; 53 }