UNPKG

siesta-lite

Version:

Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers

228 lines (193 loc) 10.3 kB
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The source code</title> <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="../resources/prettify/prettify.js"></script> <style type="text/css"> .highlight { display: block; background-color: #ddd; } </style> <script type="text/javascript"> function highlight() { document.getElementById(location.hash.replace(/#/, "")).className = "highlight"; } </script> </head> <body onload="prettyPrint(); highlight();"> <pre class="prettyprint lang-js">/* Siesta 5.6.1 Copyright(c) 2009-2022 Bryntum AB https://bryntum.com/contact https://bryntum.com/products/siesta/license */ <span id='Siesta-Test-Function'>/** </span>@class Siesta.Test.Function This is a mixin, with helper methods for testing functionality relating to Functions (such as spies). This mixin is consumed by {@link Siesta.Test} */ Role(&#39;Siesta.Test.Function&#39;, { methods : { <span id='Siesta-Test-Function-method-isCalled'> /** </span> * This assertion passes if the function is called at least one time during the test life span. * * @param {Function/String} fn The function itself or the name of the function on the host object (2nd argument) * @param {Object} host The &quot;owner&quot; of the method * @param {String} [desc] The description of the assertion. */ isCalled : function(fn, obj, desc) { this.isCalledNTimes(fn, obj, 1, desc, true); }, <span id='Siesta-Test-Function-method-isCalledOnce'> /** </span> * This assertion passes if the function is called exactly one time during the test life span. * * @param {Function/String} fn The function itself or the name of the function on the host object (2nd argument) * @param {Object} host The &quot;owner&quot; of the method * @param {String} [desc] The description of the assertion. */ isCalledOnce : function(fn, obj, desc) { this.isCalledNTimes(fn, obj, 1, desc, false); }, <span id='Siesta-Test-Function-method-isCalledNTimes'> /** </span> * This assertion passes if the function is called exactly (n) times during the test life span. * * @param {Function/String} fn The function itself or the name of the function on the host object (2nd argument) * @param {Object} host The &quot;owner&quot; of the method * @param {Number} n The expected number of calls * @param {String} [desc] The description of the assertion. */ isCalledNTimes : function(fn, obj, n, desc, isGreaterEqual) { var me = this, prop = typeof fn === &quot;string&quot; ? fn : me.getPropertyName(obj, fn); var counter = 0; var original = obj[ prop ] obj[ prop ] = function () { counter++; return original.apply(this, arguments) } desc = desc ? (desc + &#39; &#39;) : &#39;&#39;; this.on(&#39;beforetestfinalizeearly&#39;, function () { var R = Siesta.Resource(&#39;Siesta.Test.Function&#39;); if (counter === n || (isGreaterEqual &amp;&amp; counter &gt; n)) { me.pass(desc || (prop + &#39; &#39; + R.get(&#39;methodCalledExactly&#39;).replace(&#39;{n}&#39;, n))); } else { me.fail(desc || prop, { assertionName : &#39;isCalledNTimes &#39; + prop, got : counter, need : n, needDesc : R.get(&quot;Need&quot;) + &quot; &quot; + (isGreaterEqual ? R.get(&#39;atLeast&#39;) : R.get(&#39;exactly&#39;)) + &quot; &quot; }); } obj[ prop ] = original }); }, <span id='Siesta-Test-Function-method-isntCalled'> /** </span> * This assertion passes if the function is not called during the test life span. * * @param {Function/String} fn The function itself or the name of the function on the host object (2nd argument) * @param {Object} host The &quot;owner&quot; of the method * @param {String} [desc] The description of the assertion. */ isntCalled : function(fn, obj, desc) { this.isCalledNTimes(fn, obj, 0, desc); }, getPropertyName : function(host, obj) { for (var o in host) { if (host[o] === obj) return o; } }, <span id='Siesta-Test-Function-method-methodIsCalledNTimes'> /** </span> * This assertion passes when the supplied class method is called exactly (n) times during the test life span. * Under &quot;class method&quot; here we mean the function in the prototype. Note, that this assertion counts calls to the method in *any* class instance. * * The `className` parameter can be supplied as a class constructor function or as a string, representing the class * name. In the latter case the `class` will be eval&#39;ed to get a reference to the class constructor. * * For example: StartTest(function (t) { function machine(type, version) { this.machineInfo = { type : type, version : version }; }; machine.prototype.update = function (type, version) { this.setVersion(type); this.setType(version); }; machine.prototype.setVersion = function (data) { this.machineInfo.version = data; }; machine.prototype.setType = function (data) { this.machineInfo.type = data; }; t.methodIsCalled(&quot;setVersion&quot;, machine, &quot;Checking if method &#39;setVersion&#39; has been called&quot;); t.methodIsCalled(&quot;setType&quot;, machine, &quot;Checking if method &#39;setType&#39; has been called&quot;); var m = new machine(&#39;rover&#39;, &#39;0.1.2&#39;); m.update(&#39;3.2.1&#39;, &#39;New Rover&#39;); }); * * This assertion is useful when testing for example an Ext JS class where event listeners are added during * class instantiation time, which means you need to observe the prototype method before instantiation. * * @param {Function/String} fn The function itself or the name of the method on the class (2nd argument) * @param {Function/String} className The constructor function or the name of the class that contains the method * @param {Number} n The expected number of calls * @param {String} [desc] The description of the assertion */ methodIsCalledNTimes: function(fn, className, n, desc, isGreaterEqual){ var me = this, counter = 0; var R = Siesta.Resource(&#39;Siesta.Test.Function&#39;); desc = desc ? (desc + &#39; &#39;) : &#39;&#39;; try { if (me.typeOf(className) == &#39;String&#39;) className = me.global.eval(className) } catch (e) { me.fail(desc, { assertionName : &#39;isMethodCalled&#39;, annotation : R.get(&#39;exceptionEvalutingClass&#39;).replace(&#39;{e}&#39;, e) + &quot;[&quot; + className + &quot;]&quot; }) return } var prototype = className.prototype; var prop = typeof fn === &quot;string&quot; ? fn : me.getPropertyName(prototype, fn); me.on(&#39;beforetestfinalizeearly&#39;, function () { if (counter === n || (isGreaterEqual &amp;&amp; counter &gt; n)) { me.pass(desc || (prop + &#39; &#39; + R.get(&#39;methodCalledExactly&#39;).replace(&#39;{n}&#39;, n))); } else { me.fail(desc || prop, { assertionName : &#39;methodIsCalledNTimes &#39; + prop, got : counter, need : n , needDesc : R.get(&quot;Need&quot;) + &quot; &quot; + (isGreaterEqual ? R.get(&#39;atLeast&#39;) : R.get(&#39;exactly&#39;)) + &quot; &quot; }); } }); fn = prototype[ prop ]; prototype[ prop ] = function () { counter++; return fn.apply(this, arguments); }; }, <span id='Siesta-Test-Function-method-methodIsCalled'> /** </span> * This assertion passes if the class method is called at least one time during the test life span. * * See {@link #methodIsCalledNTimes} for more details. * * @param {Function/String} fn The function itself or the name of the method on the class (2nd argument) * @param {Function/String} className The class constructor function or name of the class that contains the method * @param {String} [desc] The description of the assertion. */ methodIsCalled : function(fn, className, desc) { this.methodIsCalledNTimes(fn, className, 1, desc, true); }, <span id='Siesta-Test-Function-method-methodIsntCalled'> /** </span> * This assertion passes if the class method is not called during the test life span. * * See {@link #methodIsCalledNTimes} for more details. * * @param {Function/String} fn The function itself or the name of the method on the class (2nd argument) * @param {Function/String} className The class constructor function or name of the class that contains the method * @param {String} [desc] The description of the assertion. */ methodIsntCalled : function(fn, className, desc) { this.methodIsCalledNTimes(fn, className, 0, desc); } } }); </pre> </body> </html>