11import { controller } from '../lib/controller.js'
22
33describe ( 'controller' , ( ) => {
4+ let root
5+
6+ beforeEach ( ( ) => {
7+ root = document . createElement ( 'div' )
8+ document . body . appendChild ( root )
9+ } )
10+
11+ afterEach ( ( ) => {
12+ root . remove ( )
13+ } )
14+
415 it ( 'calls register' , async ( ) => {
516 class ControllerRegisterElement extends HTMLElement { }
617 controller ( ControllerRegisterElement )
718 const instance = document . createElement ( 'controller-register' )
8- document . body . appendChild ( instance )
19+ root . appendChild ( instance )
920 expect ( instance ) . to . be . instanceof ( ControllerRegisterElement )
1021 } )
1122
1223 it ( 'adds data-catalyst to elements' , async ( ) => {
1324 controller ( class ControllerDataAttrElement extends HTMLElement { } )
1425 const instance = document . createElement ( 'controller-data-attr' )
15- document . body . appendChild ( instance )
26+ root . appendChild ( instance )
1627 expect ( instance . hasAttribute ( 'data-catalyst' ) ) . to . equal ( true )
1728 expect ( instance . getAttribute ( 'data-catalyst' ) ) . to . equal ( '' )
1829 } )
@@ -29,7 +40,7 @@ describe('controller', () => {
2940
3041 const instance = document . createElement ( 'controller-bind-order' )
3142 chai . spy . on ( instance , 'foo' )
32- document . body . appendChild ( instance )
43+ root . appendChild ( instance )
3344
3445 const sub = document . createElement ( 'controller-bind-order-sub' )
3546 sub . setAttribute ( 'data-action' , 'loaded:controller-bind-order#foo' )
@@ -51,7 +62,7 @@ describe('controller', () => {
5162 )
5263 const instance = document . createElement ( 'controller-bind-shadow' )
5364 chai . spy . on ( instance , 'foo' )
54- document . body . appendChild ( instance )
65+ root . appendChild ( instance )
5566
5667 instance . shadowRoot . querySelector ( 'button' ) . click ( )
5768
@@ -67,7 +78,7 @@ describe('controller', () => {
6778 template . innerHTML = '<button data-action="click:controller-bind-auto-shadow#foo"></button>'
6879 instance . appendChild ( template )
6980 chai . spy . on ( instance , 'foo' )
70- document . body . appendChild ( instance )
81+ root . appendChild ( instance )
7182
7283 expect ( instance . shadowRoot ) . to . exist
7384 expect ( instance ) . to . have . property ( 'shadowRoot' ) . not . equal ( null )
@@ -76,4 +87,19 @@ describe('controller', () => {
7687
7788 expect ( instance . foo ) . to . have . been . called ( 1 )
7889 } )
90+
91+ it ( 'upgrades child decendants when connected' , ( ) => {
92+ controller ( class ChildElementElement extends HTMLElement { } )
93+ controller (
94+ class ParentElementElement extends HTMLElement {
95+ connectedCallback ( ) {
96+ const child = this . querySelector ( 'child-element' )
97+ expect ( child . matches ( ':defined' ) ) . to . equal ( true )
98+ }
99+ }
100+ )
101+
102+ // eslint-disable-next-line github/unescaped-html-literal
103+ root . innerHTML = '<parent-element><child-element></child-element></parent-element>'
104+ } )
79105} )
0 commit comments