componentProps.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import {
  2. ComponentInternalInstance,
  3. getCurrentInstance,
  4. render,
  5. h,
  6. nodeOps,
  7. FunctionalComponent,
  8. defineComponent,
  9. ref
  10. } from '@vue/runtime-test'
  11. import { render as domRender, nextTick } from 'vue'
  12. import { mockWarn } from '@vue/shared'
  13. describe('component props', () => {
  14. mockWarn()
  15. test('stateful', () => {
  16. let props: any
  17. let attrs: any
  18. let proxy: any
  19. const Comp = defineComponent({
  20. props: ['foo'],
  21. render() {
  22. props = this.$props
  23. attrs = this.$attrs
  24. proxy = this
  25. }
  26. })
  27. const root = nodeOps.createElement('div')
  28. render(h(Comp, { foo: 1, bar: 2 }), root)
  29. expect(proxy.foo).toBe(1)
  30. expect(props).toEqual({ foo: 1 })
  31. expect(attrs).toEqual({ bar: 2 })
  32. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  33. expect(proxy.foo).toBe(2)
  34. expect(props).toEqual({ foo: 2 })
  35. expect(attrs).toEqual({ bar: 3, baz: 4 })
  36. render(h(Comp, { qux: 5 }), root)
  37. expect(proxy.foo).toBeUndefined()
  38. expect(props).toEqual({})
  39. expect(attrs).toEqual({ qux: 5 })
  40. })
  41. test('stateful with setup', () => {
  42. let props: any
  43. let attrs: any
  44. const Comp = defineComponent({
  45. props: ['foo'],
  46. setup(_props, { attrs: _attrs }) {
  47. return () => {
  48. props = _props
  49. attrs = _attrs
  50. }
  51. }
  52. })
  53. const root = nodeOps.createElement('div')
  54. render(h(Comp, { foo: 1, bar: 2 }), root)
  55. expect(props).toEqual({ foo: 1 })
  56. expect(attrs).toEqual({ bar: 2 })
  57. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  58. expect(props).toEqual({ foo: 2 })
  59. expect(attrs).toEqual({ bar: 3, baz: 4 })
  60. render(h(Comp, { qux: 5 }), root)
  61. expect(props).toEqual({})
  62. expect(attrs).toEqual({ qux: 5 })
  63. })
  64. test('functional with declaration', () => {
  65. let props: any
  66. let attrs: any
  67. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  68. props = _props
  69. attrs = _attrs
  70. }
  71. Comp.props = ['foo']
  72. const root = nodeOps.createElement('div')
  73. render(h(Comp, { foo: 1, bar: 2 }), root)
  74. expect(props).toEqual({ foo: 1 })
  75. expect(attrs).toEqual({ bar: 2 })
  76. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  77. expect(props).toEqual({ foo: 2 })
  78. expect(attrs).toEqual({ bar: 3, baz: 4 })
  79. render(h(Comp, { qux: 5 }), root)
  80. expect(props).toEqual({})
  81. expect(attrs).toEqual({ qux: 5 })
  82. })
  83. test('functional without declaration', () => {
  84. let props: any
  85. let attrs: any
  86. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  87. props = _props
  88. attrs = _attrs
  89. }
  90. const root = nodeOps.createElement('div')
  91. render(h(Comp, { foo: 1 }), root)
  92. expect(props).toEqual({ foo: 1 })
  93. expect(attrs).toEqual({ foo: 1 })
  94. expect(props).toBe(attrs)
  95. render(h(Comp, { bar: 2 }), root)
  96. expect(props).toEqual({ bar: 2 })
  97. expect(attrs).toEqual({ bar: 2 })
  98. expect(props).toBe(attrs)
  99. })
  100. test('boolean casting', () => {
  101. let proxy: any
  102. const Comp = {
  103. props: {
  104. foo: Boolean,
  105. bar: Boolean,
  106. baz: Boolean,
  107. qux: Boolean
  108. },
  109. render() {
  110. proxy = this
  111. }
  112. }
  113. render(
  114. h(Comp, {
  115. // absent should cast to false
  116. bar: '', // empty string should cast to true
  117. baz: 'baz', // same string should cast to true
  118. qux: 'ok' // other values should be left in-tact (but raise warning)
  119. }),
  120. nodeOps.createElement('div')
  121. )
  122. expect(proxy.foo).toBe(false)
  123. expect(proxy.bar).toBe(true)
  124. expect(proxy.baz).toBe(true)
  125. expect(proxy.qux).toBe('ok')
  126. expect('type check failed for prop "qux"').toHaveBeenWarned()
  127. })
  128. test('default value', () => {
  129. let proxy: any
  130. const Comp = {
  131. props: {
  132. foo: {
  133. default: 1
  134. },
  135. bar: {
  136. default: () => ({ a: 1 })
  137. }
  138. },
  139. render() {
  140. proxy = this
  141. }
  142. }
  143. const root = nodeOps.createElement('div')
  144. render(h(Comp, { foo: 2 }), root)
  145. expect(proxy.foo).toBe(2)
  146. expect(proxy.bar).toEqual({ a: 1 })
  147. render(h(Comp, { foo: undefined, bar: { b: 2 } }), root)
  148. expect(proxy.foo).toBe(1)
  149. expect(proxy.bar).toEqual({ b: 2 })
  150. })
  151. test('optimized props updates', async () => {
  152. const Child = defineComponent({
  153. props: ['foo'],
  154. template: `<div>{{ foo }}</div>`
  155. })
  156. const foo = ref(1)
  157. const id = ref('a')
  158. const Comp = defineComponent({
  159. setup() {
  160. return {
  161. foo,
  162. id
  163. }
  164. },
  165. components: { Child },
  166. template: `<Child :foo="foo" :id="id"/>`
  167. })
  168. // Note this one is using the main Vue render so it can compile template
  169. // on the fly
  170. const root = document.createElement('div')
  171. domRender(h(Comp), root)
  172. expect(root.innerHTML).toBe('<div id="a">1</div>')
  173. foo.value++
  174. await nextTick()
  175. expect(root.innerHTML).toBe('<div id="a">2</div>')
  176. id.value = 'b'
  177. await nextTick()
  178. expect(root.innerHTML).toBe('<div id="b">2</div>')
  179. })
  180. test('warn props mutation', () => {
  181. let instance: ComponentInternalInstance
  182. let setupProps: any
  183. const Comp = {
  184. props: ['foo'],
  185. setup(props: any) {
  186. instance = getCurrentInstance()!
  187. setupProps = props
  188. return () => null
  189. }
  190. }
  191. render(h(Comp, { foo: 1 }), nodeOps.createElement('div'))
  192. expect(setupProps.foo).toBe(1)
  193. expect(instance!.props.foo).toBe(1)
  194. setupProps.foo = 2
  195. expect(`Set operation on key "foo" failed`).toHaveBeenWarned()
  196. expect(() => {
  197. ;(instance!.proxy as any).foo = 2
  198. }).toThrow(TypeError)
  199. expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
  200. })
  201. })