componentProps.spec.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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: ['fooBar'],
  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, { fooBar: 1, bar: 2 }), root)
  29. expect(proxy.fooBar).toBe(1)
  30. expect(props).toEqual({ fooBar: 1 })
  31. expect(attrs).toEqual({ bar: 2 })
  32. // test passing kebab-case and resolving to camelCase
  33. render(h(Comp, { 'foo-bar': 2, bar: 3, baz: 4 }), root)
  34. expect(proxy.fooBar).toBe(2)
  35. expect(props).toEqual({ fooBar: 2 })
  36. expect(attrs).toEqual({ bar: 3, baz: 4 })
  37. // test updating kebab-case should not delete it (#955)
  38. render(h(Comp, { 'foo-bar': 3, bar: 3, baz: 4 }), root)
  39. expect(proxy.fooBar).toBe(3)
  40. expect(props).toEqual({ fooBar: 3 })
  41. expect(attrs).toEqual({ bar: 3, baz: 4 })
  42. render(h(Comp, { qux: 5 }), root)
  43. expect(proxy.fooBar).toBeUndefined()
  44. expect(props).toEqual({})
  45. expect(attrs).toEqual({ qux: 5 })
  46. })
  47. test('stateful with setup', () => {
  48. let props: any
  49. let attrs: any
  50. const Comp = defineComponent({
  51. props: ['foo'],
  52. setup(_props, { attrs: _attrs }) {
  53. return () => {
  54. props = _props
  55. attrs = _attrs
  56. }
  57. }
  58. })
  59. const root = nodeOps.createElement('div')
  60. render(h(Comp, { foo: 1, bar: 2 }), root)
  61. expect(props).toEqual({ foo: 1 })
  62. expect(attrs).toEqual({ bar: 2 })
  63. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  64. expect(props).toEqual({ foo: 2 })
  65. expect(attrs).toEqual({ bar: 3, baz: 4 })
  66. render(h(Comp, { qux: 5 }), root)
  67. expect(props).toEqual({})
  68. expect(attrs).toEqual({ qux: 5 })
  69. })
  70. test('functional with declaration', () => {
  71. let props: any
  72. let attrs: any
  73. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  74. props = _props
  75. attrs = _attrs
  76. }
  77. Comp.props = ['foo']
  78. const root = nodeOps.createElement('div')
  79. render(h(Comp, { foo: 1, bar: 2 }), root)
  80. expect(props).toEqual({ foo: 1 })
  81. expect(attrs).toEqual({ bar: 2 })
  82. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  83. expect(props).toEqual({ foo: 2 })
  84. expect(attrs).toEqual({ bar: 3, baz: 4 })
  85. render(h(Comp, { qux: 5 }), root)
  86. expect(props).toEqual({})
  87. expect(attrs).toEqual({ qux: 5 })
  88. })
  89. test('functional without declaration', () => {
  90. let props: any
  91. let attrs: any
  92. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  93. props = _props
  94. attrs = _attrs
  95. }
  96. const root = nodeOps.createElement('div')
  97. render(h(Comp, { foo: 1 }), root)
  98. expect(props).toEqual({ foo: 1 })
  99. expect(attrs).toEqual({ foo: 1 })
  100. expect(props).toBe(attrs)
  101. render(h(Comp, { bar: 2 }), root)
  102. expect(props).toEqual({ bar: 2 })
  103. expect(attrs).toEqual({ bar: 2 })
  104. expect(props).toBe(attrs)
  105. })
  106. test('boolean casting', () => {
  107. let proxy: any
  108. const Comp = {
  109. props: {
  110. foo: Boolean,
  111. bar: Boolean,
  112. baz: Boolean,
  113. qux: Boolean
  114. },
  115. render() {
  116. proxy = this
  117. }
  118. }
  119. render(
  120. h(Comp, {
  121. // absent should cast to false
  122. bar: '', // empty string should cast to true
  123. baz: 'baz', // same string should cast to true
  124. qux: 'ok' // other values should be left in-tact (but raise warning)
  125. }),
  126. nodeOps.createElement('div')
  127. )
  128. expect(proxy.foo).toBe(false)
  129. expect(proxy.bar).toBe(true)
  130. expect(proxy.baz).toBe(true)
  131. expect(proxy.qux).toBe('ok')
  132. expect('type check failed for prop "qux"').toHaveBeenWarned()
  133. })
  134. test('default value', () => {
  135. let proxy: any
  136. const defaultFn = jest.fn(() => ({ a: 1 }))
  137. const Comp = {
  138. props: {
  139. foo: {
  140. default: 1
  141. },
  142. bar: {
  143. default: defaultFn
  144. }
  145. },
  146. render() {
  147. proxy = this
  148. }
  149. }
  150. const root = nodeOps.createElement('div')
  151. render(h(Comp, { foo: 2 }), root)
  152. expect(proxy.foo).toBe(2)
  153. const prevBar = proxy.bar
  154. expect(proxy.bar).toEqual({ a: 1 })
  155. expect(defaultFn).toHaveBeenCalledTimes(1)
  156. // #999: updates should not cause default factory of unchanged prop to be
  157. // called again
  158. render(h(Comp, { foo: 3 }), root)
  159. expect(proxy.foo).toBe(3)
  160. expect(proxy.bar).toEqual({ a: 1 })
  161. expect(proxy.bar).toBe(prevBar)
  162. expect(defaultFn).toHaveBeenCalledTimes(1)
  163. render(h(Comp, { bar: { b: 2 } }), root)
  164. expect(proxy.foo).toBe(1)
  165. expect(proxy.bar).toEqual({ b: 2 })
  166. expect(defaultFn).toHaveBeenCalledTimes(1)
  167. render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
  168. expect(proxy.foo).toBe(3)
  169. expect(proxy.bar).toEqual({ b: 3 })
  170. expect(defaultFn).toHaveBeenCalledTimes(1)
  171. render(h(Comp, { bar: { b: 4 } }), root)
  172. expect(proxy.foo).toBe(1)
  173. expect(proxy.bar).toEqual({ b: 4 })
  174. expect(defaultFn).toHaveBeenCalledTimes(1)
  175. })
  176. test('optimized props updates', async () => {
  177. const Child = defineComponent({
  178. props: ['foo'],
  179. template: `<div>{{ foo }}</div>`
  180. })
  181. const foo = ref(1)
  182. const id = ref('a')
  183. const Comp = defineComponent({
  184. setup() {
  185. return {
  186. foo,
  187. id
  188. }
  189. },
  190. components: { Child },
  191. template: `<Child :foo="foo" :id="id"/>`
  192. })
  193. // Note this one is using the main Vue render so it can compile template
  194. // on the fly
  195. const root = document.createElement('div')
  196. domRender(h(Comp), root)
  197. expect(root.innerHTML).toBe('<div id="a">1</div>')
  198. foo.value++
  199. await nextTick()
  200. expect(root.innerHTML).toBe('<div id="a">2</div>')
  201. id.value = 'b'
  202. await nextTick()
  203. expect(root.innerHTML).toBe('<div id="b">2</div>')
  204. })
  205. test('warn props mutation', () => {
  206. let instance: ComponentInternalInstance
  207. let setupProps: any
  208. const Comp = {
  209. props: ['foo'],
  210. setup(props: any) {
  211. instance = getCurrentInstance()!
  212. setupProps = props
  213. return () => null
  214. }
  215. }
  216. render(h(Comp, { foo: 1 }), nodeOps.createElement('div'))
  217. expect(setupProps.foo).toBe(1)
  218. expect(instance!.props.foo).toBe(1)
  219. setupProps.foo = 2
  220. expect(`Set operation on key "foo" failed`).toHaveBeenWarned()
  221. expect(() => {
  222. ;(instance!.proxy as any).foo = 2
  223. }).toThrow(TypeError)
  224. expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
  225. })
  226. })