componentProps.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. // #3495
  283. test('should not warn required props using kebab-case', async () => {
  284. const Comp = {
  285. props: {
  286. fooBar: { type: String, required: true }
  287. },
  288. setup() {
  289. return () => null
  290. }
  291. }
  292. render(
  293. h(Comp, {
  294. 'foo-bar': 'hello'
  295. }),
  296. nodeOps.createElement('div')
  297. )
  298. expect(`Missing required prop: "fooBar"`).not.toHaveBeenWarned()
  299. })
  300. test('merging props from mixins and extends', () => {
  301. let setupProps: any
  302. let renderProxy: any
  303. const E = {
  304. props: ['base']
  305. }
  306. const M1 = {
  307. props: ['m1']
  308. }
  309. const M2 = {
  310. props: { m2: null }
  311. }
  312. const Comp = {
  313. props: ['self'],
  314. mixins: [M1, M2],
  315. extends: E,
  316. setup(props: any) {
  317. setupProps = props
  318. },
  319. render(this: any) {
  320. renderProxy = this
  321. return h('div', [this.self, this.base, this.m1, this.m2])
  322. }
  323. }
  324. const root = nodeOps.createElement('div')
  325. const props = {
  326. self: 'from self, ',
  327. base: 'from base, ',
  328. m1: 'from mixin 1, ',
  329. m2: 'from mixin 2'
  330. }
  331. render(h(Comp, props), root)
  332. expect(serializeInner(root)).toMatch(
  333. `from self, from base, from mixin 1, from mixin 2`
  334. )
  335. expect(setupProps).toMatchObject(props)
  336. expect(renderProxy.$props).toMatchObject(props)
  337. })
  338. test('merging props from global mixins', () => {
  339. let setupProps: any
  340. let renderProxy: any
  341. const M1 = {
  342. props: ['m1']
  343. }
  344. const M2 = {
  345. props: { m2: null }
  346. }
  347. const Comp = {
  348. props: ['self'],
  349. setup(props: any) {
  350. setupProps = props
  351. },
  352. render(this: any) {
  353. renderProxy = this
  354. return h('div', [this.self, this.m1, this.m2])
  355. }
  356. }
  357. const props = {
  358. self: 'from self, ',
  359. m1: 'from mixin 1, ',
  360. m2: 'from mixin 2'
  361. }
  362. const app = createApp(Comp, props)
  363. app.mixin(M1)
  364. app.mixin(M2)
  365. const root = nodeOps.createElement('div')
  366. app.mount(root)
  367. expect(serializeInner(root)).toMatch(
  368. `from self, from mixin 1, from mixin 2`
  369. )
  370. expect(setupProps).toMatchObject(props)
  371. expect(renderProxy.$props).toMatchObject(props)
  372. })
  373. test('props type support BigInt', () => {
  374. const Comp = {
  375. props: {
  376. foo: BigInt
  377. },
  378. render(this: any) {
  379. return h('div', [this.foo])
  380. }
  381. }
  382. const root = nodeOps.createElement('div')
  383. render(
  384. h(Comp, {
  385. foo: BigInt(BigInt(100000111)) + BigInt(2000000000) * BigInt(30000000)
  386. }),
  387. root
  388. )
  389. expect(serializeInner(root)).toMatch('<div>60000000100000111</div>')
  390. })
  391. // #3474
  392. test('should cache the value returned from the default factory to avoid unnecessary watcher trigger', async () => {
  393. let count = 0
  394. const Comp = {
  395. props: {
  396. foo: {
  397. type: Object,
  398. default: () => ({ val: 1 })
  399. },
  400. bar: Number
  401. },
  402. setup(props: any) {
  403. watch(
  404. () => props.foo,
  405. () => {
  406. count++
  407. }
  408. )
  409. return () => h('h1', [props.foo.val, props.bar])
  410. }
  411. }
  412. const foo = ref()
  413. const bar = ref(0)
  414. const app = createApp({
  415. render: () => h(Comp, { foo: foo.value, bar: bar.value })
  416. })
  417. const root = nodeOps.createElement('div')
  418. app.mount(root)
  419. expect(serializeInner(root)).toMatch(`<h1>10</h1>`)
  420. expect(count).toBe(0)
  421. bar.value++
  422. await nextTick()
  423. expect(serializeInner(root)).toMatch(`<h1>11</h1>`)
  424. expect(count).toBe(0)
  425. })
  426. })