riot-test-utils
Version:
lightweight testing utility of Riot
2 lines (1 loc) • 9.91 kB
JavaScript
"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var simulateEvent=require("simulate-event"),each=_interopDefault(require("lodash/each")),assign=_interopDefault(require("lodash/assign")),unionWith=_interopDefault(require("lodash/unionWith")),reduce=_interopDefault(require("lodash/reduce")),riot=require("riot"),shallowize=_interopDefault(require("riot-shallowize"));function toHTML(t){return t instanceof HTMLElement||t instanceof SVGElement?t.outerHTML:"<unknown></unknown>"}var JSON_TYPE=Symbol&&Symbol.for("react.test.json");function getTagName(t){return"tagName"in t?t.tagName.toLowerCase():t.nodeName}function getAttributes(t){for(var e={},n=t.attributes,r=0;r<n.length;r+=1){var o=n.item(r);null!==o&&(e[o.nodeName]=[o.nodeValue])}return e}function toJSONChild(t){switch(t.nodeType){case t.ELEMENT_NODE:return toJSON(t);case t.TEXT_NODE:return t.nodeValue;default:return null}}function getChildren(t){var e=[];if(t.hasChildNodes())for(var n=t.childNodes,r=0;r<n.length;r+=1){var o=toJSONChild(n.item(r));null!==o&&e.push(o)}return e}function toJSON(t){if(!(t instanceof HTMLElement||t instanceof SVGElement))throw new Error("element must be HTMLElement or SVGElement");return{type:getTagName(t),props:getAttributes(t),children:getChildren(t),$$typeof:JSON_TYPE}}var eventTypes=["beforeprint","afterprint","beforeunload","abort","error","change","submit","reset","cached","canplay","canplaythrough","chargingchange","chargingtimechange","checking","close","downloading","durationchange","emptied","ended","fullscreenchange","fullscreenerror","invalid","levelchange","loadeddata","loadedmetadata","noupdate","obsolete","offline","online","open","orientationchange","pause","pointerlockchange","pointerlockerror","copy","cut","paste","play","playing","ratechange","readystatechange","seeked","seeking","stalled","success","suspend","timeupdate","updateready","visibilitychange","volumechange","waiting","load","unload","resize","scroll","select","drag","dragenter","dragleave","dragover","dragstart","dragend","drop","touchcancel","touchend","touchenter","touchleave","touchmove","touchstart","blur","focus","focusin","focusout","input","show","click","dblclick","mouseenter","mouseleave","mousedown","mouseup","mouseover","mousemove","mouseout","contextmenu","wheel","message","storage","timeout","keydown","keypress","keyup","progress","loadend","loadstart","popstate","hashchange","transitionend","compositionend","compositionstart","compositionupdate","pagehide","pageshow"];function SimulateConstructor(){var t=this;each(eventTypes,function(e){t[e]=function(t,n){return simulateEvent.simulate(t,e,n)}})}var Simulate=new SimulateConstructor;function toArray(t){for(var e=t.length,n=new Array(e),r=0;r<e;r+=1)n[r]=t.item(r);return n}function each$1(t,e){for(var n=t.length,r=0;r<n;r+=1)if(!1===e(t.item(r),r,t))return}function lazy(t){var e;return function(){return void 0===e?e=t():e}}function map(t,e){for(var n=t.length,r=new Array(n),o=0;o<n;o+=1)r[o]=e(t.item(o),o,t);return r}function compare(t,e){return t.compareDocumentPosition(e)<=0}function empty(t){return t.createDocumentFragment().childNodes}function toNodeList(t){var e={};each(t,function(t,n){e[n]={value:t,enumerable:!0}});var n=t.length>0?t[0].ownerDocument:document;return Object.create(empty(n),assign(e,{length:{value:t.length},item:{value:function(t){return this[t||0]},enumerable:!0}}))}function findList(t,e){var n=map(e,function(e){return e.querySelectorAll(t)});return toNodeList(reduce(n,function(t,e){return unionWith(t,e,compare)},[]))}Simulate.eventTypes=eventTypes.concat();var WeakWrapper=function(){function t(t){var e=this;this.nodeList=t,this.elements=lazy(function(){return toArray(e.nodeList)})}return Object.defineProperty(t.prototype,"instance",{get:function(){return null},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"root",{get:function(){return this.assertSingle()},enumerable:!0,configurable:!0}),t.prototype.simulate=function(t,e){var n=Simulate[t];each$1(this.nodeList,function(t){return n(t,e)})},t.prototype.get=function(t){return void 0===t?this.elements():(t<0&&(t=this.nodeList.length+t),this.nodeList.item(t))},Object.defineProperty(t.prototype,"length",{get:function(){return this.nodeList.length},enumerable:!0,configurable:!0}),t.prototype.find=function(e){return new t(findList(e,this.nodeList))},t.prototype.text=function(){return this.assertSingle().textContent},t.prototype.html=function(){return toHTML(this.assertSingle())},t.prototype.toJSON=function(){return this.isSingle()?toJSON(this.assertSingle()):map(this.nodeList,toJSON)},t.prototype.isSingle=function(){return 1===this.nodeList.length},t.prototype.assertSingle=function(){if(!this.isSingle())throw new Error("Count of nodes must be one!");return this.nodeList.item(0)},t}();function findElement(t,e){return e.querySelectorAll(t)}function find(t,e){return new WeakWrapper(findElement(t,e))}var RiotWrapper=function(){function t(t){this.tagInstance=t}return Object.defineProperty(t.prototype,"instance",{get:function(){return this.tagInstance},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"root",{get:function(){return this.tagInstance.root},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"opts",{get:function(){return this.tagInstance.opts},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"refs",{get:function(){return this.tagInstance.refs},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"tags",{get:function(){return this.tagInstance.tags},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"parent",{get:function(){return this.tagInstance.parent},enumerable:!0,configurable:!0}),t.prototype.update=function(t){this.tagInstance.update(t)},Object.defineProperty(t.prototype,"isMounted",{get:function(){return this.tagInstance.isMounted},enumerable:!0,configurable:!0}),t.prototype.mount=function(){this.tagInstance.mount()},t.prototype.unmount=function(t){this.tagInstance.unmount(t)},t.prototype.on=function(t,e){return this.tagInstance.on(t,e),this},t.prototype.one=function(t,e){return this.tagInstance.one(t,e),this},t.prototype.trigger=function(t){for(var e,n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return(e=this.tagInstance).trigger.apply(e,[t].concat(n)),this},t.prototype.off=function(t,e){return this.tagInstance.off(t,e),this},t.prototype.mixin=function(t){this.instance.mixin(t)},t.prototype.html=function(){var t=this.tagInstance.root;return void 0!==t?toHTML(t):""},t.prototype.text=function(){var t=this.tagInstance.root;return void 0!==t?t.textContent:""},t.prototype.toJSON=function(){var t=this.tagInstance.root;return toJSON(void 0!==t?t:null)},t.prototype.find=function(t){return find(t,this.root)},t.prototype.simulate=function(t,e){Simulate[t](this.tagInstance.root,e)},t}(),extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};function __extends(t,e){function n(){this.constructor=t}extendStatics(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var EvalContext=function(){function EvalContext(){}return EvalContext.prototype.evalTag=function(tagjs){var _this=this,_a=eval("var t={},s=[],riot={tag2:function(n){t[n]=arguments;s.push(n);return n}};"+tagjs+";[t,s];"),tags=_a[0],tagNames=_a[1];return each(tags,function(t){return riot.tag.apply(_this,t)}),{tags:tags,tagNames:tagNames}},EvalContext}();function createElementToMountTo(t){return document.createElement(t)}function selectElementToMount(t,e){return e&&e.attachTo||createElementToMountTo(t)}var RiotRendererBase=function(){function t(t){this.mount=t,this.context=new EvalContext,this.instance=null,this.rendered=null}return t.prototype.loadTags=function(t){var e=riot.compile(t);return this.context.evalTag(e).tagNames},t.prototype.unloadTag=function(t){riot.unregister(t)},t.prototype.render=function(){var t,e,n,r;if(arguments.length>=2?"string"==typeof arguments[1]?(t=(a=arguments)[0],e=a[1],n=a[2],r=a[3]):(t=(u=arguments)[0],n=u[1],r=u[2]):t=arguments[0],/^\s*</m.test(t)){var o=this.loadTags(t);if(void 0===e){if(1!==o.length)throw new Error("Tag source must be single");e=o[0]}}else e=t;var i=this.createInstance(e,n,[],r);i.mount();var a,u,s=i.root;return this.instance=i,this.rendered=s},t.prototype.createInstance=function(t,e,n,r){void 0===e&&(e={}),void 0===n&&(n=[]);var o=selectElementToMount(t,r);return this.mount.apply(riot,[o,t,e])[0]},t.prototype.getMountedInstance=function(){if(null===this.instance)throw new Error("Call after render");return this.instance},t.prototype.getRenderedOutput=function(){if(null===this.rendered)throw new Error("Call after render");return this.rendered},t.prototype.unmount=function(){null!==this.instance&&this.instance.unmount(),this.rendered=null,this.instance=null},t}(),shallow=shallowize({}).shallow,ShallowRenderer=function(t){function e(){return t.call(this,shallow)||this}return __extends(e,t),e}(RiotRendererBase),TestRenderer=function(t){function e(){return t.call(this,riot.mount)||this}return __extends(e,t),e}(RiotRendererBase);function mount(){var t=new TestRenderer;t.render.apply(t,arguments);var e=t.getMountedInstance();return new RiotWrapper(e)}function shallow$1(){var t=new ShallowRenderer;t.render.apply(t,arguments);var e=t.getMountedInstance();return new RiotWrapper(e)}function extend(t){each(t,function(t,e){if(e in RiotWrapper.prototype||e in WeakWrapper.prototype)throw new Error('"'+e+'" is already defined in Wrappers');RiotWrapper.prototype[e]=WeakWrapper.prototype[e]=t})}exports.Simulate=Simulate,exports.mount=mount,exports.shallow=shallow$1,exports.find=find,exports.extend=extend,exports.ShallowRenderer=ShallowRenderer,exports.TestRenderer=TestRenderer,exports.toJSON=toJSON,exports.toHTML=toHTML;