1
0

x-on.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. import { beChecked, notBeChecked, haveAttribute, haveData, haveText, test, beVisible, notBeVisible, html } from '../../utils'
  2. test('data modified in event listener updates affected attribute bindings',
  3. html`
  4. <div x-data="{ foo: 'bar' }">
  5. <button x-on:click="foo = 'baz'"></button>
  6. <span x-bind:foo="foo"></span>
  7. </div>
  8. `,
  9. ({ get }) => {
  10. get('span').should(haveAttribute('foo', 'bar'))
  11. get('button').click()
  12. get('span').should(haveAttribute('foo', 'baz'))
  13. }
  14. )
  15. test('can call a method without parenthesis',
  16. html`
  17. <div x-data="{ foo: 'bar', baz($event) { this.foo = $event.target.dataset.bob } }">
  18. <button x-on:click="baz" data-bob="lob"></button>
  19. <span x-text="foo"></span>
  20. </div>
  21. `,
  22. ({ get }) => {
  23. get('span').should(haveText('bar'))
  24. get('button').click()
  25. get('span').should(haveText('lob'))
  26. }
  27. )
  28. test('event object is not passed if other params are present',
  29. html`
  30. <div x-data="{ foo: 'bar', baz(word) { this.foo = word } }">
  31. <button x-on:click="baz('foo')" data-bob="lob"></button>
  32. <span x-text="foo"></span>
  33. </div>
  34. `,
  35. ({ get }) => {
  36. get('span').should(haveText('bar'))
  37. get('button').click()
  38. get('span').should(haveText('foo'))
  39. }
  40. )
  41. test('nested data modified in event listener updates affected attribute bindings',
  42. html`
  43. <div x-data="{ nested: { foo: 'bar' } }">
  44. <button x-on:click="nested.foo = 'baz'"></button>
  45. <span x-bind:foo="nested.foo"></span>
  46. </div>
  47. `,
  48. ({ get }) => {
  49. get('span').should(haveAttribute('foo', 'bar'))
  50. get('button').click()
  51. get('span').should(haveAttribute('foo', 'baz'))
  52. }
  53. )
  54. test('.passive modifier should disable e.preventDefault()',
  55. html`
  56. <div x-data="{ defaultPrevented: null }">
  57. <button
  58. x-on:mousedown.passive="
  59. $event.preventDefault();
  60. defaultPrevented = $event.defaultPrevented;
  61. "
  62. >
  63. <span></span>
  64. </button>
  65. </div>
  66. `,
  67. ({ get }) => {
  68. get('button').click()
  69. get('div').should(haveData('defaultPrevented', false))
  70. }
  71. )
  72. test('.stop modifier',
  73. html`
  74. <div x-data="{ foo: 'bar' }">
  75. <button x-on:click="foo = 'baz'">
  76. <h1>h1</h1>
  77. <h2 @click.stop>h2</h2>
  78. </button>
  79. </div>
  80. `,
  81. ({ get }) => {
  82. get('div').should(haveData('foo', 'bar'))
  83. get('h2').click()
  84. get('div').should(haveData('foo', 'bar'))
  85. get('h1').click()
  86. get('div').should(haveData('foo', 'baz'))
  87. }
  88. )
  89. test('.self modifier',
  90. html`
  91. <div x-data="{ foo: 'bar' }">
  92. <h1 x-on:click.self="foo = 'baz'" id="selfTarget">
  93. content
  94. <button>click</button>
  95. content
  96. </h1>
  97. <span x-text="foo"></span>
  98. </div>
  99. `,
  100. ({ get }) => {
  101. get('span').should(haveText('bar'))
  102. get('button').click()
  103. get('span').should(haveText('bar'))
  104. get('h1').click()
  105. get('span').should(haveText('baz'))
  106. }
  107. )
  108. test('.prevent modifier',
  109. html`
  110. <div x-data="{}">
  111. <input type="checkbox" x-on:click.prevent>
  112. </div>
  113. `,
  114. ({ get }) => {
  115. get('input').check()
  116. get('input').should(notBeChecked())
  117. }
  118. )
  119. test('.window modifier',
  120. html`
  121. <div x-data="{ foo: 'bar' }">
  122. <div x-on:click.window="foo = 'baz'"></div>
  123. <span x-text="foo"></span>
  124. </div>
  125. `,
  126. ({ get }) => {
  127. get('span').should(haveText('bar'))
  128. get('span').click()
  129. get('span').should(haveText('baz'))
  130. }
  131. )
  132. test('expressions can start with if',
  133. html`
  134. <div x-data="{ foo: 'bar' }">
  135. <button @click="if (foo === 'bar') foo = 'baz'">click</button>
  136. <span x-text="foo"></span>
  137. </div>
  138. `,
  139. ({ get }) => {
  140. get('span').should(haveText('bar'))
  141. get('button').click()
  142. get('span').should(haveText('baz'))
  143. }
  144. )
  145. test('unbind global event handler when element is removed',
  146. html`
  147. <div x-data="{ count: 0 }">
  148. <div x-on:click.window="count++" x-ref="rmMe"></div>
  149. <button @click="$refs.rmMe.remove()">click</button>
  150. <span x-text="count"></span>
  151. </div>
  152. `,
  153. ({ get }) => {
  154. get('button').click()
  155. get('span').click()
  156. get('span').should(haveText('1'))
  157. }
  158. )
  159. test('.document modifier',
  160. html`
  161. <div x-data="{ foo: 'bar' }">
  162. <div x-on:click.document="foo = 'baz'"></div>
  163. <span x-text="foo"></span>
  164. </div>
  165. `,
  166. ({ get }) => {
  167. get('span').should(haveText('bar'))
  168. get('span').click()
  169. get('span').should(haveText('baz'))
  170. }
  171. )
  172. test('.once modifier',
  173. html`
  174. <div x-data="{ count: 0 }">
  175. <button x-on:click.once="count = count+1"></button>
  176. <span x-text="count"></span>
  177. </div>
  178. `,
  179. ({ get }) => {
  180. get('span').should(haveText('0'))
  181. get('button').click()
  182. get('span').should(haveText('1'))
  183. get('button').click()
  184. get('span').should(haveText('1'))
  185. }
  186. )
  187. test('.once modifier with @keyup',
  188. html`
  189. <div x-data="{ count: 0 }">
  190. <input type="text" x-on:keyup.once="count = count+1">
  191. <span x-text="count"></span>
  192. </div>
  193. `,
  194. ({ get }) => {
  195. get('span').should(haveText('0'))
  196. get('input').type('f')
  197. get('span').should(haveText('1'))
  198. get('input').type('o')
  199. get('span').should(haveText('1'))
  200. }
  201. )
  202. test('.debounce modifier',
  203. html`
  204. <div x-data="{ count: 0 }">
  205. <input x-on:input.debounce="count = count+1">
  206. <span x-text="count"></span>
  207. </div>
  208. `,
  209. ({ get }) => {
  210. get('span').should(haveText('0'))
  211. get('input').type('f')
  212. get('span').should(haveText('1'))
  213. get('input').type('ffffffffffff')
  214. get('span').should(haveText('2'))
  215. }
  216. )
  217. test('keydown modifiers',
  218. html`
  219. <div x-data="{ count: 0 }">
  220. <input type="text"
  221. x-on:keydown="count++"
  222. x-on:keydown.enter="count++"
  223. x-on:keydown.space="count++"
  224. x-on:keydown.up="count++"
  225. x-on:keydown.down="count++"
  226. x-on:keydown.right="count++"
  227. x-on:keydown.left="count++"
  228. x-on:keydown.cmd="count++"
  229. x-on:keydown.meta="count++"
  230. x-on:keydown.escape="count++"
  231. x-on:keydown.esc="count++"
  232. x-on:keydown.ctrl="count++"
  233. x-on:keydown.slash="count++"
  234. x-on:keydown.period="count++"
  235. x-on:keydown.equal="count++"
  236. >
  237. <span x-text="count"></span>
  238. </div>
  239. `,
  240. ({ get }) => {
  241. get('span').should(haveText('0'))
  242. get('input').type('f')
  243. get('span').should(haveText('1'))
  244. get('input').type('{enter}')
  245. get('span').should(haveText('3'))
  246. get('input').type(' ')
  247. get('span').should(haveText('5'))
  248. get('input').type('{leftarrow}')
  249. get('span').should(haveText('7'))
  250. get('input').type('{rightarrow}')
  251. get('span').should(haveText('9'))
  252. get('input').type('{uparrow}')
  253. get('span').should(haveText('11'))
  254. get('input').type('{downarrow}')
  255. get('span').should(haveText('13'))
  256. get('input').type('{meta}')
  257. get('span').should(haveText('16'))
  258. get('input').type('{esc}')
  259. get('span').should(haveText('19'))
  260. get('input').type('{ctrl}')
  261. get('span').should(haveText('21'))
  262. get('input').type('/')
  263. get('span').should(haveText('23'))
  264. get('input').type('=')
  265. get('span').should(haveText('25'))
  266. get('input').type('.')
  267. get('span').should(haveText('27'))
  268. }
  269. )
  270. test('keydown combo modifiers',
  271. html`
  272. <div x-data="{ count: 0 }">
  273. <input type="text" x-on:keydown.cmd.enter="count++">
  274. <span x-text="count"></span>
  275. </div>
  276. `,
  277. ({ get }) => {
  278. get('span').should(haveText('0'))
  279. get('input').type('f')
  280. get('span').should(haveText('0'))
  281. get('input').type('{cmd}{enter}')
  282. get('span').should(haveText('1'))
  283. }
  284. )
  285. test('keydown with specified key and stop modifier only stops for specified key',
  286. html`
  287. <div x-data="{ count: 0 }">
  288. <article x-on:keydown="count++">
  289. <input type="text" x-on:keydown.enter.stop>
  290. </article>
  291. <span x-text="count"></span>
  292. </div>
  293. `,
  294. ({ get }) => {
  295. get('span').should(haveText('0'))
  296. get('input').type('f')
  297. get('span').should(haveText('1'))
  298. get('input').type('{enter}')
  299. get('span').should(haveText('1'))
  300. }
  301. )
  302. test('@click.away',
  303. html`
  304. <div x-data="{ foo: 'bar' }">
  305. <h1 @click.away="foo = 'baz'">h1</h1>
  306. <h2>h2</h2>
  307. <span x-text="foo"></span>
  308. </div>
  309. `,
  310. ({ get }) => {
  311. get('span').should(haveText('bar'))
  312. get('h1').click()
  313. get('span').should(haveText('bar'))
  314. get('h2').click()
  315. get('span').should(haveText('baz'))
  316. }
  317. )
  318. test('@click.away with x-show (prevent race condition)',
  319. html`
  320. <div x-data="{ show: false }">
  321. <button @click="show = true">Show</button>
  322. <h1 x-show="show" @click.away="show = false">h1</h1>
  323. <h2>h2</h2>
  324. </div>
  325. `,
  326. ({ get }) => {
  327. get('h1').should(notBeVisible())
  328. get('button').click()
  329. get('h1').should(beVisible())
  330. }
  331. )
  332. test('event with colon',
  333. html`
  334. <div x-data="{ foo: 'bar' }">
  335. <div x-on:my:event.document="foo = 'baz'"></div>
  336. <button @click="document.dispatchEvent(new CustomEvent('my:event', { bubbles: true }))">click</button>
  337. <span x-text="foo"></span>
  338. </div>
  339. `,
  340. ({ get }) => {
  341. get('span').should(haveText('bar'))
  342. get('button').click()
  343. get('span').should(haveText('baz'))
  344. }
  345. )
  346. test('event instance can be passed to method reference',
  347. html`
  348. <div x-data="{ foo: 'bar', changeFoo(e) { this.foo = e.target.id } }">
  349. <button x-on:click="changeFoo" id="baz"></button>
  350. <span x-text="foo"></span>
  351. </div>
  352. `,
  353. ({ get }) => {
  354. get('span').should(haveText('bar'))
  355. get('button').click()
  356. get('span').should(haveText('baz'))
  357. }
  358. )
  359. test('.camel modifier correctly binds event listener',
  360. html`
  361. <div x-data="{ foo: 'bar' }" x-on:event-name.camel="foo = 'baz'">
  362. <button x-on:click="$dispatch('eventName')"></button>
  363. <span x-text="foo"></span>
  364. </div>
  365. `,
  366. ({ get }) => {
  367. get('span').should(haveText('bar'))
  368. get('button').click()
  369. get('span').should(haveText('baz'))
  370. }
  371. )
  372. test('.camel modifier correctly binds event listener with namespace',
  373. html`
  374. <div x-data="{ foo: 'bar' }" x-on:ns:event-name.camel="foo = 'baz'">
  375. <button x-on:click="$dispatch('ns:eventName')"></button>
  376. <span x-text="foo"></span>
  377. </div>
  378. `,
  379. ({ get }) => {
  380. get('span').should(haveText('bar'))
  381. get('button').click()
  382. get('span').should(haveText('baz'))
  383. }
  384. )
  385. test('.dot modifier correctly binds event listener',
  386. html`
  387. <div x-data="{ foo: 'bar' }" x-on:event-name.dot="foo = 'baz'">
  388. <button x-on:click="$dispatch('event.name')"></button>
  389. <span x-text="foo"></span>
  390. </div>
  391. `,
  392. ({ get }) => {
  393. get('span').should(haveText('bar'))
  394. get('button').click()
  395. get('span').should(haveText('baz'))
  396. }
  397. )
  398. test('.dot modifier correctly binds event listener with namespace',
  399. html`
  400. <div x-data="{ foo: 'bar' }" x-on:ns:event-name.dot="foo = 'baz'">
  401. <button x-on:click="$dispatch('ns:event.name')"></button>
  402. <span x-text="foo"></span>
  403. </div>
  404. `,
  405. ({ get }) => {
  406. get('span').should(haveText('bar'))
  407. get('button').click()
  408. get('span').should(haveText('baz'))
  409. }
  410. )