componentProps.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import {
  2. ComponentInternalInstance,
  3. getCurrentInstance,
  4. render,
  5. h,
  6. nodeOps,
  7. FunctionalComponent,
  8. defineComponent,
  9. ref,
  10. serializeInner,
  11. createApp,
  12. provide,
  13. inject,
  14. watch
  15. } from '@vue/runtime-test'
  16. import { render as domRender, nextTick } from 'vue'
  17. describe('component props', () => {
  18. test('stateful', () => {
  19. let props: any
  20. let attrs: any
  21. let proxy: any
  22. const Comp = defineComponent({
  23. props: ['fooBar', 'barBaz'],
  24. render() {
  25. props = this.$props
  26. attrs = this.$attrs
  27. proxy = this
  28. }
  29. })
  30. const root = nodeOps.createElement('div')
  31. render(h(Comp, { fooBar: 1, bar: 2 }), root)
  32. expect(proxy.fooBar).toBe(1)
  33. expect(props).toEqual({ fooBar: 1 })
  34. expect(attrs).toEqual({ bar: 2 })
  35. // test passing kebab-case and resolving to camelCase
  36. render(h(Comp, { 'foo-bar': 2, bar: 3, baz: 4 }), root)
  37. expect(proxy.fooBar).toBe(2)
  38. expect(props).toEqual({ fooBar: 2 })
  39. expect(attrs).toEqual({ bar: 3, baz: 4 })
  40. // test updating kebab-case should not delete it (#955)
  41. render(h(Comp, { 'foo-bar': 3, bar: 3, baz: 4, barBaz: 5 }), root)
  42. expect(proxy.fooBar).toBe(3)
  43. expect(proxy.barBaz).toBe(5)
  44. expect(props).toEqual({ fooBar: 3, barBaz: 5 })
  45. expect(attrs).toEqual({ bar: 3, baz: 4 })
  46. render(h(Comp, { qux: 5 }), root)
  47. expect(proxy.fooBar).toBeUndefined()
  48. // remove the props with camelCase key (#1412)
  49. expect(proxy.barBaz).toBeUndefined()
  50. expect(props).toEqual({})
  51. expect(attrs).toEqual({ qux: 5 })
  52. })
  53. test('stateful with setup', () => {
  54. let props: any
  55. let attrs: any
  56. const Comp = defineComponent({
  57. props: ['foo'],
  58. setup(_props, { attrs: _attrs }) {
  59. return () => {
  60. props = _props
  61. attrs = _attrs
  62. }
  63. }
  64. })
  65. const root = nodeOps.createElement('div')
  66. render(h(Comp, { foo: 1, bar: 2 }), root)
  67. expect(props).toEqual({ foo: 1 })
  68. expect(attrs).toEqual({ bar: 2 })
  69. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  70. expect(props).toEqual({ foo: 2 })
  71. expect(attrs).toEqual({ bar: 3, baz: 4 })
  72. render(h(Comp, { qux: 5 }), root)
  73. expect(props).toEqual({})
  74. expect(attrs).toEqual({ qux: 5 })
  75. })
  76. test('functional with declaration', () => {
  77. let props: any
  78. let attrs: any
  79. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  80. props = _props
  81. attrs = _attrs
  82. }
  83. Comp.props = ['foo']
  84. const root = nodeOps.createElement('div')
  85. render(h(Comp, { foo: 1, bar: 2 }), root)
  86. expect(props).toEqual({ foo: 1 })
  87. expect(attrs).toEqual({ bar: 2 })
  88. render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
  89. expect(props).toEqual({ foo: 2 })
  90. expect(attrs).toEqual({ bar: 3, baz: 4 })
  91. render(h(Comp, { qux: 5 }), root)
  92. expect(props).toEqual({})
  93. expect(attrs).toEqual({ qux: 5 })
  94. })
  95. test('functional without declaration', () => {
  96. let props: any
  97. let attrs: any
  98. const Comp: FunctionalComponent = (_props, { attrs: _attrs }) => {
  99. props = _props
  100. attrs = _attrs
  101. }
  102. const root = nodeOps.createElement('div')
  103. render(h(Comp, { foo: 1 }), root)
  104. expect(props).toEqual({ foo: 1 })
  105. expect(attrs).toEqual({ foo: 1 })
  106. expect(props).toBe(attrs)
  107. render(h(Comp, { bar: 2 }), root)
  108. expect(props).toEqual({ bar: 2 })
  109. expect(attrs).toEqual({ bar: 2 })
  110. expect(props).toBe(attrs)
  111. })
  112. test('boolean casting', () => {
  113. let proxy: any
  114. const Comp = {
  115. props: {
  116. foo: Boolean,
  117. bar: Boolean,
  118. baz: Boolean,
  119. qux: Boolean
  120. },
  121. render() {
  122. proxy = this
  123. }
  124. }
  125. render(
  126. h(Comp, {
  127. // absent should cast to false
  128. bar: '', // empty string should cast to true
  129. baz: 'baz', // same string should cast to true
  130. qux: 'ok' // other values should be left in-tact (but raise warning)
  131. }),
  132. nodeOps.createElement('div')
  133. )
  134. expect(proxy.foo).toBe(false)
  135. expect(proxy.bar).toBe(true)
  136. expect(proxy.baz).toBe(true)
  137. expect(proxy.qux).toBe('ok')
  138. expect('type check failed for prop "qux"').toHaveBeenWarned()
  139. })
  140. test('default value', () => {
  141. let proxy: any
  142. const defaultFn = jest.fn(() => ({ a: 1 }))
  143. const defaultBaz = jest.fn(() => ({ b: 1 }))
  144. const Comp = {
  145. props: {
  146. foo: {
  147. default: 1
  148. },
  149. bar: {
  150. default: defaultFn
  151. },
  152. baz: {
  153. type: Function,
  154. default: defaultBaz
  155. }
  156. },
  157. render() {
  158. proxy = this
  159. }
  160. }
  161. const root = nodeOps.createElement('div')
  162. render(h(Comp, { foo: 2 }), root)
  163. expect(proxy.foo).toBe(2)
  164. const prevBar = proxy.bar
  165. expect(proxy.bar).toEqual({ a: 1 })
  166. expect(proxy.baz).toEqual(defaultBaz)
  167. expect(defaultFn).toHaveBeenCalledTimes(1)
  168. expect(defaultBaz).toHaveBeenCalledTimes(0)
  169. // #999: updates should not cause default factory of unchanged prop to be
  170. // called again
  171. render(h(Comp, { foo: 3 }), root)
  172. expect(proxy.foo).toBe(3)
  173. expect(proxy.bar).toEqual({ a: 1 })
  174. expect(proxy.bar).toBe(prevBar)
  175. expect(defaultFn).toHaveBeenCalledTimes(1)
  176. render(h(Comp, { bar: { b: 2 } }), root)
  177. expect(proxy.foo).toBe(1)
  178. expect(proxy.bar).toEqual({ b: 2 })
  179. expect(defaultFn).toHaveBeenCalledTimes(1)
  180. render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
  181. expect(proxy.foo).toBe(3)
  182. expect(proxy.bar).toEqual({ b: 3 })
  183. expect(defaultFn).toHaveBeenCalledTimes(1)
  184. render(h(Comp, { bar: { b: 4 } }), root)
  185. expect(proxy.foo).toBe(1)
  186. expect(proxy.bar).toEqual({ b: 4 })
  187. expect(defaultFn).toHaveBeenCalledTimes(1)
  188. })
  189. test('using inject in default value factory', () => {
  190. const Child = defineComponent({
  191. props: {
  192. test: {
  193. default: () => inject('test', 'default')
  194. }
  195. },
  196. setup(props) {
  197. return () => {
  198. return h('div', props.test)
  199. }
  200. }
  201. })
  202. const Comp = {
  203. setup() {
  204. provide('test', 'injected')
  205. return () => h(Child)
  206. }
  207. }
  208. const root = nodeOps.createElement('div')
  209. render(h(Comp), root)
  210. expect(serializeInner(root)).toBe(`<div>injected</div>`)
  211. })
  212. test('optimized props updates', async () => {
  213. const Child = defineComponent({
  214. props: ['foo'],
  215. template: `<div>{{ foo }}</div>`
  216. })
  217. const foo = ref(1)
  218. const id = ref('a')
  219. const Comp = defineComponent({
  220. setup() {
  221. return {
  222. foo,
  223. id
  224. }
  225. },
  226. components: { Child },
  227. template: `<Child :foo="foo" :id="id"/>`
  228. })
  229. // Note this one is using the main Vue render so it can compile template
  230. // on the fly
  231. const root = document.createElement('div')
  232. domRender(h(Comp), root)
  233. expect(root.innerHTML).toBe('<div id="a">1</div>')
  234. foo.value++
  235. await nextTick()
  236. expect(root.innerHTML).toBe('<div id="a">2</div>')
  237. id.value = 'b'
  238. await nextTick()
  239. expect(root.innerHTML).toBe('<div id="b">2</div>')
  240. })
  241. test('warn props mutation', () => {
  242. let instance: ComponentInternalInstance
  243. let setupProps: any
  244. const Comp = {
  245. props: ['foo'],
  246. setup(props: any) {
  247. instance = getCurrentInstance()!
  248. setupProps = props
  249. return () => null
  250. }
  251. }
  252. render(h(Comp, { foo: 1 }), nodeOps.createElement('div'))
  253. expect(setupProps.foo).toBe(1)
  254. expect(instance!.props.foo).toBe(1)
  255. setupProps.foo = 2
  256. expect(`Set operation on key "foo" failed`).toHaveBeenWarned()
  257. expect(() => {
  258. ;(instance!.proxy as any).foo = 2
  259. }).toThrow(TypeError)
  260. expect(`Attempting to mutate prop "foo"`).toHaveBeenWarned()
  261. // should not throw when overriding properties other than props
  262. expect(() => {
  263. ;(instance!.proxy as any).hasOwnProperty = () => {}
  264. }).not.toThrow(TypeError)
  265. })
  266. test('warn absent required props', () => {
  267. const Comp = {
  268. props: {
  269. bool: { type: Boolean, required: true },
  270. str: { type: String, required: true },
  271. num: { type: Number, required: true }
  272. },
  273. setup() {
  274. return () => null
  275. }
  276. }
  277. render(h(Comp), nodeOps.createElement('div'))
  278. expect(`Missing required prop: "bool"`).toHaveBeenWarned()
  279. expect(`Missing required prop: "str"`).toHaveBeenWarned()
  280. expect(`Missing required prop: "num"`).toHaveBeenWarned()
  281. })
  282. test('merging props from mixins and extends', () => {
  283. let setupProps: any
  284. let renderProxy: any
  285. const E = {
  286. props: ['base']
  287. }
  288. const M1 = {
  289. props: ['m1']
  290. }
  291. const M2 = {
  292. props: { m2: null }
  293. }
  294. const Comp = {
  295. props: ['self'],
  296. mixins: [M1, M2],
  297. extends: E,
  298. setup(props: any) {
  299. setupProps = props
  300. },
  301. render(this: any) {
  302. renderProxy = this
  303. return h('div', [this.self, this.base, this.m1, this.m2])
  304. }
  305. }
  306. const root = nodeOps.createElement('div')
  307. const props = {
  308. self: 'from self, ',
  309. base: 'from base, ',
  310. m1: 'from mixin 1, ',
  311. m2: 'from mixin 2'
  312. }
  313. render(h(Comp, props), root)
  314. expect(serializeInner(root)).toMatch(
  315. `from self, from base, from mixin 1, from mixin 2`
  316. )
  317. expect(setupProps).toMatchObject(props)
  318. expect(renderProxy.$props).toMatchObject(props)
  319. })
  320. test('merging props from global mixins', () => {
  321. let setupProps: any
  322. let renderProxy: any
  323. const M1 = {
  324. props: ['m1']
  325. }
  326. const M2 = {
  327. props: { m2: null }
  328. }
  329. const Comp = {
  330. props: ['self'],
  331. setup(props: any) {
  332. setupProps = props
  333. },
  334. render(this: any) {
  335. renderProxy = this
  336. return h('div', [this.self, this.m1, this.m2])
  337. }
  338. }
  339. const props = {
  340. self: 'from self, ',
  341. m1: 'from mixin 1, ',
  342. m2: 'from mixin 2'
  343. }
  344. const app = createApp(Comp, props)
  345. app.mixin(M1)
  346. app.mixin(M2)
  347. const root = nodeOps.createElement('div')
  348. app.mount(root)
  349. expect(serializeInner(root)).toMatch(
  350. `from self, from mixin 1, from mixin 2`
  351. )
  352. expect(setupProps).toMatchObject(props)
  353. expect(renderProxy.$props).toMatchObject(props)
  354. })
  355. test('props type support BigInt', () => {
  356. const Comp = {
  357. props: {
  358. foo: BigInt
  359. },
  360. render(this: any) {
  361. return h('div', [this.foo])
  362. }
  363. }
  364. const root = nodeOps.createElement('div')
  365. render(
  366. h(Comp, {
  367. foo: BigInt(BigInt(100000111)) + BigInt(2000000000) * BigInt(30000000)
  368. }),
  369. root
  370. )
  371. expect(serializeInner(root)).toMatch('<div>60000000100000111</div>')
  372. })
  373. // #3474
  374. test('should cache the value returned from the default factory to avoid unnecessary watcher trigger', async () => {
  375. let count = 0
  376. const Comp = {
  377. props: {
  378. foo: {
  379. type: Object,
  380. default: () => ({ val: 1 })
  381. },
  382. bar: Number
  383. },
  384. setup(props: any) {
  385. watch(
  386. () => props.foo,
  387. () => {
  388. count++
  389. }
  390. )
  391. return () => h('h1', [props.foo.val, props.bar])
  392. }
  393. }
  394. const foo = ref()
  395. const bar = ref(0)
  396. const app = createApp({
  397. render: () => h(Comp, { foo: foo.value, bar: bar.value })
  398. })
  399. const root = nodeOps.createElement('div')
  400. app.mount(root)
  401. expect(serializeInner(root)).toMatch(`<h1>10</h1>`)
  402. expect(count).toBe(0)
  403. bar.value++
  404. await nextTick()
  405. expect(serializeInner(root)).toMatch(`<h1>11</h1>`)
  406. expect(count).toBe(0)
  407. })
  408. })