componentProps.spec.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import {
  2. ComponentInternalInstance,
  3. getCurrentInstance,
  4. render,
  5. h,
  6. nodeOps,
  7. FunctionalComponent,
  8. defineComponent,
  9. ref,
  10. serializeInner
  11. } from '@vue/runtime-test'
  12. import { render as domRender, nextTick } from 'vue'
  13. import { mockWarn } from '@vue/shared'
  14. describe('component props', () => {
  15. mockWarn()
  16. test('stateful', () => {
  17. let props: any
  18. let attrs: any
  19. let proxy: any
  20. const Comp = defineComponent({
  21. props: ['fooBar'],
  22. render() {
  23. props = this.$props
  24. attrs = this.$attrs
  25. proxy = this
  26. }
  27. })
  28. const root = nodeOps.createElement('div')
  29. render(h(Comp, { fooBar: 1, bar: 2 }), root)
  30. expect(proxy.fooBar).toBe(1)
  31. expect(props).toEqual({ fooBar: 1 })
  32. expect(attrs).toEqual({ bar: 2 })
  33. // test passing kebab-case and resolving to camelCase
  34. render(h(Comp, { 'foo-bar': 2, bar: 3, baz: 4 }), root)
  35. expect(proxy.fooBar).toBe(2)
  36. expect(props).toEqual({ fooBar: 2 })
  37. expect(attrs).toEqual({ bar: 3, baz: 4 })
  38. // test updating kebab-case should not delete it (#955)
  39. render(h(Comp, { 'foo-bar': 3, bar: 3, baz: 4 }), root)
  40. expect(proxy.fooBar).toBe(3)
  41. expect(props).toEqual({ fooBar: 3 })
  42. expect(attrs).toEqual({ bar: 3, baz: 4 })
  43. render(h(Comp, { qux: 5 }), root)
  44. expect(proxy.fooBar).toBeUndefined()
  45. expect(props).toEqual({})
  46. expect(attrs).toEqual({ qux: 5 })
  47. })
  48. test('stateful with setup', () => {
  49. let props: any
  50. let attrs: any
  51. const Comp = defineComponent({
  52. props: ['foo'],
  53. setup(_props, { attrs: _attrs }) {
  54. return () => {
  55. props = _props
  56. attrs = _attrs
  57. }
  58. }
  59. })
  60. const root = nodeOps.createElement('div')
  61. render(h(Comp, { foo: 1, bar: 2 }), root)
  62. expect(props).toEqual({ foo: 1 })
  63. expect(attrs).toEqual({ bar: 2 })
  64. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  65. expect(props).toEqual({ foo: 2 })
  66. expect(attrs).toEqual({ bar: 3, baz: 4 })
  67. render(h(Comp, { qux: 5 }), root)
  68. expect(props).toEqual({})
  69. expect(attrs).toEqual({ qux: 5 })
  70. })
  71. test('functional with declaration', () => {
  72. let props: any
  73. let attrs: any
  74. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  75. props = _props
  76. attrs = _attrs
  77. }
  78. Comp.props = ['foo']
  79. const root = nodeOps.createElement('div')
  80. render(h(Comp, { foo: 1, bar: 2 }), root)
  81. expect(props).toEqual({ foo: 1 })
  82. expect(attrs).toEqual({ bar: 2 })
  83. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  84. expect(props).toEqual({ foo: 2 })
  85. expect(attrs).toEqual({ bar: 3, baz: 4 })
  86. render(h(Comp, { qux: 5 }), root)
  87. expect(props).toEqual({})
  88. expect(attrs).toEqual({ qux: 5 })
  89. })
  90. test('functional without declaration', () => {
  91. let props: any
  92. let attrs: any
  93. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  94. props = _props
  95. attrs = _attrs
  96. }
  97. const root = nodeOps.createElement('div')
  98. render(h(Comp, { foo: 1 }), root)
  99. expect(props).toEqual({ foo: 1 })
  100. expect(attrs).toEqual({ foo: 1 })
  101. expect(props).toBe(attrs)
  102. render(h(Comp, { bar: 2 }), root)
  103. expect(props).toEqual({ bar: 2 })
  104. expect(attrs).toEqual({ bar: 2 })
  105. expect(props).toBe(attrs)
  106. })
  107. test('boolean casting', () => {
  108. let proxy: any
  109. const Comp = {
  110. props: {
  111. foo: Boolean,
  112. bar: Boolean,
  113. baz: Boolean,
  114. qux: Boolean
  115. },
  116. render() {
  117. proxy = this
  118. }
  119. }
  120. render(
  121. h(Comp, {
  122. // absent should cast to false
  123. bar: '', // empty string should cast to true
  124. baz: 'baz', // same string should cast to true
  125. qux: 'ok' // other values should be left in-tact (but raise warning)
  126. }),
  127. nodeOps.createElement('div')
  128. )
  129. expect(proxy.foo).toBe(false)
  130. expect(proxy.bar).toBe(true)
  131. expect(proxy.baz).toBe(true)
  132. expect(proxy.qux).toBe('ok')
  133. expect('type check failed for prop "qux"').toHaveBeenWarned()
  134. })
  135. test('default value', () => {
  136. let proxy: any
  137. const defaultFn = jest.fn(() => ({ a: 1 }))
  138. const Comp = {
  139. props: {
  140. foo: {
  141. default: 1
  142. },
  143. bar: {
  144. default: defaultFn
  145. }
  146. },
  147. render() {
  148. proxy = this
  149. }
  150. }
  151. const root = nodeOps.createElement('div')
  152. render(h(Comp, { foo: 2 }), root)
  153. expect(proxy.foo).toBe(2)
  154. const prevBar = proxy.bar
  155. expect(proxy.bar).toEqual({ a: 1 })
  156. expect(defaultFn).toHaveBeenCalledTimes(1)
  157. // #999: updates should not cause default factory of unchanged prop to be
  158. // called again
  159. render(h(Comp, { foo: 3 }), root)
  160. expect(proxy.foo).toBe(3)
  161. expect(proxy.bar).toEqual({ a: 1 })
  162. expect(proxy.bar).toBe(prevBar)
  163. expect(defaultFn).toHaveBeenCalledTimes(1)
  164. render(h(Comp, { bar: { b: 2 } }), root)
  165. expect(proxy.foo).toBe(1)
  166. expect(proxy.bar).toEqual({ b: 2 })
  167. expect(defaultFn).toHaveBeenCalledTimes(1)
  168. render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
  169. expect(proxy.foo).toBe(3)
  170. expect(proxy.bar).toEqual({ b: 3 })
  171. expect(defaultFn).toHaveBeenCalledTimes(1)
  172. render(h(Comp, { bar: { b: 4 } }), root)
  173. expect(proxy.foo).toBe(1)
  174. expect(proxy.bar).toEqual({ b: 4 })
  175. expect(defaultFn).toHaveBeenCalledTimes(1)
  176. })
  177. test('optimized props updates', async () => {
  178. const Child = defineComponent({
  179. props: ['foo'],
  180. template: `<div>{{ foo }}</div>`
  181. })
  182. const foo = ref(1)
  183. const id = ref('a')
  184. const Comp = defineComponent({
  185. setup() {
  186. return {
  187. foo,
  188. id
  189. }
  190. },
  191. components: { Child },
  192. template: `<Child :foo="foo" :id="id"/>`
  193. })
  194. // Note this one is using the main Vue render so it can compile template
  195. // on the fly
  196. const root = document.createElement('div')
  197. domRender(h(Comp), root)
  198. expect(root.innerHTML).toBe('<div id="a">1</div>')
  199. foo.value++
  200. await nextTick()
  201. expect(root.innerHTML).toBe('<div id="a">2</div>')
  202. id.value = 'b'
  203. await nextTick()
  204. expect(root.innerHTML).toBe('<div id="b">2</div>')
  205. })
  206. test('warn props mutation', () => {
  207. let instance: ComponentInternalInstance
  208. let setupProps: any
  209. const Comp = {
  210. props: ['foo'],
  211. setup(props: any) {
  212. instance = getCurrentInstance()!
  213. setupProps = props
  214. return () => null
  215. }
  216. }
  217. render(h(Comp, { foo: 1 }), nodeOps.createElement('div'))
  218. expect(setupProps.foo).toBe(1)
  219. expect(instance!.props.foo).toBe(1)
  220. setupProps.foo = 2
  221. expect(`Set operation on key "foo" failed`).toHaveBeenWarned()
  222. expect(() => {
  223. ;(instance!.proxy as any).foo = 2
  224. }).toThrow(TypeError)
  225. expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
  226. })
  227. test('merging props from mixins and extends', () => {
  228. let setupProps: any
  229. let renderProxy: any
  230. const E = {
  231. props: ['base']
  232. }
  233. const M1 = {
  234. props: ['m1']
  235. }
  236. const M2 = {
  237. props: { m2: null }
  238. }
  239. const Comp = {
  240. props: ['self'],
  241. mixins: [M1, M2],
  242. extends: E,
  243. setup(props: any) {
  244. setupProps = props
  245. },
  246. render(this: any) {
  247. renderProxy = this
  248. return h('div', [this.self, this.base, this.m1, this.m2])
  249. }
  250. }
  251. const root = nodeOps.createElement('div')
  252. const props = {
  253. self: 'from self, ',
  254. base: 'from base, ',
  255. m1: 'from mixin 1, ',
  256. m2: 'from mixin 2'
  257. }
  258. render(h(Comp, props), root)
  259. expect(serializeInner(root)).toMatch(
  260. `from self, from base, from mixin 1, from mixin 2`
  261. )
  262. expect(setupProps).toMatchObject(props)
  263. expect(renderProxy.$props).toMatchObject(props)
  264. })
  265. })