@magnit-ce/code-tests 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -526,13 +526,16 @@ var CodeTestsElement = class extends HTMLElement {
526
526
  this.#boundClickHandler = this.#onClick.bind(this);
527
527
  }
528
528
  connectedCallback() {
529
+ assignClassAndIdToPart(this.shadowRoot);
529
530
  this.addEventListener("click", this.#boundClickHandler);
531
+ if (this.getAttribute("auto") == "false") {
532
+ return;
533
+ }
530
534
  const testsPath = this.getAttribute("src") ?? this.getAttribute("test") ?? this.getAttribute("tests") ?? this.getAttribute("run") ?? this.getAttribute("path");
531
535
  if (testsPath == null) {
532
536
  return;
533
537
  }
534
538
  this.loadTests(testsPath);
535
- assignClassAndIdToPart(this.shadowRoot);
536
539
  }
537
540
  disconnectedCallback() {
538
541
  this.removeEventListener("click", this.#boundClickHandler);
@@ -686,6 +689,9 @@ var CodeTestsElement = class extends HTMLElement {
686
689
  this.#handleHookResult(hookResult, false, "before", error);
687
690
  console.error(error);
688
691
  this.#continueRunningTests = false;
692
+ this.classList.remove("running");
693
+ this.part.remove("running");
694
+ this.dispatchEvent(new CustomEvent("afterall" /* AfterAll */, { bubbles: true, composed: true }));
689
695
  return;
690
696
  }
691
697
  }
@@ -704,6 +710,9 @@ var CodeTestsElement = class extends HTMLElement {
704
710
  }
705
711
  }
706
712
  if (this.#continueRunningTests == false) {
713
+ this.classList.remove("running");
714
+ this.part.remove("running");
715
+ this.dispatchEvent(new CustomEvent("afterall" /* AfterAll */, { bubbles: true, composed: true }));
707
716
  return;
708
717
  }
709
718
  const afterHooks = this.#hooks.get(AFTERALL);
@@ -723,6 +732,9 @@ var CodeTestsElement = class extends HTMLElement {
723
732
  this.#handleHookResult(hookResult, false, "after", error);
724
733
  console.error(error);
725
734
  this.#continueRunningTests = false;
735
+ this.classList.remove("running");
736
+ this.part.remove("running");
737
+ this.dispatchEvent(new CustomEvent("afterall" /* AfterAll */, { bubbles: true, composed: true }));
726
738
  return;
727
739
  }
728
740
  }
@@ -493,13 +493,16 @@ var CodeTestsElement = class extends HTMLElement {
493
493
  this.#boundClickHandler = this.#onClick.bind(this);
494
494
  }
495
495
  connectedCallback() {
496
+ assignClassAndIdToPart(this.shadowRoot);
496
497
  this.addEventListener("click", this.#boundClickHandler);
498
+ if (this.getAttribute("auto") == "false") {
499
+ return;
500
+ }
497
501
  const testsPath = this.getAttribute("src") ?? this.getAttribute("test") ?? this.getAttribute("tests") ?? this.getAttribute("run") ?? this.getAttribute("path");
498
502
  if (testsPath == null) {
499
503
  return;
500
504
  }
501
505
  this.loadTests(testsPath);
502
- assignClassAndIdToPart(this.shadowRoot);
503
506
  }
504
507
  disconnectedCallback() {
505
508
  this.removeEventListener("click", this.#boundClickHandler);
@@ -653,6 +656,9 @@ var CodeTestsElement = class extends HTMLElement {
653
656
  this.#handleHookResult(hookResult, false, "before", error);
654
657
  console.error(error);
655
658
  this.#continueRunningTests = false;
659
+ this.classList.remove("running");
660
+ this.part.remove("running");
661
+ this.dispatchEvent(new CustomEvent("afterall" /* AfterAll */, { bubbles: true, composed: true }));
656
662
  return;
657
663
  }
658
664
  }
@@ -671,6 +677,9 @@ var CodeTestsElement = class extends HTMLElement {
671
677
  }
672
678
  }
673
679
  if (this.#continueRunningTests == false) {
680
+ this.classList.remove("running");
681
+ this.part.remove("running");
682
+ this.dispatchEvent(new CustomEvent("afterall" /* AfterAll */, { bubbles: true, composed: true }));
674
683
  return;
675
684
  }
676
685
  const afterHooks = this.#hooks.get(AFTERALL);
@@ -690,6 +699,9 @@ var CodeTestsElement = class extends HTMLElement {
690
699
  this.#handleHookResult(hookResult, false, "after", error);
691
700
  console.error(error);
692
701
  this.#continueRunningTests = false;
702
+ this.classList.remove("running");
703
+ this.part.remove("running");
704
+ this.dispatchEvent(new CustomEvent("afterall" /* AfterAll */, { bubbles: true, composed: true }));
693
705
  return;
694
706
  }
695
707
  }
