componentProps.spec.ts 14 KB

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