componentProps.spec.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 defaultBaz = jest.fn(() => ({ b: 1 }))
  139. const Comp = {
  140. props: {
  141. foo: {
  142. default: 1
  143. },
  144. bar: {
  145. default: defaultFn
  146. },
  147. baz: {
  148. type: Function,
  149. default: defaultBaz
  150. }
  151. },
  152. render() {
  153. proxy = this
  154. }
  155. }
  156. const root = nodeOps.createElement('div')
  157. render(h(Comp, { foo: 2 }), root)
  158. expect(proxy.foo).toBe(2)
  159. const prevBar = proxy.bar
  160. expect(proxy.bar).toEqual({ a: 1 })
  161. expect(proxy.baz).toEqual(defaultBaz)
  162. expect(defaultFn).toHaveBeenCalledTimes(1)
  163. expect(defaultBaz).toHaveBeenCalledTimes(0)
  164. // #999: updates should not cause default factory of unchanged prop to be
  165. // called again
  166. render(h(Comp, { foo: 3 }), root)
  167. expect(proxy.foo).toBe(3)
  168. expect(proxy.bar).toEqual({ a: 1 })
  169. expect(proxy.bar).toBe(prevBar)
  170. expect(defaultFn).toHaveBeenCalledTimes(1)
  171. render(h(Comp, { bar: { b: 2 } }), root)
  172. expect(proxy.foo).toBe(1)
  173. expect(proxy.bar).toEqual({ b: 2 })
  174. expect(defaultFn).toHaveBeenCalledTimes(1)
  175. render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
  176. expect(proxy.foo).toBe(3)
  177. expect(proxy.bar).toEqual({ b: 3 })
  178. expect(defaultFn).toHaveBeenCalledTimes(1)
  179. render(h(Comp, { bar: { b: 4 } }), root)
  180. expect(proxy.foo).toBe(1)
  181. expect(proxy.bar).toEqual({ b: 4 })
  182. expect(defaultFn).toHaveBeenCalledTimes(1)
  183. })
  184. test('optimized props updates', async () => {
  185. const Child = defineComponent({
  186. props: ['foo'],
  187. template: `<div>{{ foo }}</div>`
  188. })
  189. const foo = ref(1)
  190. const id = ref('a')
  191. const Comp = defineComponent({
  192. setup() {
  193. return {
  194. foo,
  195. id
  196. }
  197. },
  198. components: { Child },
  199. template: `<Child :foo="foo" :id="id"/>`
  200. })
  201. // Note this one is using the main Vue render so it can compile template
  202. // on the fly
  203. const root = document.createElement('div')
  204. domRender(h(Comp), root)
  205. expect(root.innerHTML).toBe('<div id="a">1</div>')
  206. foo.value++
  207. await nextTick()
  208. expect(root.innerHTML).toBe('<div id="a">2</div>')
  209. id.value = 'b'
  210. await nextTick()
  211. expect(root.innerHTML).toBe('<div id="b">2</div>')
  212. })
  213. test('warn props mutation', () => {
  214. let instance: ComponentInternalInstance
  215. let setupProps: any
  216. const Comp = {
  217. props: ['foo'],
  218. setup(props: any) {
  219. instance = getCurrentInstance()!
  220. setupProps = props
  221. return () => null
  222. }
  223. }
  224. render(h(Comp, { foo: 1 }), nodeOps.createElement('div'))
  225. expect(setupProps.foo).toBe(1)
  226. expect(instance!.props.foo).toBe(1)
  227. setupProps.foo = 2
  228. expect(`Set operation on key "foo" failed`).toHaveBeenWarned()
  229. expect(() => {
  230. ;(instance!.proxy as any).foo = 2
  231. }).toThrow(TypeError)
  232. expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
  233. })
  234. test('merging props from mixins and extends', () => {
  235. let setupProps: any
  236. let renderProxy: any
  237. const E = {
  238. props: ['base']
  239. }
  240. const M1 = {
  241. props: ['m1']
  242. }
  243. const M2 = {
  244. props: { m2: null }
  245. }
  246. const Comp = {
  247. props: ['self'],
  248. mixins: [M1, M2],
  249. extends: E,
  250. setup(props: any) {
  251. setupProps = props
  252. },
  253. render(this: any) {
  254. renderProxy = this
  255. return h('div', [this.self, this.base, this.m1, this.m2])
  256. }
  257. }
  258. const root = nodeOps.createElement('div')
  259. const props = {
  260. self: 'from self, ',
  261. base: 'from base, ',
  262. m1: 'from mixin 1, ',
  263. m2: 'from mixin 2'
  264. }
  265. render(h(Comp, props), root)
  266. expect(serializeInner(root)).toMatch(
  267. `from self, from base, from mixin 1, from mixin 2`
  268. )
  269. expect(setupProps).toMatchObject(props)
  270. expect(renderProxy.$props).toMatchObject(props)
  271. })
  272. })