You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					36 lines
				
				725 B
			
		
		
			
		
	
	
					36 lines
				
				725 B
			| 
											2 years ago
										 | 'use strict' | ||
|  | 
 | ||
|  | module.exports = function stubs(obj, method, cfg, stub) { | ||
|  |   if (!obj || !method || !obj[method]) | ||
|  |     throw new Error('You must provide an object and a key for an existing method') | ||
|  | 
 | ||
|  |   if (!stub) { | ||
|  |     stub = cfg | ||
|  |     cfg = {} | ||
|  |   } | ||
|  | 
 | ||
|  |   stub = stub || function() {} | ||
|  | 
 | ||
|  |   cfg.callthrough = cfg.callthrough || false | ||
|  |   cfg.calls = cfg.calls || 0 | ||
|  | 
 | ||
|  |   var norevert = cfg.calls === 0 | ||
|  | 
 | ||
|  |   var cached = obj[method].bind(obj) | ||
|  | 
 | ||
|  |   obj[method] = function() { | ||
|  |     var args = [].slice.call(arguments) | ||
|  |     var returnVal | ||
|  | 
 | ||
|  |     if (cfg.callthrough) | ||
|  |       returnVal = cached.apply(obj, args) | ||
|  | 
 | ||
|  |     returnVal = stub.apply(obj, args) || returnVal | ||
|  | 
 | ||
|  |     if (!norevert && --cfg.calls === 0) | ||
|  |       obj[method] = cached | ||
|  | 
 | ||
|  |     return returnVal | ||
|  |   } | ||
|  | } |