x-on.spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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('.capture modifier',
  90. html`
  91. <div x-data="{ foo: 'bar' }">
  92. <button @click.capture="foo = 'baz'">
  93. <h1>h1</h1>
  94. <h2 @click="foo = 'bob'">h2</h2>
  95. </button>
  96. </div>
  97. `,
  98. ({ get }) => {
  99. get('div').should(haveData('foo', 'bar'))
  100. get('h2').click()
  101. get('div').should(haveData('foo', 'bob'))
  102. }
  103. )
  104. test('.self modifier',
  105. html`
  106. <div x-data="{ foo: 'bar' }">
  107. <h1 x-on:click.self="foo = 'baz'" id="selfTarget">
  108. content
  109. <button>click</button>
  110. content
  111. </h1>
  112. <span x-text="foo"></span>
  113. </div>
  114. `,
  115. ({ get }) => {
  116. get('span').should(haveText('bar'))
  117. get('button').click()
  118. get('span').should(haveText('bar'))
  119. get('h1').click()
  120. get('span').should(haveText('baz'))
  121. }
  122. )
  123. test('.prevent modifier',
  124. html`
  125. <div x-data="{}">
  126. <input type="checkbox" x-on:click.prevent>
  127. </div>
  128. `,
  129. ({ get }) => {
  130. get('input').check()
  131. get('input').should(notBeChecked())
  132. }
  133. )
  134. test('.window modifier',
  135. html`
  136. <div x-data="{ foo: 'bar' }">
  137. <div x-on:click.window="foo = 'baz'"></div>
  138. <span x-text="foo"></span>
  139. </div>
  140. `,
  141. ({ get }) => {
  142. get('span').should(haveText('bar'))
  143. get('span').click()
  144. get('span').should(haveText('baz'))
  145. }
  146. )
  147. test('expressions can start with if',
  148. html`
  149. <div x-data="{ foo: 'bar' }">
  150. <button @click="if (foo === 'bar') foo = 'baz'">click</button>
  151. <span x-text="foo"></span>
  152. </div>
  153. `,
  154. ({ get }) => {
  155. get('span').should(haveText('bar'))
  156. get('button').click()
  157. get('span').should(haveText('baz'))
  158. }
  159. )
  160. test('unbind global event handler when element is removed',
  161. html`
  162. <div x-data="{ count: 0 }">
  163. <div x-on:click.window="count++" x-ref="rmMe"></div>
  164. <button @click="$refs.rmMe.remove()">click</button>
  165. <span x-text="count"></span>
  166. </div>
  167. `,
  168. ({ get }) => {
  169. get('button').click()
  170. get('span').click()
  171. get('span').should(haveText('1'))
  172. }
  173. )
  174. test('.document modifier',
  175. html`
  176. <div x-data="{ foo: 'bar' }">
  177. <div x-on:click.document="foo = 'baz'"></div>
  178. <span x-text="foo"></span>
  179. </div>
  180. `,
  181. ({ get }) => {
  182. get('span').should(haveText('bar'))
  183. get('span').click()
  184. get('span').should(haveText('baz'))
  185. }
  186. )
  187. test('.once modifier',
  188. html`
  189. <div x-data="{ count: 0 }">
  190. <button x-on:click.once="count = count+1"></button>
  191. <span x-text="count"></span>
  192. </div>
  193. `,
  194. ({ get }) => {
  195. get('span').should(haveText('0'))
  196. get('button').click()
  197. get('span').should(haveText('1'))
  198. get('button').click()
  199. get('span').should(haveText('1'))
  200. }
  201. )
  202. test('.once modifier with @keyup',
  203. html`
  204. <div x-data="{ count: 0 }">
  205. <input type="text" x-on:keyup.once="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('o')
  214. get('span').should(haveText('1'))
  215. }
  216. )
  217. test('.once modifier with @keyup and specified key',
  218. html`
  219. <div x-data="{ count: 0 }">
  220. <input type="text" x-on:keyup.enter.once="count = count+1">
  221. <span x-text="count"></span>
  222. </div>
  223. `,
  224. ({ get }) => {
  225. get('span').should(haveText('0'))
  226. get('input').type('f')
  227. get('span').should(haveText('0'))
  228. get('input').type('{enter}')
  229. get('span').should(haveText('1'))
  230. get('input').type('{enter}')
  231. get('span').should(haveText('1'))
  232. }
  233. )
  234. test('.debounce modifier',
  235. html`
  236. <div x-data="{ count: 0 }">
  237. <input x-on:input.debounce="count = count+1">
  238. <span x-text="count"></span>
  239. </div>
  240. `,
  241. ({ get }) => {
  242. get('span').should(haveText('0'))
  243. get('input').type('f')
  244. get('span').should(haveText('1'))
  245. get('input').type('ffffffffffff')
  246. get('span').should(haveText('2'))
  247. }
  248. )
  249. test('.throttle modifier',
  250. html`
  251. <div x-data="{ count: 0 }">
  252. <input x-on:keyup.throttle.504ms="count = count+1">
  253. <span x-text="count"></span>
  254. </div>
  255. `,
  256. ({ get }) => {
  257. get('span').should(haveText('0'))
  258. get('input').type('f')
  259. get('span').should(haveText('1'))
  260. get('input').type('ffffffffffff')
  261. get('span').should(haveText('1'))
  262. }
  263. )
  264. test('keydown modifiers',
  265. html`
  266. <div x-data="{ count: 0 }">
  267. <input type="text"
  268. x-on:keydown="count++"
  269. x-on:keydown.enter="count++"
  270. x-on:keydown.space="count++"
  271. x-on:keydown.up="count++"
  272. x-on:keydown.down="count++"
  273. x-on:keydown.right="count++"
  274. x-on:keydown.left="count++"
  275. x-on:keydown.cmd="count++"
  276. x-on:keydown.meta="count++"
  277. x-on:keydown.escape="count++"
  278. x-on:keydown.esc="count++"
  279. x-on:keydown.ctrl="count++"
  280. x-on:keydown.slash="count++"
  281. x-on:keydown.period="count++"
  282. x-on:keydown.equal="count++"
  283. >
  284. <span x-text="count"></span>
  285. </div>
  286. `,
  287. ({ get }) => {
  288. get('span').should(haveText('0'))
  289. get('input').type('f')
  290. get('span').should(haveText('1'))
  291. get('input').type('{enter}')
  292. get('span').should(haveText('3'))
  293. get('input').type(' ')
  294. get('span').should(haveText('5'))
  295. get('input').type('{leftarrow}')
  296. get('span').should(haveText('7'))
  297. get('input').type('{rightarrow}')
  298. get('span').should(haveText('9'))
  299. get('input').type('{uparrow}')
  300. get('span').should(haveText('11'))
  301. get('input').type('{downarrow}')
  302. get('span').should(haveText('13'))
  303. get('input').type('{meta}')
  304. get('span').should(haveText('16'))
  305. get('input').type('{esc}')
  306. get('span').should(haveText('19'))
  307. get('input').type('{ctrl}')
  308. get('span').should(haveText('21'))
  309. get('input').type('/')
  310. get('span').should(haveText('23'))
  311. get('input').type('=')
  312. get('span').should(haveText('25'))
  313. get('input').type('.')
  314. get('span').should(haveText('27'))
  315. }
  316. )
  317. test('discerns between space minus underscore',
  318. html`
  319. <div x-data="{ count: 0 }">
  320. <input id="space" type="text" x-on:keydown.space="count++" />
  321. <input id="minus" type="text" x-on:keydown.-="count++" />
  322. <input id="underscore" type="text" x-on:keydown._="count++" />
  323. <span x-text="count"></span>
  324. </div>
  325. `,
  326. ({get}) => {
  327. get('span').should(haveText('0'))
  328. get('#space').type(' ')
  329. get('span').should(haveText('1'))
  330. get('#space').type('-')
  331. get('span').should(haveText('1'))
  332. get('#minus').type('-')
  333. get('span').should(haveText('2'))
  334. get('#minus').type(' ')
  335. get('span').should(haveText('2'))
  336. get('#underscore').type('_')
  337. get('span').should(haveText('3'))
  338. get('#underscore').type(' ')
  339. get('span').should(haveText('3'))
  340. })
  341. test('keydown combo modifiers',
  342. html`
  343. <div x-data="{ count: 0 }">
  344. <input type="text" x-on:keydown.cmd.enter="count++">
  345. <span x-text="count"></span>
  346. </div>
  347. `,
  348. ({ get }) => {
  349. get('span').should(haveText('0'))
  350. get('input').type('f')
  351. get('span').should(haveText('0'))
  352. get('input').type('{cmd}{enter}')
  353. get('span').should(haveText('1'))
  354. }
  355. )
  356. test('keydown with specified key and stop modifier only stops for specified key',
  357. html`
  358. <div x-data="{ count: 0 }">
  359. <article x-on:keydown="count++">
  360. <input type="text" x-on:keydown.enter.stop>
  361. </article>
  362. <span x-text="count"></span>
  363. </div>
  364. `,
  365. ({ get }) => {
  366. get('span').should(haveText('0'))
  367. get('input').type('f')
  368. get('span').should(haveText('1'))
  369. get('input').type('{enter}')
  370. get('span').should(haveText('1'))
  371. }
  372. )
  373. test('@click.away',
  374. html`
  375. <div x-data="{ foo: 'bar' }">
  376. <h1 @click.away="foo = 'baz'">h1</h1>
  377. <h2>h2</h2>
  378. <span x-text="foo"></span>
  379. </div>
  380. `,
  381. ({ get }) => {
  382. get('span').should(haveText('bar'))
  383. get('h1').click()
  384. get('span').should(haveText('bar'))
  385. get('h2').click()
  386. get('span').should(haveText('baz'))
  387. }
  388. )
  389. test('@click.away with x-show (prevent race condition)',
  390. html`
  391. <div x-data="{ show: false }">
  392. <button @click="show = true">Show</button>
  393. <h1 x-show="show" @click.away="show = false">h1</h1>
  394. <h2>h2</h2>
  395. </div>
  396. `,
  397. ({ get }) => {
  398. get('h1').should(notBeVisible())
  399. get('button').click()
  400. get('h1').should(beVisible())
  401. }
  402. )
  403. test('event with colon',
  404. html`
  405. <div x-data="{ foo: 'bar' }">
  406. <div x-on:my:event.document="foo = 'baz'"></div>
  407. <button @click="document.dispatchEvent(new CustomEvent('my:event', { bubbles: true }))">click</button>
  408. <span x-text="foo"></span>
  409. </div>
  410. `,
  411. ({ get }) => {
  412. get('span').should(haveText('bar'))
  413. get('button').click()
  414. get('span').should(haveText('baz'))
  415. }
  416. )
  417. test('event instance can be passed to method reference',
  418. html`
  419. <div x-data="{ foo: 'bar', changeFoo(e) { this.foo = e.target.id } }">
  420. <button x-on:click="changeFoo" id="baz"></button>
  421. <span x-text="foo"></span>
  422. </div>
  423. `,
  424. ({ get }) => {
  425. get('span').should(haveText('bar'))
  426. get('button').click()
  427. get('span').should(haveText('baz'))
  428. }
  429. )
  430. test('.camel modifier correctly binds event listener',
  431. html`
  432. <div x-data="{ foo: 'bar' }" x-on:event-name.camel="foo = 'baz'">
  433. <button x-on:click="$dispatch('eventName')"></button>
  434. <span x-text="foo"></span>
  435. </div>
  436. `,
  437. ({ get }) => {
  438. get('span').should(haveText('bar'))
  439. get('button').click()
  440. get('span').should(haveText('baz'))
  441. }
  442. )
  443. test('.camel modifier correctly binds event listener with namespace',
  444. html`
  445. <div x-data="{ foo: 'bar' }" x-on:ns:event-name.camel="foo = 'baz'">
  446. <button x-on:click="$dispatch('ns:eventName')"></button>
  447. <span x-text="foo"></span>
  448. </div>
  449. `,
  450. ({ get }) => {
  451. get('span').should(haveText('bar'))
  452. get('button').click()
  453. get('span').should(haveText('baz'))
  454. }
  455. )
  456. test('.dot modifier correctly binds event listener',
  457. html`
  458. <div x-data="{ foo: 'bar' }" x-on:event-name.dot="foo = 'baz'">
  459. <button x-on:click="$dispatch('event.name')"></button>
  460. <span x-text="foo"></span>
  461. </div>
  462. `,
  463. ({ get }) => {
  464. get('span').should(haveText('bar'))
  465. get('button').click()
  466. get('span').should(haveText('baz'))
  467. }
  468. )
  469. test('.dot modifier correctly binds event listener with namespace',
  470. html`
  471. <div x-data="{ foo: 'bar' }" x-on:ns:event-name.dot="foo = 'baz'">
  472. <button x-on:click="$dispatch('ns:event.name')"></button>
  473. <span x-text="foo"></span>
  474. </div>
  475. `,
  476. ({ get }) => {
  477. get('span').should(haveText('bar'))
  478. get('button').click()
  479. get('span').should(haveText('baz'))
  480. }
  481. )
  482. test('handles await in handlers with invalid right hand expressions',
  483. html`
  484. <div x-data="{ text: 'original' }">
  485. <button @click="let value = 'new string'; text = await Promise.resolve(value)"></button>
  486. <span x-text="text"></span>
  487. </div>
  488. `,
  489. ({ get }) => {
  490. get('span').should(haveText('original'))
  491. get('button').click()
  492. get('span').should(haveText('new string'))
  493. }
  494. )