UNPKG

siesta-lite

Version:

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

597 lines (502 loc) 27.6 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-ExtJS-Component'>/** </span>@class Siesta.Test.ExtJS.Component This is a mixin, with helper methods for testing functionality relating to Ext.Component. This mixin is being consumed by {@link Siesta.Test.ExtJS}. */ Role(&#39;Siesta.Test.ExtJS.Component&#39;, { requires: [&#39;waitFor&#39;], methods: { componentIsHidden : function (comp) { var el = this.compToEl(comp); if (!el) return true return (comp.isHidden &amp;&amp; comp.isHidden() || comp.isVisible &amp;&amp; !comp.isVisible()) || !this.isElementVisible(el); }, <span id='Siesta-Test-ExtJS-Component-method-waitForComponentVisible'> /** </span> * Waits until the main element of the passed component is the &#39;top&#39; element in the DOM. The callback will receive the passed component instance. * * @param {Ext.Component/String} component An Ext.Component instance or a ComponentQuery string. In the latter case, * this method will also wait until the component query find some component (meaning the component does not have to * be already created when waiting starts) * @param {Function} callback The callback to call after the component becomes visible * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForComponentVisible: function (component, callback, scope, timeout) { var R = Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;); var me = this; if (this.typeOf(component) != &#39;String&#39; &amp;&amp; !this.isExtJSComponentQueryTarget(component)) { throw R.get(&#39;badInputText&#39;) + &#39;: &#39; + component; } return this.waitFor({ method : function () { var comp = me.normalizeComponent(component, true) if (!comp) return false var el = me.compToEl(comp); return el &amp;&amp; me.elementIsTop(el, true) &amp;&amp; comp; }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForComponentVisible&#39;, description : &#39; &#39; + R.get(&#39;component&#39;) + &#39; &quot;&#39; + (me.typeOf(component) == &#39;String&#39; ? component : component.id) + &#39;&quot; &#39; + R.get(&#39;toBeVisible&#39;) }); }, <span id='Siesta-Test-ExtJS-Component-method-waitForComponentNotVisible'> /** </span> * Waits until the main element of the passed component is not visible. The callback will receive the passed component instance. * * @param {Ext.Component/String} component An Ext.Component instance or a ComponentQuery string. In the latter case, * this method will also wait until the component query find some component (meaning the component does not have to * be already created when waiting starts) * @param {Function} callback The callback to call after the component becomes not visible * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForComponentNotVisible: function (component, callback, scope, timeout) { var R = Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;); var me = this; if (this.typeOf(component) != &#39;String&#39; &amp;&amp; !this.isExtJSComponentQueryTarget(component)) { throw R.get(&#39;badInputText&#39;) + &#39;: &#39; + component; } return this.waitFor({ method : function () { var comp = me.normalizeComponent(component, true) if (!comp) return false return me.componentIsHidden(comp) &amp;&amp; comp }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForComponentNotVisible&#39;, description : &#39; &#39; + R.get(&#39;component&#39;) + &#39; &quot;&#39; + (me.typeOf(component) == &#39;String&#39; ? component : component.id) + &#39;&quot; &#39; + R.get(&#39;toNotBeVisible&#39;) }); }, <span id='Siesta-Test-ExtJS-Component-method-waitForComponentQuery'> /** </span> * Waits until Ext.ComponentQuery detects some results from the passed query parameter. The callback will receive the result of the query. * * The &quot;root&quot; argument of this method can be omitted. * * @param {String} selector The component query phrase * @param {Ext.Container} root The container to start a component query from. Optional * @param {Function} callback The callback to call after the xtype has been found * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForComponentQuery: function (selector, root, callback, scope, timeout) { // no `root` supplied if (this.typeOf(root) == &#39;Function&#39;) { timeout = scope scope = callback callback = root root = this.getExt() &amp;&amp; this.getExt().ComponentQuery } return this.waitFor({ method : function () { var result = (root &amp;&amp; root.query(selector) || this.getExt() &amp;&amp; this.getExt().ComponentQuery.query(selector)); return result &amp;&amp; result.length &gt; 0 ? result : false; }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForComponentQuery&#39;, description : &#39; &#39; + Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;, &#39;componentQuery&#39;) + &#39; &quot;&#39; + selector + &#39;&quot;&#39; }); }, <span id='Siesta-Test-ExtJS-Component-method-waitForCompositeQuery'> /** </span> * Waits until {@link Siesta.Test.ExtJSCore#compositeQuery} detects some results from the passed query parameter. The callback will receive the result of the query. * * The &quot;root&quot; argument of this method can be omitted. * * @param {String} query The composite query phrase * @param {Ext.Container} root The container to start a component query from. Optional * @param {Function} callback The callback * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForCompositeQuery: function (query, root, callback, scope, timeout) { // no `root` supplied if (this.typeOf(root) == &#39;Function&#39;) { timeout = scope scope = callback callback = root root = this.getExt().ComponentQuery } var me = this; return me.waitFor({ method : function () { var result = me.compositeQuery(query, root, true); return result.length &gt; 0 ? result : false; }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForCompositeQuery&#39;, description : &#39; &#39; + Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;, &#39;compositeQuery&#39;) + &#39; &quot;&#39; + query + &#39;&quot;&#39; }); }, <span id='Siesta-Test-ExtJS-Component-method-waitForCompositeQueryNotFound'> /** </span> * Waits until {@link Siesta.Test.ExtJSCore#compositeQuery} does not detects any results from the passed query parameter. * * The &quot;root&quot; argument of this method can be omitted. * * @param {String} query The composite query phrase * @param {Ext.Container} root The container to start a component query from. Optional * @param {Function} callback The callback * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForCompositeQueryNotFound: function (query, root, callback, scope, timeout) { // no `root` supplied if (this.typeOf(root) == &#39;Function&#39;) { timeout = scope scope = callback callback = root root = this.getExt().ComponentQuery } var me = this; return me.waitFor({ method : function () { var result = me.compositeQuery(query, root, true); return result.length &gt; 0 ? false : true; }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForCompositeQueryNotFound&#39;, description : &#39; &#39; + Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;, &#39;compositeQuery&#39;) + &#39; &quot;&#39; + query + &#39;&quot; &#39; + Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;, &#39;toReturnEmptyArray&#39;) }); }, <span id='Siesta-Test-ExtJS-Component-method-waitForCQ'> /** </span> * Shorthand alias for {@link #waitForComponentQuery} * * @param {String} query The component query phrase * @param {Ext.Container} root The container to start a component query from * @param {Function} callback The callback to call after the xtype has been found * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForCQ: function () { return this.waitForComponentQuery.apply(this, arguments); }, <span id='Siesta-Test-ExtJS-Component-method-waitForCQNotFound'> /** </span> * Alias for {@link #waitForComponentQueryNotFound} * * @param {String} query * @param {Function} callback * @param {Object} scope * @param {Number} timeout */ waitForCQNotFound: function () { return this.waitForComponentQueryNotFound.apply(this, arguments); }, <span id='Siesta-Test-ExtJS-Component-method-waitForComponentQueryNotFound'> /** </span> * Waits until Ext.ComponentQuery from the passed query parameter is no longer found, and then calls the callback supplied. * * The &quot;root&quot; argument of this method can be omitted. * * @param {String} query The component query selector * @param {Ext.Container} root The container to start a component query from. Optional * @param {Function} callback The callback to call after the xtype has been found * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForComponentQueryNotFound: function (query, root, callback, scope, timeout) { var R = Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;); // no `root` supplied if (this.typeOf(root) == &#39;Function&#39;) { timeout = scope scope = callback callback = root root = this.getExt().ComponentQuery } return this.waitFor({ method : function () { var result = root.query(query); return result.length === 0 &amp;&amp; result; }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForComponentQueryNotFound&#39;, description : R.get(&#39;componentQuery&#39;) + &#39;: &#39; + query + &#39; &#39; + R.get(&#39;toReturnEmpty&#39;) }); }, <span id='Siesta-Test-ExtJS-Component-method-waitForCQVisible'> /** </span> * Alias for {@link #waitForComponentQueryVisible} * * @param {String} query * @param {Function} callback * @param {Object} scope * @param {Number} timeout */ waitForCQVisible: function () { return this.waitForComponentQueryVisible.apply(this, arguments); }, <span id='Siesta-Test-ExtJS-Component-method-waitForCQNotVisible'> /** </span> * Alias for {@link #waitForComponentQueryNotVisible} * * @param {String} query * @param {Function} callback * @param {Object} scope * @param {Number} timeout */ waitForCQNotVisible: function () { return this.waitForComponentQueryNotVisible.apply(this, arguments); }, <span id='Siesta-Test-ExtJS-Component-method-waitForComponentQueryVisible'> /** </span> * Waits until all results of the `Ext.ComponentQuery` are detected and visible. * * The &quot;root&quot; argument of this method can be omitted. * * @param {String} query The component query selector * @param {Ext.Container} root The container to start a component query from. Optional * @param {Function} callback The callback to call after the waiting has been completed * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForComponentQueryVisible: function (query, root, callback, scope, timeout) { var me = this, R = Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;), Ext = me.getExt(); // no `root` supplied if (this.typeOf(root) == &#39;Function&#39;) { timeout = scope scope = callback callback = root root = Ext.ComponentQuery } var firstNonVisibleId var resultsLen return this.waitFor({ method : function () { firstNonVisibleId = null var result = root.query(query), allVisible = true resultsLen = result.length if (resultsLen &gt; 0) { Joose.A.each(result, function (c) { if (!c.rendered || !me.isElementVisible(c)) { allVisible = false firstNonVisibleId = c.id return false } }) return allVisible &amp;&amp; result } else { return false } }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForComponentQueryVisible&#39;, description : &#39; &#39; + R.get(&#39;componentQuery&#39;) + &#39;: &#39; + query + &#39; to return a non-empty set of visible components&#39;, annotation : function () { // empty resultset if (resultsLen === 0) return &quot;No matching components&quot; // success - return nothing to not pollute the output with extra details if (resultsLen &gt; 0 &amp;&amp; firstNonVisibleId == null) return &quot;&quot; // non-empty resultset with some components hidden if (resultsLen &gt; 0 &amp;&amp; firstNonVisibleId != null) return &quot;The matching component [id=&quot; + firstNonVisibleId + &quot;] is not visible&quot; } }) }, <span id='Siesta-Test-ExtJS-Component-method-waitForComponentQueryNotVisible'> /** </span> * Waits until the result of the `Ext.ComponentQuery` is either empty, or the found component(s) is hidden. * * The &quot;root&quot; argument of this method can be omitted. * * @param {String} query The component query selector * @param {Ext.Container} root The container to start a component query from. Optional * @param {Function} callback The callback to call after the xtype has been found * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForComponentQueryNotVisible: function (query, root, callback, scope, timeout) { var me = this, R = Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;), Ext = me.getExt(); // no `root` supplied if (this.typeOf(root) == &#39;Function&#39;) { timeout = scope scope = callback callback = root root = Ext.ComponentQuery } var firstVisibleId var resultsLen return this.waitFor({ method : function () { firstVisibleId = null var result = root.query(query), allHidden = true; resultsLen = result.length if (resultsLen &gt; 0) { Joose.A.each(result, function (comp) { if (!me.componentIsHidden(comp)) { firstVisibleId = comp.id allHidden = false; return false; } }); return allHidden &amp;&amp; result; } else { return true; } }, callback : callback, scope : scope, timeout : timeout, assertionName : &#39;waitForComponentQueryVisible&#39;, description : &#39; &#39; + R.get(&#39;componentQuery&#39;) + &#39;: &#39; + query + &#39; &#39; + R.get(&#39;toReturnHiddenCmp&#39;), annotation : function () { // success - return nothing to not pollute the output with extra details if (resultsLen === 0 || firstVisibleId == null) return &quot;&quot; // non-empty resultset with some components visible if (firstVisibleId != null) return &quot;The matching component [id=&quot; + firstVisibleId + &quot;] is visible&quot; } }); }, <span id='Siesta-Test-ExtJS-Component-method-waitForXType'> /** </span> * Waits until the a component with the specified xtype can be detected by a simple ComponentQuery. * * The &quot;root&quot; argument of this method can be omitted. * * @param {String} xtype The component xtype to look for. * @param {Ext.Container} root The container to start a component query from. Optional * @param {Function} callback The callback to call after the xtype has been found * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForXType: function (xtype, root, callback, scope, timeout) { return this.waitForComponentQuery(xtype, root, callback, scope, timeout); }, <span id='Siesta-Test-ExtJS-Component-method-waitForComponent'> /** </span> * Waits until the a component with the specified xtype can be detected by a simple ComponentQuery. * * @param {String} component The class name to wait for. * @param {Boolean} rendered true to also wait for the component to be rendered * @param {Function} callback The callback to call after the component has been found * @param {Object} scope The scope for the callback * @param {Int} timeout The maximum amount of time to wait for the condition to be fulfilled. Defaults to the {@link Siesta.Test.ExtJS#waitForTimeout} value. */ waitForComponent: function (component, rendered, callback, scope, timeout) { var Ext = this.getExt(); var xtype if (Ext.isString(component)) { xtype = Ext.ClassManager.get(component).xtype; } else { xtype = component.xtype; } if (rendered) { xtype = xtype + &#39;[rendered]&#39;; } return this.waitForXType(xtype, callback, scope, timeout); }, <span id='Siesta-Test-ExtJS-Component-method-hasSize'> /** </span> * This assertion passes when the passed width and height matches the result of component.getSize() * * @param {Ext.Component/String} component An Ext.Component instance or a ComponentQuery * @param {Int} width * @param {Int} height * @param {String} [description] The description of the assertion */ hasSize: function (component, width, height, description) { component = this.normalizeComponent(component); this.isDeeply(component.getSize(), { width: width, height: height }, description); }, <span id='Siesta-Test-ExtJS-Component-method-hasPosition'> /** </span> * This assertion passes when the passed x and y matches the result of component.getPosition() * * @param {Ext.Component/String} component An Ext.Component instance or a ComponentQuery * @param {Int} x * @param {Int} y * @param {String} [description] The description of the assertion */ hasPosition: function (component, x, y, description) { component = this.normalizeComponent(component); this.isDeeply(component.getPosition(), [x, y], description); }, <span id='Siesta-Test-ExtJS-Component-method-destroysOk'> /** </span> * This assertion accepts variable number of Ext.Component instances (can be also provided as component query string). * Then it calls their &quot;destroy&quot; method and verifies that: * - there were no exceptions during destroy * - that each component was actually destoyed (since destroy can be canceled in the &quot;beforedestroy&quot; event listener) * * @param {Ext.Component/Array[Ext.Component]/String} components A single instance of Ext.Component, an array of such or a string with component query * @param {String} [description] The description of the assertion */ destroysOk : function (components, description) { var Ext = this.Ext(); var R = Siesta.Resource(&#39;Siesta.Test.ExtJS.Component&#39;); if (this.typeOf(components) != &#39;Array&#39;) { if (this.typeOf(components) == &#39;String&#39;) components = this.Ext().ComponentQuery.query(components); else components = [ components ] } if (!components.length) { this.fail(description, { assertionName : &#39;destroysOk&#39;, annotation : R.get(&#39;invalidDestroysOkInput&#39;) }) return } var currentComp var e = this.getExceptionCatcher()(function () { Joose.A.each(components, function (component) { currentComp = component component.destroy() }) }) if (e !== undefined) { this.fail(description, { assertionName : &#39;destroysOk&#39;, got : e, gotDesc : R.get(&#39;exception&#39;), annotation : R.get(&#39;exceptionAnnotation&#39;) + &#39; &#39; + currentComp.id }) return } var me = this var allDestroyed = Joose.A.each(components, function (component) { // ExtJS ST if (!(component.isDestroyed || component.destroy == Ext.emptyFn)) { me.fail(description, { assertionName : &#39;destroysOk&#39;, annotation : R.get(&#39;Component&#39;) + &#39; [&#39; + component.id + &#39;] &#39; + R.get(&#39;destroyFailed&#39;) }) return false } }) if (allDestroyed === false) return this.pass(description, { descTpl : R.get(&#39;destroyPassed&#39;) }) } } }); </pre> </body> </html>