@@ -138,6 +138,45 @@ describe('bind', () => {
138138 expect ( instance . foo ) . to . have . been . called . exactly ( 2 )
139139 } )
140140
141+ it ( 'can bind elements within the shadowDOM' , ( ) => {
142+ const instance = document . createElement ( 'bind-test-element' )
143+ chai . spy . on ( instance , 'foo' )
144+ instance . attachShadow ( { mode : 'open' } )
145+ const el1 = document . createElement ( 'div' )
146+ const el2 = document . createElement ( 'div' )
147+ el1 . setAttribute ( 'data-action' , 'click:bind-test-element#foo' )
148+ el2 . setAttribute ( 'data-action' , 'submit:bind-test-element#foo' )
149+ instance . shadowRoot . append ( el1 , el2 )
150+ bind ( instance )
151+ expect ( instance . foo ) . to . have . not . been . called ( )
152+ el1 . click ( )
153+ expect ( instance . foo ) . to . have . been . called . exactly ( 1 )
154+ el2 . dispatchEvent ( new CustomEvent ( 'submit' ) )
155+ expect ( instance . foo ) . to . have . been . called . exactly ( 2 )
156+ } )
157+
158+ it ( 'binds elements added to shadowDOM' , async ( ) => {
159+ const instance = document . createElement ( 'bind-test-element' )
160+ chai . spy . on ( instance , 'foo' )
161+ instance . attachShadow ( { mode : 'open' } )
162+ const el1 = document . createElement ( 'div' )
163+ const el2 = document . createElement ( 'div' )
164+ el1 . setAttribute ( 'data-action' , 'click:bind-test-element#foo' )
165+ el2 . setAttribute ( 'data-action' , 'submit:bind-test-element#foo' )
166+ bind ( instance )
167+ instance . shadowRoot . append ( el1 )
168+ instance . shadowRoot . append ( el2 )
169+ // We need to wait for a couple of frames after injecting the HTML into to
170+ // controller so that the actions have been bound to the controller.
171+ await waitForNextAnimationFrame ( )
172+ await waitForNextAnimationFrame ( )
173+ expect ( instance . foo ) . to . have . not . been . called ( )
174+ el1 . click ( )
175+ expect ( instance . foo ) . to . have . been . called . exactly ( 1 )
176+ el2 . dispatchEvent ( new CustomEvent ( 'submit' ) )
177+ expect ( instance . foo ) . to . have . been . called . exactly ( 2 )
178+ } )
179+
141180 describe ( 'listenForBind' , ( ) => {
142181 it ( 're-binds actions that are denoted by HTML that is dynamically injected into the controller' , async function ( ) {
143182 const instance = document . createElement ( 'bind-test-element' )
0 commit comments