@@ -385,7 +385,7 @@ pre
385
385
  <div id="after-all-results" class="results"></div>
386
386
  </details>`;var T=class extends Promise{async toBeDefined(t){if(await this==null)throw new Error(`${t??"Value"} is undefined`)}async toBe(t,s=!1){let e=await this;if((s==!0?e===t:e==t)==!1)throw new Error(` Value is not equal.
387
387
  Expected: ${t}
388
- Result: ${e}`)}async toContainText(t){let s=await this}async toHaveAttribute(t){let s=await this;if(!(s instanceof HTMLElement))throw new Error("Unable to check for attribute on non-HTMLElement target");if(s.getAttribute(t))throw new Error("Taret does not have attribute")}},y=Symbol("beforeAll"),v=Symbol("beforeEach"),k=Symbol("afterAll"),x=Symbol("afterEach"),A=class{static timeoutMS=500;static#e;static#p;static expect(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}static expectSync(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}static expectBefore(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}};function q(h){return A.expect(h)}var F=":not(slot,defs,g,rect,path,circle,ellipse,line,polygon,text,tspan,use,svg image,svg title,desc,template,template *)";function $(h){let t=[...h.querySelectorAll(`${F}[id]`)];for(let e=0;e<t.length;e++)t[e].part.add(t[e].id);let s=[...h.querySelectorAll(`${F}[class]`)];for(let e=0;e<s.length;e++)s[e].part.add(...s[e].classList)}var j=(r=>(r.BeforeAll="beforeall",r.AfterAll="afterall",r.BeforeTest="beforetest",r.AfterTest="aftertest",r))(j||{}),M=Symbol("No Test Defined"),P=new CSSStyleSheet;P.replaceSync(S);var D="code-tests",R=class extends HTMLElement{componentParts=new Map;getElement(t){if(this.componentParts.get(t)==null){let s=this.findElement(t);s!=null&&this.componentParts.set(t,s)}return this.componentParts.get(t)}findElement(t){return this.shadowRoot.getElementById(t)}#e=new Map;#p={[y]:L(),[v]:L(),[x]:L(),[k]:L()};#t=!0;constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.innerHTML=H,this.shadowRoot.adoptedStyleSheets.push(P),this.#c=this.#h.bind(this)}connectedCallback(){this.addEventListener("click",this.#c);let t=this.getAttribute("src")??this.getAttribute("test")??this.getAttribute("tests")??this.getAttribute("run")??this.getAttribute("path");t!=null&&(this.loadTests(t),$(this.shadowRoot))}disconnectedCallback(){this.removeEventListener("click",this.#c)}#c;#h(t){let s=t.composedPath().find(n=>n instanceof HTMLButtonElement&&n.classList.contains("run"));if(s==null)return;let e=s.closest("li");if(e==null){s.hasAttribute("data-all")==!0&&this.runTests();return}let r=e.dataset.testId;if(r==null)return;let a=this.#s.get(r);a!=null&&this.#u(r,a)}async loadTests(t){try{this.getElement("tests").innerHTML="",this.#s.clear(),this.classList.remove("has-before-hook"),this.classList.remove("has-after-hook");let s=window.location.href.lastIndexOf("/"),r=window.location.href.substring(s).indexOf(".")!=-1==!0?window.location.href.substring(0,s+1):window.location.href,a=r+t.substring(0,t.lastIndexOf("/")+1),n=r+t,o=await(await fetch(n)).text();o=o.replaceAll(/['"`](((\.\/)|(\.\.\/))+(.*))['"`]/g,`'${a}$1'`);let l=new File([o],t.substring(t.lastIndexOf("/")),{type:"text/javascript"}),i=await import(URL.createObjectURL(l)),c=i.tests??i.default;if(c==null)throw new Error(`Unable to find tests definition in file at path: ${t}`);let b=c[y];if(b!=null){if(this.#e.get(y)==null){let u=new Map;u.set(b,new Set),this.#e.set(y,u)}this.classList.add("has-before-hook")}let E=c[v];if(E!=null&&this.#e.get(v)==null){let u=new Map;u.set(E,new Set),this.#e.set(v,u)}let g=c[k];if(g!=null){if(this.#e.get(k)==null){let u=new Map;u.set(g,new Set),this.#e.set(k,u)}this.classList.add("has-after-hook")}let m=c[x];if(m!=null&&this.#e.get(x)==null){let u=new Map;u.set(m,new Set),this.#e.set(x,u)}for(let[w,u]of Object.entries(c)){let C=this.#g(w,u);if(b!=null){let d=this.#e.get(y);if(d!=null){let p=d.get(b);p?.add(C)}}if(E!=null){let d=this.#e.get(v);if(d!=null){let p=d.get(E);p?.add(C)}}if(g!=null){let d=this.#e.get(k);if(d!=null){let p=d.get(g);p?.add(C)}}if(m!=null){let d=this.#e.get(x);if(d!=null){let p=d.get(m);p?.add(C)}}}}catch(s){this.#i("An error occurred while loading the tasks:",s)}}async runTests(){this.dispatchEvent(new CustomEvent("beforeall",{bubbles:!0,composed:!0})),this.#t=!0,this.classList.add("running"),this.toggleAttribute("success",!1),this.#f();let t=this.hasAttribute("in-order"),s=this.#e.get(y);if(s!=null){let a;try{let n=this.getElement("before-all-details");n.classList.add("running"),n.part.add("running");for(let[o,l]of s)a=await o(),this.#n(a,!0,"before");n.part.remove("running"),n.classList.remove("running")}catch(n){this.#n(a,!1,"before",n),console.error(n),this.#t=!1;return}}if(t==!1){let a=[];for(let[n,o]of this.#s)a.push(this.#u(n,o));await Promise.all(a)}else for(let[a,n]of this.#s){if(this.#t==!1)break;await this.#u(a,n)}if(this.#t==!1)return;let e=this.#e.get(k);if(e!=null){let a;try{let n=this.getElement("after-all-details");n.classList.add("running"),n.part.add("running");for(let[o,l]of e)a=await o(),this.#n(a,!0,"after");n.part.remove("running"),n.classList.remove("running")}catch(n){this.#n(a,!1,"after",n),console.error(n),this.#t=!1;return}}let r=this.shadowRoot.querySelectorAll('[success="false"]');this.setAttribute("success",r.length==0?"true":"false"),this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}))}#f(){for(let[e,r]of this.#s){let a=this.getElement("tests").querySelector(`[data-test-id="${e}"]`);if(a==null){this.#i(`Unable to find test element for test: ${e}`);return}a.toggleAttribute("success",!1),a.classList.remove("success","fail"),a.part.remove("success","fail")}let t=this.getElement("before-all-details");t.toggleAttribute("success",!1),t.classList.remove("success","fail"),t.part.remove("success","fail");let s=this.getElement("after-all-details");s.toggleAttribute("success",!1),s.classList.remove("success","fail"),s.part.remove("success","fail")}async#u(t,s){let e=this.getElement("tests").querySelector(`[data-test-id="${t}"]`);if(e==null){this.#i(`Unable to find test element for test: ${t}`);return}e.toggleAttribute("success",!1),e.classList.add("running"),e.part.add("running"),e.classList.remove("success","fail"),e.part.remove("success","fail");let r=e.querySelector(".result-icon");r?.classList.remove("success","fail"),r?.part.remove("success","fail"),r?.classList.add("running"),r?.part.add("running");let a=e.querySelector(".error-message");a!=null&&(a.textContent="");let n=e.querySelector("details");n!=null&&(n.open=!1);let o=M,l,f=M,i;try{if(this.dispatchEvent(new CustomEvent("beforetest",{bubbles:!0,cancelable:!0,composed:!0,detail:{testElement:e}}))==!0){let b=this.#e.get(v);if(b!=null){for(let[g,m]of b)if(m.has(t)){o=await g();break}}l=await s();let E=this.#e.get(x);if(E!=null){for(let[g,m]of E)if(m.has(t)){f=await g();break}}i="before",o!=M&&this.#a(e,o,!0,void 0,i),i=void 0,this.#a(e,l,!0,void 0,i),i="after",f!=M&&this.#a(e,f,!0,void 0,i)}}catch(c){this.#a(e,l,!1,c,i),console.error(c),this.#t=!1}finally{e?.classList.remove("running"),e?.part.remove("running"),r?.classList.remove("running"),r?.part.remove("running"),this.dispatchEvent(new CustomEvent("aftertest",{bubbles:!0,cancelable:!0,composed:!0,detail:{testElement:e}}))}}#a(t,s,e,r,a){if(s instanceof HTMLElement)this.#o(t,s,e,a);else if(s==null){let o=a==null?"Passed":"Hook Ran Successfully",l=this.#r(e==!0?`${o}`:`Failed${r!=null?`:
388
+ Result: ${e}`)}async toContainText(t){let s=await this}async toHaveAttribute(t){let s=await this;if(!(s instanceof HTMLElement))throw new Error("Unable to check for attribute on non-HTMLElement target");if(s.getAttribute(t))throw new Error("Taret does not have attribute")}},y=Symbol("beforeAll"),v=Symbol("beforeEach"),k=Symbol("afterAll"),x=Symbol("afterEach"),A=class{static timeoutMS=500;static#e;static#p;static expect(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}static expectSync(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}static expectBefore(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}};function q(h){return A.expect(h)}var F=":not(slot,defs,g,rect,path,circle,ellipse,line,polygon,text,tspan,use,svg image,svg title,desc,template,template *)";function $(h){let t=[...h.querySelectorAll(`${F}[id]`)];for(let e=0;e<t.length;e++)t[e].part.add(t[e].id);let s=[...h.querySelectorAll(`${F}[class]`)];for(let e=0;e<s.length;e++)s[e].part.add(...s[e].classList)}var j=(r=>(r.BeforeAll="beforeall",r.AfterAll="afterall",r.BeforeTest="beforetest",r.AfterTest="aftertest",r))(j||{}),M=Symbol("No Test Defined"),P=new CSSStyleSheet;P.replaceSync(S);var D="code-tests",R=class extends HTMLElement{componentParts=new Map;getElement(t){if(this.componentParts.get(t)==null){let s=this.findElement(t);s!=null&&this.componentParts.set(t,s)}return this.componentParts.get(t)}findElement(t){return this.shadowRoot.getElementById(t)}#e=new Map;#p={[y]:L(),[v]:L(),[x]:L(),[k]:L()};#t=!0;constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.innerHTML=H,this.shadowRoot.adoptedStyleSheets.push(P),this.#c=this.#h.bind(this)}connectedCallback(){if($(this.shadowRoot),this.addEventListener("click",this.#c),this.getAttribute("auto")=="false")return;let t=this.getAttribute("src")??this.getAttribute("test")??this.getAttribute("tests")??this.getAttribute("run")??this.getAttribute("path");t!=null&&this.loadTests(t)}disconnectedCallback(){this.removeEventListener("click",this.#c)}#c;#h(t){let s=t.composedPath().find(n=>n instanceof HTMLButtonElement&&n.classList.contains("run"));if(s==null)return;let e=s.closest("li");if(e==null){s.hasAttribute("data-all")==!0&&this.runTests();return}let r=e.dataset.testId;if(r==null)return;let a=this.#s.get(r);a!=null&&this.#u(r,a)}async loadTests(t){try{this.getElement("tests").innerHTML="",this.#s.clear(),this.classList.remove("has-before-hook"),this.classList.remove("has-after-hook");let s=window.location.href.lastIndexOf("/"),r=window.location.href.substring(s).indexOf(".")!=-1==!0?window.location.href.substring(0,s+1):window.location.href,a=r+t.substring(0,t.lastIndexOf("/")+1),n=r+t,o=await(await fetch(n)).text();o=o.replaceAll(/['"`](((\.\/)|(\.\.\/))+(.*))['"`]/g,`'${a}$1'`);let l=new File([o],t.substring(t.lastIndexOf("/")),{type:"text/javascript"}),i=await import(URL.createObjectURL(l)),c=i.tests??i.default;if(c==null)throw new Error(`Unable to find tests definition in file at path: ${t}`);let b=c[y];if(b!=null){if(this.#e.get(y)==null){let u=new Map;u.set(b,new Set),this.#e.set(y,u)}this.classList.add("has-before-hook")}let E=c[v];if(E!=null&&this.#e.get(v)==null){let u=new Map;u.set(E,new Set),this.#e.set(v,u)}let g=c[k];if(g!=null){if(this.#e.get(k)==null){let u=new Map;u.set(g,new Set),this.#e.set(k,u)}this.classList.add("has-after-hook")}let m=c[x];if(m!=null&&this.#e.get(x)==null){let u=new Map;u.set(m,new Set),this.#e.set(x,u)}for(let[w,u]of Object.entries(c)){let C=this.#g(w,u);if(b!=null){let d=this.#e.get(y);if(d!=null){let p=d.get(b);p?.add(C)}}if(E!=null){let d=this.#e.get(v);if(d!=null){let p=d.get(E);p?.add(C)}}if(g!=null){let d=this.#e.get(k);if(d!=null){let p=d.get(g);p?.add(C)}}if(m!=null){let d=this.#e.get(x);if(d!=null){let p=d.get(m);p?.add(C)}}}}catch(s){this.#i("An error occurred while loading the tasks:",s)}}async runTests(){this.dispatchEvent(new CustomEvent("beforeall",{bubbles:!0,composed:!0})),this.#t=!0,this.classList.add("running"),this.toggleAttribute("success",!1),this.#f();let t=this.hasAttribute("in-order"),s=this.#e.get(y);if(s!=null){let a;try{let n=this.getElement("before-all-details");n.classList.add("running"),n.part.add("running");for(let[o,l]of s)a=await o(),this.#n(a,!0,"before");n.part.remove("running"),n.classList.remove("running")}catch(n){this.#n(a,!1,"before",n),console.error(n),this.#t=!1,this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}));return}}if(t==!1){let a=[];for(let[n,o]of this.#s)a.push(this.#u(n,o));await Promise.all(a)}else for(let[a,n]of this.#s){if(this.#t==!1)break;await this.#u(a,n)}if(this.#t==!1){this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}));return}let e=this.#e.get(k);if(e!=null){let a;try{let n=this.getElement("after-all-details");n.classList.add("running"),n.part.add("running");for(let[o,l]of e)a=await o(),this.#n(a,!0,"after");n.part.remove("running"),n.classList.remove("running")}catch(n){this.#n(a,!1,"after",n),console.error(n),this.#t=!1,this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}));return}}let r=this.shadowRoot.querySelectorAll('[success="false"]');this.setAttribute("success",r.length==0?"true":"false"),this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}))}#f(){for(let[e,r]of this.#s){let a=this.getElement("tests").querySelector(`[data-test-id="${e}"]`);if(a==null){this.#i(`Unable to find test element for test: ${e}`);return}a.toggleAttribute("success",!1),a.classList.remove("success","fail"),a.part.remove("success","fail")}let t=this.getElement("before-all-details");t.toggleAttribute("success",!1),t.classList.remove("success","fail"),t.part.remove("success","fail");let s=this.getElement("after-all-details");s.toggleAttribute("success",!1),s.classList.remove("success","fail"),s.part.remove("success","fail")}async#u(t,s){let e=this.getElement("tests").querySelector(`[data-test-id="${t}"]`);if(e==null){this.#i(`Unable to find test element for test: ${t}`);return}e.toggleAttribute("success",!1),e.classList.add("running"),e.part.add("running"),e.classList.remove("success","fail"),e.part.remove("success","fail");let r=e.querySelector(".result-icon");r?.classList.remove("success","fail"),r?.part.remove("success","fail"),r?.classList.add("running"),r?.part.add("running");let a=e.querySelector(".error-message");a!=null&&(a.textContent="");let n=e.querySelector("details");n!=null&&(n.open=!1);let o=M,l,f=M,i;try{if(this.dispatchEvent(new CustomEvent("beforetest",{bubbles:!0,cancelable:!0,composed:!0,detail:{testElement:e}}))==!0){let b=this.#e.get(v);if(b!=null){for(let[g,m]of b)if(m.has(t)){o=await g();break}}l=await s();let E=this.#e.get(x);if(E!=null){for(let[g,m]of E)if(m.has(t)){f=await g();break}}i="before",o!=M&&this.#a(e,o,!0,void 0,i),i=void 0,this.#a(e,l,!0,void 0,i),i="after",f!=M&&this.#a(e,f,!0,void 0,i)}}catch(c){this.#a(e,l,!1,c,i),console.error(c),this.#t=!1}finally{e?.classList.remove("running"),e?.part.remove("running"),r?.classList.remove("running"),r?.part.remove("running"),this.dispatchEvent(new CustomEvent("aftertest",{bubbles:!0,cancelable:!0,composed:!0,detail:{testElement:e}}))}}#a(t,s,e,r,a){if(s instanceof HTMLElement)this.#o(t,s,e,a);else if(s==null){let o=a==null?"Passed":"Hook Ran Successfully",l=this.#r(e==!0?`${o}`:`Failed${r!=null?`:
389
389
  ${r.message}`:""}`,e,a);this.#o(t,l,e,a)}else if(typeof s=="string"){let o=this.#r(`${s}${r==null?"":`:
390
390
  ${r.message}`}`,e,a);this.#o(t,o,e,a)}else if(typeof s=="object"){let o=s;if(o.success!=null&&o.expected!=null&&o.value!=null){let l=a==null?"Passed":"Success",f=a==null?"Failed":"Fail",i=this.#r(`${o.success==!0?`${l}:`:`${f}:`}
391
391
  Expected:${o.expected}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magnit-ce/code-tests",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "A custom html element that interprets and runs tests in a browser.",
5
5
  "type": "module",
6
6
  "main": "dist/code-tests.js",