UNPKG

siesta-lite

Version:

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

299 lines (236 loc) 11.4 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 */ Singleton(&#39;Siesta.Project.Browser.FeatureSupport&#39;, { does : [ Siesta.Util.Role.CanCalculatePageScroll ], has : { isConfigured : false, supports : Joose.I.Object, simulator : { lazy : &#39;this.buildWebSimulator&#39; }, tests : { init : [ { id : &#39;TouchEvents&#39;, fn : function() { return this.isEventSupported(&#39;touchend&#39;, window); } }, { id : &#39;PointerEvents&#39;, fn : function () { return Boolean(window.PointerEvent) } }, { id : &#39;MSPointerEvents&#39;, fn : function() { return Boolean(window.MSPointerEvent) } }, { id : &#39;PointerEventsGeneric&#39;, fn : function () { return Boolean(window.PointerEvent || window.MSPointerEvent) } }, // &quot;fn&quot;s are called as methods of the &quot;Siesta.Project.Browser.FeatureSupport&quot; singleton { id : &quot;mouseEnterLeave&quot;, fn : function() { var el = document.createElement(&quot;div&quot;); return &#39;onmouseenter&#39; in el &amp;&amp; &#39;onmouseleave&#39; in el; } }, { id : &quot;enterOnAnchorTriggersClick&quot;, fn : function() { var sim = this.getSimulator(), E = Siesta.Test.UserAgent.KeyCodes().keys.ENTER, result = false; var anchor = $(&#39;&lt;a href=&quot;foo&quot; style=&quot;display:none&quot;&gt;test me&lt;/a&gt;&#39;); $(&#39;body&#39;).append(anchor); anchor.focus({ preventScroll : true }); anchor.click(function(e) { result = true; return false; }); sim.simulateEvent(anchor, &#39;keypress&#39;, { keyCode : E, charCode : 0 }); anchor.remove(); return result; } }, { id : &quot;canSimulateKeyCharacters&quot;, fn : function() { var sim = this.getSimulator(); var input = $(&#39;&lt;input class=&quot;siesta-hidden&quot; type=&quot;text&quot; /&gt;&#39;), A = Siesta.Test.UserAgent.KeyCodes().keys.A; $(&#39;body&#39;).append(input); input.focus({ preventScroll : true }); sim.simulateEvent(input, &#39;keypress&#39;, { keyCode : A, charCode : A }); sim.simulateEvent(input, &#39;textInput&#39;, { text : &quot;A&quot; }); var result = input.val() === &#39;A&#39;; input.remove(); return result; } }, { id : &quot;canSimulateBackspace&quot;, fn : function() { var sim = this.getSimulator(); var input = $(&#39;&lt;input class=&quot;siesta-hidden&quot; type=&quot;text&quot; /&gt;&#39;), BS = Siesta.Test.UserAgent.KeyCodes().keys.BACKSPACE, A = Siesta.Test.UserAgent.KeyCodes().keys.A; $(&#39;body&#39;).append(input); input.focus({ preventScroll : true }); sim.simulateEvent(input, &#39;keypress&#39;, { keyCode : A, charCode : A }); sim.simulateEvent(input, &#39;keypress&#39;, { keyCode : A, charCode : A }); sim.simulateEvent(input, &#39;keypress&#39;, { keyCode : BS, charCode : BS }); var result = input.val() === &#39;A&#39;; input.remove(); return result; } }, { id : &quot;enterSubmitsForm&quot;, fn : function() { var sim = this.getSimulator(), E = Siesta.Test.UserAgent.KeyCodes().keys.ENTER, result = false; var form = $(&#39;&lt;form method=&quot;post&quot;&gt;&lt;input type=&quot;text&quot;/&gt;&lt;/form&gt;&#39;); var input = $(form).find(&#39;input&#39;); $(&#39;body&#39;).append(form); form[0].onsubmit = function(e) { result = true; return false; }; input.focus({ preventScroll : true }); sim.simulateEvent(input, &#39;keypress&#39;, { keyCode : E, charCode : 0 }); form.remove(); return result; } }, // remove after https://bugzilla.mozilla.org/show_bug.cgi?id=959992 will be fixed { id : &quot;imageWithIdCreatesGlobalEnumerable&quot;, fn : function () { var img = $(&#39;&lt;img id=&quot;test_img_id&quot;/&gt;&#39;); $(&#39;body&#39;).append(img); var hasImgId = false for (var i in window) { if (i == &#39;test_img_id&#39;) hasImgId = true } img.remove(); return hasImgId; } }, // Safari triggers native &#39;change&#39; event when an INPUT is blurred { id : &quot;inputFiresChangeAfterLosingFocus&quot;, fn : function() { var sim = this.getSimulator(); var input = document.createElement(&#39;input&#39;), result = false, A = Siesta.Test.UserAgent.KeyCodes().keys.A; input.className = &quot;siesta-hidden&quot;; document.body.appendChild(input); input.addEventListener(&#39;change&#39;, function (ev) { result = ev.isTrusted; }); input.focus({ preventScroll : true }); sim.simulateEvent(input, &#39;keydown&#39;, { keyCode : A, charCode : A }); sim.simulateEvent(input, &#39;keypress&#39;, { keyCode : A, charCode : A }); sim.simulateEvent(input, &#39;keyup&#39;, { keyCode : A, charCode : A }); sim.simulateEvent(input, &#39;textInput&#39;, { text : &quot;A&quot; }); input.blur(); document.body.removeChild(input); return result; } } ] } }, methods : { buildWebSimulator : function () { return new Siesta.Test.Simulator({ global : window, test : { normalizeElement : function(a) { return a[ 0 ] || a; } } }); }, configure : function() { // Already configured if (this.isConfigured) return this.isConfigured = true this.maintainScrollPositionDuring(function () { for (var i = 0; i &lt; this.tests.length; i++) { var test = this.tests[i]; var testId = test.id; var detectorFn = test.fn; try { // also save the results to &quot;results&quot; property - we&#39;ll use this in our own test suite // where we copy the feature testing results from the outer scope to inner this.supports[ testId ] = detectorFn.call(this); } catch (e) { this.supports[ testId ] = false } } }) }, // from Modernizr isEventSupported: function (eventName, element) { var isSupported; if (!eventName) return false if (!element || typeof element === &#39;string&#39;) element = document.createElement(element || &#39;div&#39;); // Testing via the `in` operator is sufficient for modern browsers and IE. // When using `setAttribute`, IE skips &quot;unload&quot;, WebKit skips &quot;unload&quot; and // &quot;resize&quot;, whereas `in` &quot;catches&quot; those. eventName = &#39;on&#39; + eventName; isSupported = eventName in element; // Fallback technique for old Firefox - bit.ly/event-detection if (!isSupported) { if (!element.setAttribute) { // Switch to generic element if it lacks `setAttribute`. // It could be the `document`, `window`, or something else. element = document.createElement(&#39;div&#39;); } element.setAttribute(eventName, &#39;&#39;); isSupported = typeof element[ eventName ] === &#39;function&#39;; if (element[ eventName ] !== undefined ) { // If property was created, &quot;remove it&quot; by setting value to `undefined`. element[ eventName ] = undefined; } element.removeAttribute(eventName); } return isSupported; } } }) </pre> </body> </html>