@atom-learning/jest-stitches
Version:
Jest utilities for @stitches/react
3 lines (2 loc) • 4.37 kB
JavaScript
function e(e,r){return p(r)?((e,r)=>{var{props:t}=r;return l(e,t?(t.className||t.class||"").toString():null)})(e,r):"function"==typeof r.findWhere?((e,r)=>{var t=((e=>"function"==typeof e.dive&&"string"!=typeof e.type())(r)?r.dive():r).findWhere(u),n=t.length&&t.first().prop("className")?t.first().prop("className").toString():t.first().prop("className");return l(e,n)})(e,r):(e=>"[cheerio object]"===e.cheerio)(r)?((e,r)=>{var t=r.attr("class");return l(e,t)})(e,r):((e,r)=>l(e,r.getAttribute("class")))(e,r)}function r(e){return e.cssText}function t(e,r){return e.concat(r.rules)}function n(e,r){return e.concat(r.declarations)}function a(e,r){return O.test(r),e}import{parse as o,stringify as s}from"css";import c from"chalk";var i={media:"media",rule:"rule"},l=(e,r)=>r?e.concat(r.split(" ")):e,u=e=>e.prop("className")&&"string"==typeof e.type(),p=e=>{if(e.$$typeof===Symbol.for("react.test.json"))return 1;if(e.hasOwnProperty("props")&&e.hasOwnProperty("type")&&e.hasOwnProperty("ref")&&e.hasOwnProperty("key")){try{e.$$typeof=Symbol.for("react.test.json")}catch(e){}return 1}},f=/^((HTML|SVG)\w*)?Element$/,y=e=>1===e.nodeType&&e.constructor&&e.constructor.name&&f.test(e.constructor.name),m=r=>r.reduce(e,[]),v=/^@keyframes\s+(animation-[^{\s]+)+/,d=/\/\*[\s\S]*?\*\//g,h=e=>{var t=e.textContent;return t?[t]:e.sheet?[].slice.call(e.sheet.cssRules).map(r):[]},g=(e,r)=>{if(!e.length)return"";var t,n,a=new RegExp("\\.("+e.join("|")+")"),o={},s="";(t=r,n=h,[].concat(...t.map(n))).forEach(e=>{a.test(e)&&(s+=e);var r=e.match(v);if(null!==r){var t=r[1];void 0===o[t]&&(o[t]=""),o[t]+=e}});var c=Object.keys(o),i="";if(c.length){var l=new RegExp(c.join("|"),"g"),u={},p=0;s=s.replace(l,e=>(void 0===u[e]&&(u[e]="animation-"+p++,i+=o[e]),u[e])),i=i.replace(l,e=>u[e])}return(i+s).replace(d,"")},w=()=>Array.from(document.querySelectorAll("style")),E=e=>{return r=e.filter(Boolean),Array.from(new Set(r));var r},S=(e,r,t)=>r.some(r=>t?t instanceof RegExp?t.test(r):b(r).includes(b(t)):e.includes(r.slice(1))),j=(e,r)=>e.filter(e=>{var t=e.media?e.media.replace(/\s/g,"").includes(r.replace(/\s/g,"")):0;return e.type===i.media&&t}).reduce(t,[]),x=/([:;,([{}>~/\s]|\/\*)\s+/g,A=/\s+([:;,)\]{}>~/!]|\*\/)/g,b=e=>e.trim().replace(x,"$1").replace(A,"$1"),$={toHaveStyleRule:function(e,r,t,a){function s(){return"Property not found: "+r}void 0===a&&(a={});var{target:l,media:u}=a,p=m([e]),f=g(p,w()),y=o(f).stylesheet.rules;u&&(y=j(y,u));var v=y.filter(e=>e.type===i.rule&&S(p,e.selectors,l)).reduce(n,[]).filter(e=>"declaration"===e.type&&b(e.property)===b(r)).pop();if(!v)return{pass:0,message:s};var s,d=((e,r)=>r instanceof RegExp?r.test(e.value):(e=>{return e&&("Function",r=e.asymmetricMatch,"[object Function]"===Object.prototype.toString.apply(r));var r})(r)?r.asymmetricMatch(e.value):b(r)===b(e.value))(v,t);return{pass:d,message:s=()=>"Expected "+r+(d?" not ":" ")+"to match:\n "+c.green(t)+"\nReceived:\n "+c.red(v.value)}}},O=/^e[a-zA-Z0-9]+[0-9]+$/,R=(e,r,t)=>e.reduce(a,r+(r?"\n\n":"")+t),k=function(e){void 0===e&&(e={});var{DOMElements:r=1}=e,t=new WeakSet;return{test:e=>e&&!t.has(e)&&(p(e)||r&&y(e)),print(e,r){var n=function e(r,t){if(void 0===t&&(t=[]),Array.isArray(r)){for(var n of r)e(n,t);return t}var a=r.children||r.props&&r.props.children;if(a)for(var o of a=r.props?Array.isArray(a)?a:[a]:a)e(o,t);return"object"==typeof r&&t.push(r),t}(e),a=m(n),c=w(),i=((e,r)=>{var t,n=g(e,r),a=o(n);a.stylesheet.rules=a.stylesheet.rules.map(e=>{var r;if("keyframes"===e.type){var t=null;function n(r){"animation-name"!==r.property&&"animation"!==r.property||r.value!==e.name||(t=e)}return a.stylesheet.rules.some(e=>{var r;return null===(r=e.declarations)||void 0===r?void 0:r.some(n)}),t}return(null===(r=e.selectors)||void 0===r?void 0:r.includes(":root"))?null:e}).filter(Boolean);try{t=s(a)}catch(e){throw console.error(e),new Error('There was an error parsing the following css: "'+n+'"')}return t})(a,c);n.forEach(t.add,t);var l=r(e);return n.forEach(t.delete,t),E(c),R(a,i,l)}}},{print:N,test:P}=k(),M={print:N,test:P};export default M;export{i as RULE_TYPES,k as createSerializer,m as getClassNamesFromNodes,E as getKeys,j as getMediaRules,w as getStyleElements,g as getStylesFromClassNames,S as hasClassNames,y as isDOMElement,p as isReactElement,$ as matchers,N as print,R as replaceClassNames,P as test};
//# sourceMappingURL=index.mjs.map