@@ -31,129 +31,102 @@ describe('bind', () => {
3131 const instance = document . createElement ( 'bind-test-element' )
3232 chai . spy . on ( instance , 'foo' )
3333 const el = document . createElement ( 'div' )
34- instance . querySelectorAll = ( ) => [ el ]
35- el . closest = ( ) => instance
36- el . getAttribute = ( ) => 'click:bind-test-element#foo'
37- chai . spy . on ( el , 'addEventListener' )
34+ el . setAttribute ( 'data-action' , 'click:bind-test-element#foo' )
35+ instance . appendChild ( el )
3836 bind ( instance )
39- expect ( el . addEventListener ) . to . have . been . called . once . with ( 'click' )
40- const { calls} = el . addEventListener . __spy
41- const fn = calls [ 0 ] [ 1 ]
4237 expect ( instance . foo ) . to . have . not . been . called ( )
43- fn ( )
38+ el . click ( )
4439 expect ( instance . foo ) . to . have . been . called ( 1 )
4540 } )
4641
4742 it ( 'allows for the presence of `:` in an event name' , ( ) => {
4843 const instance = document . createElement ( 'bind-test-element' )
4944 chai . spy . on ( instance , 'foo' )
5045 const el = document . createElement ( 'div' )
51- instance . querySelectorAll = ( ) => [ el ]
52- el . closest = ( ) => instance
53- el . getAttribute = ( ) => 'custom:event:bind-test-element#foo'
54- chai . spy . on ( el , 'addEventListener' )
46+ el . setAttribute ( 'data-action' , 'custom:event:bind-test-element#foo' )
47+ instance . appendChild ( el )
5548 bind ( instance )
56- expect ( el . addEventListener ) . to . have . been . called . once . with ( 'custom:event' )
57- const { calls} = el . addEventListener . __spy
58- const fn = calls [ 0 ] [ 1 ]
5949 expect ( instance . foo ) . to . have . not . been . called ( )
60- fn ( )
50+ el . dispatchEvent ( new CustomEvent ( 'custom:event' ) )
6151 expect ( instance . foo ) . to . have . been . called ( 1 )
6252 } )
6353
6454 it ( 'binds events on the controller to itself' , ( ) => {
6555 const instance = document . createElement ( 'bind-test-element' )
6656 chai . spy . on ( instance , 'foo' )
67- instance . matches = ( ) => true
68- instance . getAttribute = ( ) => 'click:bind-test-element#foo'
69- instance . addEventListener = ( ) => true
70- instance . querySelectorAll = ( ) => [ ]
71- chai . spy . on ( instance , 'addEventListener' )
57+ instance . setAttribute ( 'data-action' , 'click:bind-test-element#foo' )
7258 bind ( instance )
73- expect ( instance . addEventListener ) . to . have . been . called . once . with ( 'click' )
74- const { calls} = instance . addEventListener . __spy
75- const fn = calls [ 0 ] [ 1 ]
7659 expect ( instance . foo ) . to . have . not . been . called ( )
77- fn ( )
60+ instance . click ( )
7861 expect ( instance . foo ) . to . have . been . called ( 1 )
7962 } )
8063
8164 it ( 'does not bind elements whose closest selector is not this controller' , ( ) => {
8265 const instance = document . createElement ( 'bind-test-element' )
8366 chai . spy . on ( instance , 'foo' )
8467 const el = document . createElement ( 'div' )
85- instance . querySelectorAll = ( ) => [ el ]
86- el . closest = ( ) => null
87- el . getAttribute = ( ) => 'click:bind-test-element#foo'
88- chai . spy . on ( el , 'addEventListener' )
68+ el . getAttribute ( 'data-action' , 'click:bind-test-element#foo' )
69+ const container = document . createElement ( 'div' )
70+ container . append ( instance , el )
8971 bind ( instance )
90- expect ( el . addEventListener ) . to . have . not . been . called ( )
72+ el . click ( )
73+ expect ( instance . foo ) . to . have . not . been . called ( )
9174 } )
9275
9376 it ( 'does not bind elements whose data-action does not match controller tagname' , ( ) => {
9477 const instance = document . createElement ( 'bind-test-element' )
9578 chai . spy . on ( instance , 'foo' )
9679 const el = document . createElement ( 'div' )
97- instance . querySelectorAll = ( ) => [ el ]
98- el . closest = ( ) => null
99- el . getAttribute = ( ) => 'click:other-controller#foo'
100- chai . spy . on ( el , 'addEventListener' )
80+ el . setAttribute ( 'data-action' , 'click:other-controller#foo' )
81+ instance . appendChild ( el )
10182 bind ( instance )
102- expect ( el . addEventListener ) . to . have . not . been . called ( )
83+ expect ( instance . foo ) . to . have . not . been . called ( )
84+ el . click ( )
85+ expect ( instance . foo ) . to . have . not . been . called ( )
10386 } )
10487
10588 it ( 'does not bind methods that dont exist' , ( ) => {
10689 const instance = document . createElement ( 'bind-test-element' )
10790 chai . spy . on ( instance , 'foo' )
10891 const el = document . createElement ( 'div' )
109- instance . querySelectorAll = ( ) => [ el ]
110- el . closest = ( ) => instance
111- el . getAttribute = ( ) => 'click:bind-test-element#frob'
112- chai . spy . on ( el , 'addEventListener' )
92+ el . setAttribute ( 'data-action' , 'click:bind-test-element#frob' )
93+ instance . appendChild ( el )
11394 bind ( instance )
114- expect ( el . addEventListener ) . to . have . not . been . called ( )
95+ el . click ( )
96+ expect ( instance . foo ) . to . have . not . been . called ( )
11597 } )
11698
11799 it ( 'can bind multiple event types' , ( ) => {
118100 const instance = document . createElement ( 'bind-test-element' )
119101 chai . spy . on ( instance , 'foo' )
120102 const el = document . createElement ( 'div' )
121- instance . querySelectorAll = ( ) => [ el ]
122- el . closest = ( ) => instance
123- el . getAttribute = ( ) => 'click:bind-test-element#foo submit:bind-test-element#foo'
124- chai . spy . on ( el , 'addEventListener' )
103+ el . setAttribute ( 'data-action' , 'click:bind-test-element#foo submit:bind-test-element#foo' )
104+ instance . appendChild ( el )
125105 bind ( instance )
126- expect ( el . addEventListener ) . to . have . been . called ( 2 )
127- expect ( el . addEventListener ) . to . be . first . called . with ( 'click' )
128- expect ( el . addEventListener ) . to . be . second . called . with ( 'submit' )
129- const { calls} = el . addEventListener . __spy
130106 expect ( instance . foo ) . to . have . not . been . called ( )
131- calls [ 0 ] [ 1 ] ( 'a' )
132- expect ( instance . foo ) . to . have . been . called . once . with ( 'a' )
133- calls [ 1 ] [ 1 ] ( 'b' )
134- expect ( instance . foo ) . to . have . been . called . twice . second . with ( 'b' )
107+ el . dispatchEvent ( new CustomEvent ( 'click' ) )
108+ expect ( instance . foo ) . to . have . been . called . exactly ( 1 )
109+ el . dispatchEvent ( new CustomEvent ( 'submit' ) )
110+ expect ( instance . foo ) . to . have . been . called . exactly ( 2 )
111+ const calls = instance . foo . __spy . calls
112+ expect ( calls ) . to . have . nested . property ( '[0][0].type' , 'click' )
113+ expect ( calls ) . to . have . nested . property ( '[1][0].type' , 'submit' )
135114 } )
136115
137116 it ( 'can bind multiple elements to the same event' , ( ) => {
138117 const instance = document . createElement ( 'bind-test-element' )
139118 chai . spy . on ( instance , 'foo' )
140119 const el1 = document . createElement ( 'div' )
141120 const el2 = document . createElement ( 'div' )
142- instance . querySelectorAll = ( ) => [ el1 , el2 ]
143- el1 . closest = ( ) => instance
144- el2 . closest = ( ) => instance
145- el1 . getAttribute = ( ) => 'click:bind-test-element#foo'
146- el2 . getAttribute = ( ) => 'submit:bind-test-element#foo'
147- chai . spy . on ( el1 , 'addEventListener' )
148- chai . spy . on ( el2 , 'addEventListener' )
121+ el1 . setAttribute ( 'data-action' , 'click:bind-test-element#foo' )
122+ el2 . setAttribute ( 'data-action' , 'submit:bind-test-element#foo' )
123+ instance . append ( el1 , el2 )
149124 bind ( instance )
150- expect ( el1 . addEventListener ) . to . be . called . once . with ( 'click' )
151- expect ( el2 . addEventListener ) . to . be . called . once . with ( 'submit' )
152125 expect ( instance . foo ) . to . have . not . been . called ( )
153- el1 . addEventListener . __spy . calls [ 0 ] [ 1 ] ( 'a' )
154- expect ( instance . foo ) . to . have . been . called . once . with ( 'a' )
155- el2 . addEventListener . __spy . calls [ 0 ] [ 1 ] ( 'b' )
156- expect ( instance . foo ) . to . have . been . called . twice . second . with ( 'b' )
126+ el1 . click ( )
127+ expect ( instance . foo ) . to . have . been . called . exactly ( 1 )
128+ el2 . dispatchEvent ( new CustomEvent ( 'submit' ) )
129+ expect ( instance . foo ) . to . have . been . called . exactly ( 2 )
157130 } )
158131
159132 describe ( 'listenForBind' , ( ) => {
0 commit comments