componentEmits.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Note: emits and listener fallthrough is tested in
  2. // ./rendererAttrsFallthrough.spec.ts.
  3. import { mockWarn } from '@vue/shared'
  4. import { render, defineComponent, h, nodeOps } from '@vue/runtime-test'
  5. import { isEmitListener } from '../src/componentEmits'
  6. describe('emits option', () => {
  7. mockWarn()
  8. test('trigger both raw event and capitalize handlers', () => {
  9. const Foo = defineComponent({
  10. render() {},
  11. created() {
  12. // the `emit` function is bound on component instances
  13. this.$emit('foo')
  14. this.$emit('bar')
  15. }
  16. })
  17. const onfoo = jest.fn()
  18. const onBar = jest.fn()
  19. const Comp = () => h(Foo, { onfoo, onBar })
  20. render(h(Comp), nodeOps.createElement('div'))
  21. expect(onfoo).toHaveBeenCalled()
  22. expect(onBar).toHaveBeenCalled()
  23. })
  24. test('trigger hyphendated events for update:xxx events', () => {
  25. const Foo = defineComponent({
  26. render() {},
  27. created() {
  28. this.$emit('update:fooProp')
  29. this.$emit('update:barProp')
  30. }
  31. })
  32. const fooSpy = jest.fn()
  33. const barSpy = jest.fn()
  34. const Comp = () =>
  35. h(Foo, {
  36. 'onUpdate:fooProp': fooSpy,
  37. 'onUpdate:bar-prop': barSpy
  38. })
  39. render(h(Comp), nodeOps.createElement('div'))
  40. expect(fooSpy).toHaveBeenCalled()
  41. expect(barSpy).toHaveBeenCalled()
  42. })
  43. test('warning for undeclared event (array)', () => {
  44. const Foo = defineComponent({
  45. emits: ['foo'],
  46. render() {},
  47. created() {
  48. // @ts-ignore
  49. this.$emit('bar')
  50. }
  51. })
  52. render(h(Foo), nodeOps.createElement('div'))
  53. expect(
  54. `Component emitted event "bar" but it is not declared`
  55. ).toHaveBeenWarned()
  56. })
  57. test('warning for undeclared event (object)', () => {
  58. const Foo = defineComponent({
  59. emits: {
  60. foo: null
  61. },
  62. render() {},
  63. created() {
  64. // @ts-ignore
  65. this.$emit('bar')
  66. }
  67. })
  68. render(h(Foo), nodeOps.createElement('div'))
  69. expect(
  70. `Component emitted event "bar" but it is not declared`
  71. ).toHaveBeenWarned()
  72. })
  73. test('validator warning', () => {
  74. const Foo = defineComponent({
  75. emits: {
  76. foo: (arg: number) => arg > 0
  77. },
  78. render() {},
  79. created() {
  80. this.$emit('foo', -1)
  81. }
  82. })
  83. render(h(Foo), nodeOps.createElement('div'))
  84. expect(`event validation failed for event "foo"`).toHaveBeenWarned()
  85. })
  86. test('isEmitListener', () => {
  87. expect(isEmitListener(['click'], 'onClick')).toBe(true)
  88. expect(isEmitListener(['click'], 'onclick')).toBe(true)
  89. expect(isEmitListener({ click: null }, 'onClick')).toBe(true)
  90. expect(isEmitListener({ click: null }, 'onclick')).toBe(true)
  91. expect(isEmitListener(['click'], 'onBlick')).toBe(false)
  92. expect(isEmitListener({ click: null }, 'onBlick')).toBe(false)
  93. })
  94. })