11import { expect , fixture , html } from '@open-wc/testing'
22import { fake } from 'sinon'
33import { controller } from '../src/controller.js'
4- import { bindShadow } from '../src/bind.js'
4+ import { bindShadow , listenForBind } from '../src/bind.js'
55
66describe ( 'Actionable' , ( ) => {
77 @controller
@@ -28,6 +28,43 @@ describe('Actionable', () => {
2828 < div id ="el6 " data-action ="click:bind-test#foo "> </ div > ` )
2929 } )
3030
31+ it ( 'keeps a new subscription cached when an old subscription is unsubscribed again' , ( ) => {
32+ const root = document . createElement ( 'div' )
33+ const old = listenForBind ( root )
34+ old . unsubscribe ( )
35+ const current = listenForBind ( root )
36+ old . unsubscribe ( )
37+ const reused = listenForBind ( root )
38+ try {
39+ expect ( old . closed ) . to . equal ( true )
40+ expect ( current . closed ) . to . equal ( false )
41+ expect ( reused === current ) . to . equal ( true )
42+ } finally {
43+ current . unsubscribe ( )
44+ reused . unsubscribe ( )
45+ }
46+ } )
47+
48+ it ( 'does not leave an observer binding actions after the current subscription is closed' , async ( ) => {
49+ const root = instance . attachShadow ( { mode : 'open' } )
50+ const old = listenForBind ( root )
51+ old . unsubscribe ( )
52+ const current = listenForBind ( root )
53+ old . unsubscribe ( )
54+ const reused = listenForBind ( root )
55+ reused . unsubscribe ( )
56+ try {
57+ const button = document . createElement ( 'button' )
58+ button . setAttribute ( 'data-action' , 'click:bind-test#foo' )
59+ root . append ( button )
60+ await Promise . resolve ( )
61+ button . click ( )
62+ expect ( instance . foo . callCount ) . to . equal ( 0 )
63+ } finally {
64+ current . unsubscribe ( )
65+ }
66+ } )
67+
3168 it ( 'binds events on elements based on their data-action attribute' , ( ) => {
3269 expect ( instance . foo ) . to . have . callCount ( 0 )
3370 instance . querySelector < HTMLElement > ( '#el1' ) ! . click ( )
0 commit comments