on.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import Vue from 'vue'
  2. describe('Directive v-on', () => {
  3. let vm, spy, spy2, el
  4. beforeEach(() => {
  5. spy = jasmine.createSpy()
  6. spy2 = jasmine.createSpy()
  7. el = document.createElement('div')
  8. document.body.appendChild(el)
  9. })
  10. afterEach(() => {
  11. document.body.removeChild(vm.$el)
  12. })
  13. it('should bind event to a method', () => {
  14. vm = new Vue({
  15. el,
  16. template: '<div v-on:click="foo"></div>',
  17. methods: { foo: spy }
  18. })
  19. triggerEvent(vm.$el, 'click')
  20. expect(spy.calls.count()).toBe(1)
  21. const args = spy.calls.allArgs()
  22. const event = args[0] && args[0][0] || {}
  23. expect(event.type).toBe('click')
  24. })
  25. it('should bind event to a inline statement', () => {
  26. vm = new Vue({
  27. el,
  28. template: '<div v-on:click="foo(1,2,3,$event)"></div>',
  29. methods: { foo: spy }
  30. })
  31. triggerEvent(vm.$el, 'click')
  32. expect(spy.calls.count()).toBe(1)
  33. const args = spy.calls.allArgs()
  34. const firstArgs = args[0]
  35. expect(firstArgs.length).toBe(4)
  36. expect(firstArgs[0]).toBe(1)
  37. expect(firstArgs[1]).toBe(2)
  38. expect(firstArgs[2]).toBe(3)
  39. expect(firstArgs[3].type).toBe('click')
  40. })
  41. it('should support inline function expression', () => {
  42. const spy = jasmine.createSpy()
  43. vm = new Vue({
  44. el,
  45. template: `<div class="test" @click="function (e) { log(e.target.className) }"></div>`,
  46. methods: {
  47. log: spy
  48. }
  49. }).$mount()
  50. triggerEvent(vm.$el, 'click')
  51. expect(spy).toHaveBeenCalledWith('test')
  52. })
  53. it('should support shorthand', () => {
  54. vm = new Vue({
  55. el,
  56. template: '<a href="#test" @click.prevent="foo"></a>',
  57. methods: { foo: spy }
  58. })
  59. triggerEvent(vm.$el, 'click')
  60. expect(spy.calls.count()).toBe(1)
  61. })
  62. it('should support stop propagation', () => {
  63. vm = new Vue({
  64. el,
  65. template: `
  66. <div @click.stop="foo"></div>
  67. `,
  68. methods: { foo: spy }
  69. })
  70. const hash = window.location.hash
  71. triggerEvent(vm.$el, 'click')
  72. expect(window.location.hash).toBe(hash)
  73. })
  74. it('should support prevent default', () => {
  75. vm = new Vue({
  76. el,
  77. template: `
  78. <div @click="bar">
  79. <div @click.stop="foo"></div>
  80. </div>
  81. `,
  82. methods: { foo: spy, bar: spy2 }
  83. })
  84. triggerEvent(vm.$el.firstChild, 'click')
  85. expect(spy).toHaveBeenCalled()
  86. expect(spy2).not.toHaveBeenCalled()
  87. })
  88. it('should support capture', () => {
  89. const callOrder = []
  90. vm = new Vue({
  91. el,
  92. template: `
  93. <div @click.capture="foo">
  94. <div @click="bar"></div>
  95. </div>
  96. `,
  97. methods: {
  98. foo () { callOrder.push(1) },
  99. bar () { callOrder.push(2) }
  100. }
  101. })
  102. triggerEvent(vm.$el.firstChild, 'click')
  103. expect(callOrder.toString()).toBe('1,2')
  104. })
  105. it('should support once', () => {
  106. vm = new Vue({
  107. el,
  108. template: `
  109. <div @click.once="foo">
  110. </div>
  111. `,
  112. methods: { foo: spy }
  113. })
  114. triggerEvent(vm.$el, 'click')
  115. expect(spy.calls.count()).toBe(1)
  116. triggerEvent(vm.$el, 'click')
  117. expect(spy.calls.count()).toBe(1) // should no longer trigger
  118. })
  119. it('should support capture and once', () => {
  120. const callOrder = []
  121. vm = new Vue({
  122. el,
  123. template: `
  124. <div @click.capture.once="foo">
  125. <div @click="bar"></div>
  126. </div>
  127. `,
  128. methods: {
  129. foo () { callOrder.push(1) },
  130. bar () { callOrder.push(2) }
  131. }
  132. })
  133. triggerEvent(vm.$el.firstChild, 'click')
  134. expect(callOrder.toString()).toBe('1,2')
  135. triggerEvent(vm.$el.firstChild, 'click')
  136. expect(callOrder.toString()).toBe('1,2,2')
  137. })
  138. it('should support keyCode', () => {
  139. vm = new Vue({
  140. el,
  141. template: `<input @keyup.enter="foo">`,
  142. methods: { foo: spy }
  143. })
  144. triggerEvent(vm.$el, 'keyup', e => {
  145. e.keyCode = 13
  146. })
  147. expect(spy).toHaveBeenCalled()
  148. })
  149. it('should support number keyCode', () => {
  150. vm = new Vue({
  151. el,
  152. template: `<input @keyup.13="foo">`,
  153. methods: { foo: spy }
  154. })
  155. triggerEvent(vm.$el, 'keyup', e => {
  156. e.keyCode = 13
  157. })
  158. expect(spy).toHaveBeenCalled()
  159. })
  160. it('should support custom keyCode', () => {
  161. Vue.config.keyCodes.test = 1
  162. vm = new Vue({
  163. el,
  164. template: `<input @keyup.test="foo">`,
  165. methods: { foo: spy }
  166. })
  167. triggerEvent(vm.$el, 'keyup', e => {
  168. e.keyCode = 1
  169. })
  170. expect(spy).toHaveBeenCalled()
  171. })
  172. it('should override build-in keyCode', () => {
  173. Vue.config.keyCodes.up = [1, 87]
  174. vm = new Vue({
  175. el,
  176. template: `<input @keyup.up="foo" @keyup.down="foo">`,
  177. methods: { foo: spy }
  178. })
  179. triggerEvent(vm.$el, 'keyup', e => {
  180. e.keyCode = 87
  181. })
  182. expect(spy).toHaveBeenCalled()
  183. triggerEvent(vm.$el, 'keyup', e => {
  184. e.keyCode = 1
  185. })
  186. expect(spy).toHaveBeenCalledTimes(2)
  187. // should not affect build-in down keycode
  188. triggerEvent(vm.$el, 'keyup', e => {
  189. e.keyCode = 40
  190. })
  191. expect(spy).toHaveBeenCalledTimes(3)
  192. })
  193. it('should bind to a child component', () => {
  194. Vue.component('bar', {
  195. template: '<span>Hello</span>'
  196. })
  197. vm = new Vue({
  198. el,
  199. template: '<bar @custom="foo"></bar>',
  200. methods: { foo: spy }
  201. })
  202. vm.$children[0].$emit('custom', 'foo', 'bar')
  203. expect(spy).toHaveBeenCalledWith('foo', 'bar')
  204. })
  205. it('should be able to bind native events for a child component', () => {
  206. Vue.component('bar', {
  207. template: '<span>Hello</span>'
  208. })
  209. vm = new Vue({
  210. el,
  211. template: '<bar @click.native="foo"></bar>',
  212. methods: { foo: spy }
  213. })
  214. vm.$children[0].$emit('click')
  215. expect(spy).not.toHaveBeenCalled()
  216. triggerEvent(vm.$children[0].$el, 'click')
  217. expect(spy).toHaveBeenCalled()
  218. })
  219. it('.once modifier should work with child components', () => {
  220. Vue.component('bar', {
  221. template: '<span>Hello</span>'
  222. })
  223. vm = new Vue({
  224. el,
  225. template: '<bar @custom.once="foo"></bar>',
  226. methods: { foo: spy }
  227. })
  228. vm.$children[0].$emit('custom')
  229. expect(spy.calls.count()).toBe(1)
  230. vm.$children[0].$emit('custom')
  231. expect(spy.calls.count()).toBe(1) // should not be called again
  232. })
  233. it('remove listener', done => {
  234. const spy2 = jasmine.createSpy('remove listener')
  235. vm = new Vue({
  236. el,
  237. methods: { foo: spy, bar: spy2 },
  238. data: {
  239. ok: true
  240. },
  241. render (h) {
  242. return this.ok
  243. ? h('input', { on: { click: this.foo }})
  244. : h('input', { on: { input: this.bar }})
  245. }
  246. })
  247. triggerEvent(vm.$el, 'click')
  248. expect(spy.calls.count()).toBe(1)
  249. expect(spy2.calls.count()).toBe(0)
  250. vm.ok = false
  251. waitForUpdate(() => {
  252. triggerEvent(vm.$el, 'click')
  253. expect(spy.calls.count()).toBe(1) // should no longer trigger
  254. triggerEvent(vm.$el, 'input')
  255. expect(spy2.calls.count()).toBe(1)
  256. }).then(done)
  257. })
  258. it('remove capturing listener', done => {
  259. const spy2 = jasmine.createSpy('remove listener')
  260. vm = new Vue({
  261. el,
  262. methods: { foo: spy, bar: spy2, stopped (ev) { ev.stopPropagation() } },
  263. data: {
  264. ok: true
  265. },
  266. render (h) {
  267. return this.ok
  268. ? h('div', { on: { '!click': this.foo }}, [h('div', { on: { click: this.stopped }})])
  269. : h('div', { on: { mouseOver: this.bar }}, [h('div')])
  270. }
  271. })
  272. triggerEvent(vm.$el.firstChild, 'click')
  273. expect(spy.calls.count()).toBe(1)
  274. expect(spy2.calls.count()).toBe(0)
  275. vm.ok = false
  276. waitForUpdate(() => {
  277. triggerEvent(vm.$el.firstChild, 'click')
  278. expect(spy.calls.count()).toBe(1) // should no longer trigger
  279. triggerEvent(vm.$el, 'mouseOver')
  280. expect(spy2.calls.count()).toBe(1)
  281. }).then(done)
  282. })
  283. it('remove once listener', done => {
  284. const spy2 = jasmine.createSpy('remove listener')
  285. vm = new Vue({
  286. el,
  287. methods: { foo: spy, bar: spy2 },
  288. data: {
  289. ok: true
  290. },
  291. render (h) {
  292. return this.ok
  293. ? h('input', { on: { '~click': this.foo }})
  294. : h('input', { on: { input: this.bar }})
  295. }
  296. })
  297. triggerEvent(vm.$el, 'click')
  298. expect(spy.calls.count()).toBe(1)
  299. triggerEvent(vm.$el, 'click')
  300. expect(spy.calls.count()).toBe(1) // should no longer trigger
  301. expect(spy2.calls.count()).toBe(0)
  302. vm.ok = false
  303. waitForUpdate(() => {
  304. triggerEvent(vm.$el, 'click')
  305. expect(spy.calls.count()).toBe(1) // should no longer trigger
  306. triggerEvent(vm.$el, 'input')
  307. expect(spy2.calls.count()).toBe(1)
  308. }).then(done)
  309. })
  310. it('remove capturing and once listener', done => {
  311. const spy2 = jasmine.createSpy('remove listener')
  312. vm = new Vue({
  313. el,
  314. methods: { foo: spy, bar: spy2, stopped (ev) { ev.stopPropagation() } },
  315. data: {
  316. ok: true
  317. },
  318. render (h) {
  319. return this.ok
  320. ? h('div', { on: { '~!click': this.foo }}, [h('div', { on: { click: this.stopped }})])
  321. : h('div', { on: { mouseOver: this.bar }}, [h('div')])
  322. }
  323. })
  324. triggerEvent(vm.$el.firstChild, 'click')
  325. expect(spy.calls.count()).toBe(1)
  326. triggerEvent(vm.$el.firstChild, 'click')
  327. expect(spy.calls.count()).toBe(1) // should no longer trigger
  328. expect(spy2.calls.count()).toBe(0)
  329. vm.ok = false
  330. waitForUpdate(() => {
  331. triggerEvent(vm.$el.firstChild, 'click')
  332. expect(spy.calls.count()).toBe(1) // should no longer trigger
  333. triggerEvent(vm.$el, 'mouseOver')
  334. expect(spy2.calls.count()).toBe(1)
  335. }).then(done)
  336. })
  337. it('remove listener on child component', done => {
  338. const spy2 = jasmine.createSpy('remove listener')
  339. vm = new Vue({
  340. el,
  341. methods: { foo: spy, bar: spy2 },
  342. data: {
  343. ok: true
  344. },
  345. components: {
  346. test: {
  347. template: '<div></div>'
  348. }
  349. },
  350. render (h) {
  351. return this.ok
  352. ? h('test', { on: { foo: this.foo }})
  353. : h('test', { on: { bar: this.bar }})
  354. }
  355. })
  356. vm.$children[0].$emit('foo')
  357. expect(spy.calls.count()).toBe(1)
  358. expect(spy2.calls.count()).toBe(0)
  359. vm.ok = false
  360. waitForUpdate(() => {
  361. vm.$children[0].$emit('foo')
  362. expect(spy.calls.count()).toBe(1) // should no longer trigger
  363. vm.$children[0].$emit('bar')
  364. expect(spy2.calls.count()).toBe(1)
  365. }).then(done)
  366. })
  367. it('warn missing handlers', () => {
  368. vm = new Vue({
  369. el,
  370. data: { none: null },
  371. template: `<div @click="none"></div>`
  372. })
  373. expect(`Invalid handler for event "click": got null`).toHaveBeenWarned()
  374. expect(() => {
  375. triggerEvent(vm.$el, 'click')
  376. }).not.toThrow()
  377. })
  378. })