componentProps.spec.ts 13 KB

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