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