componentProps.spec.ts 8.8 KB

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