11import { bind } from '../lib/bind.js'
22
33describe ( 'bind' , ( ) => {
4- class MyController {
5- get tagName ( ) {
6- return 'my-controller'
7- }
8- querySelectorAll ( ) { }
9- foo ( ) { }
10- matches ( ) { }
11- }
4+ window . customElements . define ( 'bind-test-element' , class extends HTMLElement { } )
125
136 it ( 'queries for Elements matching data-action*="tagname"' , ( ) => {
14- const instance = new MyController ( )
7+ const instance = document . createElement ( 'bind-test-element' )
158 chai . spy . on ( instance , 'querySelectorAll' , ( ) => [ ] )
169 bind ( instance )
17- expect ( instance . querySelectorAll ) . to . have . been . called . once . with . exactly ( '[data-action*=":my-controller #"]' )
10+ expect ( instance . querySelectorAll ) . to . have . been . called . once . with . exactly ( '[data-action*=":bind-test-element #"]' )
1811 } )
1912
2013 it ( 'binds events on elements based on their data-action attribute' , ( ) => {
21- const instance = new MyController ( )
14+ const instance = document . createElement ( 'bind-test-element' )
2215 chai . spy . on ( instance , 'foo' )
2316 const el = document . createElement ( 'div' )
2417 instance . querySelectorAll = ( ) => [ el ]
2518 el . closest = ( ) => instance
26- el . getAttribute = ( ) => 'click:my-controller #foo'
19+ el . getAttribute = ( ) => 'click:bind-test-element #foo'
2720 chai . spy . on ( el , 'addEventListener' )
2821 bind ( instance )
2922 expect ( el . addEventListener ) . to . have . been . called . once . with ( 'click' )
@@ -35,12 +28,12 @@ describe('bind', () => {
3528 } )
3629
3730 it ( 'allows for the presence of `:` in an event name' , ( ) => {
38- const instance = new MyController ( )
31+ const instance = document . createElement ( 'bind-test-element' )
3932 chai . spy . on ( instance , 'foo' )
4033 const el = document . createElement ( 'div' )
4134 instance . querySelectorAll = ( ) => [ el ]
4235 el . closest = ( ) => instance
43- el . getAttribute = ( ) => 'custom:event:my-controller #foo'
36+ el . getAttribute = ( ) => 'custom:event:bind-test-element #foo'
4437 chai . spy . on ( el , 'addEventListener' )
4538 bind ( instance )
4639 expect ( el . addEventListener ) . to . have . been . called . once . with ( 'custom:event' )
@@ -52,10 +45,10 @@ describe('bind', () => {
5245 } )
5346
5447 it ( 'binds events on the controller to itself' , ( ) => {
55- const instance = new MyController ( )
48+ const instance = document . createElement ( 'bind-test-element' )
5649 chai . spy . on ( instance , 'foo' )
5750 instance . matches = ( ) => true
58- instance . getAttribute = ( ) => 'click:my-controller #foo'
51+ instance . getAttribute = ( ) => 'click:bind-test-element #foo'
5952 instance . addEventListener = ( ) => true
6053 instance . querySelectorAll = ( ) => [ ]
6154 chai . spy . on ( instance , 'addEventListener' )
@@ -69,19 +62,19 @@ describe('bind', () => {
6962 } )
7063
7164 it ( 'does not bind elements whose closest selector is not this controller' , ( ) => {
72- const instance = new MyController ( )
65+ const instance = document . createElement ( 'bind-test-element' )
7366 chai . spy . on ( instance , 'foo' )
7467 const el = document . createElement ( 'div' )
7568 instance . querySelectorAll = ( ) => [ el ]
7669 el . closest = ( ) => null
77- el . getAttribute = ( ) => 'click:my-controller #foo'
70+ el . getAttribute = ( ) => 'click:bind-test-element #foo'
7871 chai . spy . on ( el , 'addEventListener' )
7972 bind ( instance )
8073 expect ( el . addEventListener ) . to . have . not . been . called ( )
8174 } )
8275
8376 it ( 'does not bind elements whose data-action does not match controller tagname' , ( ) => {
84- const instance = new MyController ( )
77+ const instance = document . createElement ( 'bind-test-element' )
8578 chai . spy . on ( instance , 'foo' )
8679 const el = document . createElement ( 'div' )
8780 instance . querySelectorAll = ( ) => [ el ]
@@ -93,24 +86,24 @@ describe('bind', () => {
9386 } )
9487
9588 it ( 'does not bind methods that dont exist' , ( ) => {
96- const instance = new MyController ( )
89+ const instance = document . createElement ( 'bind-test-element' )
9790 chai . spy . on ( instance , 'foo' )
9891 const el = document . createElement ( 'div' )
9992 instance . querySelectorAll = ( ) => [ el ]
10093 el . closest = ( ) => instance
101- el . getAttribute = ( ) => 'click:my-controller #frob'
94+ el . getAttribute = ( ) => 'click:bind-test-element #frob'
10295 chai . spy . on ( el , 'addEventListener' )
10396 bind ( instance )
10497 expect ( el . addEventListener ) . to . have . not . been . called ( )
10598 } )
10699
107100 it ( 'can bind multiple event types' , ( ) => {
108- const instance = new MyController ( )
101+ const instance = document . createElement ( 'bind-test-element' )
109102 chai . spy . on ( instance , 'foo' )
110103 const el = document . createElement ( 'div' )
111104 instance . querySelectorAll = ( ) => [ el ]
112105 el . closest = ( ) => instance
113- el . getAttribute = ( ) => 'click:my-controller #foo submit:my-controller #foo'
106+ el . getAttribute = ( ) => 'click:bind-test-element #foo submit:bind-test-element #foo'
114107 chai . spy . on ( el , 'addEventListener' )
115108 bind ( instance )
116109 expect ( el . addEventListener ) . to . have . been . called ( 2 )
@@ -125,15 +118,15 @@ describe('bind', () => {
125118 } )
126119
127120 it ( 'can bind multiple elements to the same event' , ( ) => {
128- const instance = new MyController ( )
121+ const instance = document . createElement ( 'bind-test-element' )
129122 chai . spy . on ( instance , 'foo' )
130123 const el1 = document . createElement ( 'div' )
131124 const el2 = document . createElement ( 'div' )
132125 instance . querySelectorAll = ( ) => [ el1 , el2 ]
133126 el1 . closest = ( ) => instance
134127 el2 . closest = ( ) => instance
135- el1 . getAttribute = ( ) => 'click:my-controller #foo'
136- el2 . getAttribute = ( ) => 'submit:my-controller #foo'
128+ el1 . getAttribute = ( ) => 'click:bind-test-element #foo'
129+ el2 . getAttribute = ( ) => 'submit:bind-test-element #foo'
137130 chai . spy . on ( el1 , 'addEventListener' )
138131 chai . spy . on ( el2 , 'addEventListener' )
139132 bind ( instance )
0 commit comments