componentProps.spec.ts 8.0 KB

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