rendererAttrsFallthrough.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /**
  2. * @vitest-environment jsdom
  3. */
  4. // using DOM renderer because this case is mostly DOM-specific
  5. import {
  6. Fragment,
  7. type FunctionalComponent,
  8. createBlock,
  9. createCommentVNode,
  10. defineComponent,
  11. h,
  12. mergeProps,
  13. nextTick,
  14. onUpdated,
  15. openBlock,
  16. ref,
  17. render,
  18. withModifiers,
  19. } from '@vue/runtime-dom'
  20. import { PatchFlags } from '@vue/shared'
  21. describe('attribute fallthrough', () => {
  22. it('should allow attrs to fallthrough', async () => {
  23. const click = vi.fn()
  24. const childUpdated = vi.fn()
  25. const Hello = {
  26. setup() {
  27. const count = ref(0)
  28. function inc() {
  29. count.value++
  30. click()
  31. }
  32. return () =>
  33. h(Child, {
  34. foo: count.value + 1,
  35. id: 'test',
  36. class: 'c' + count.value,
  37. style: { color: count.value ? 'red' : 'green' },
  38. onClick: inc,
  39. 'data-id': count.value + 1,
  40. })
  41. },
  42. }
  43. const Child = {
  44. setup(props: any) {
  45. onUpdated(childUpdated)
  46. return () =>
  47. h(
  48. 'div',
  49. {
  50. class: 'c2',
  51. style: { fontWeight: 'bold' },
  52. },
  53. props.foo,
  54. )
  55. },
  56. }
  57. const root = document.createElement('div')
  58. document.body.appendChild(root)
  59. render(h(Hello), root)
  60. const node = root.children[0] as HTMLElement
  61. expect(node.getAttribute('id')).toBe('test')
  62. expect(node.getAttribute('foo')).toBe('1')
  63. expect(node.getAttribute('class')).toBe('c2 c0')
  64. expect(node.style.color).toBe('green')
  65. expect(node.style.fontWeight).toBe('bold')
  66. expect(node.dataset.id).toBe('1')
  67. node.dispatchEvent(new CustomEvent('click'))
  68. expect(click).toHaveBeenCalled()
  69. await nextTick()
  70. expect(childUpdated).toHaveBeenCalled()
  71. expect(node.getAttribute('id')).toBe('test')
  72. expect(node.getAttribute('foo')).toBe('2')
  73. expect(node.getAttribute('class')).toBe('c2 c1')
  74. expect(node.style.color).toBe('red')
  75. expect(node.style.fontWeight).toBe('bold')
  76. expect(node.dataset.id).toBe('2')
  77. })
  78. it('should only allow whitelisted fallthrough on functional component with optional props', async () => {
  79. const click = vi.fn()
  80. const childUpdated = vi.fn()
  81. const count = ref(0)
  82. function inc() {
  83. count.value++
  84. click()
  85. }
  86. const Hello = () =>
  87. h(Child, {
  88. foo: count.value + 1,
  89. id: 'test',
  90. class: 'c' + count.value,
  91. style: { color: count.value ? 'red' : 'green' },
  92. onClick: inc,
  93. })
  94. const Child = (props: any) => {
  95. childUpdated()
  96. return h(
  97. 'div',
  98. {
  99. class: 'c2',
  100. style: { fontWeight: 'bold' },
  101. },
  102. props.foo,
  103. )
  104. }
  105. const root = document.createElement('div')
  106. document.body.appendChild(root)
  107. render(h(Hello), root)
  108. const node = root.children[0] as HTMLElement
  109. // not whitelisted
  110. expect(node.getAttribute('id')).toBe(null)
  111. expect(node.getAttribute('foo')).toBe(null)
  112. // whitelisted: style, class, event listeners
  113. expect(node.getAttribute('class')).toBe('c2 c0')
  114. expect(node.style.color).toBe('green')
  115. expect(node.style.fontWeight).toBe('bold')
  116. node.dispatchEvent(new CustomEvent('click'))
  117. expect(click).toHaveBeenCalled()
  118. await nextTick()
  119. expect(childUpdated).toHaveBeenCalled()
  120. expect(node.getAttribute('id')).toBe(null)
  121. expect(node.getAttribute('foo')).toBe(null)
  122. expect(node.getAttribute('class')).toBe('c2 c1')
  123. expect(node.style.color).toBe('red')
  124. expect(node.style.fontWeight).toBe('bold')
  125. })
  126. it('should allow all attrs on functional component with declared props', async () => {
  127. const click = vi.fn()
  128. const childUpdated = vi.fn()
  129. const count = ref(0)
  130. function inc() {
  131. count.value++
  132. click()
  133. }
  134. const Hello = () =>
  135. h(Child, {
  136. foo: count.value + 1,
  137. id: 'test',
  138. class: 'c' + count.value,
  139. style: { color: count.value ? 'red' : 'green' },
  140. onClick: inc,
  141. })
  142. const Child = (props: { foo: number }) => {
  143. childUpdated()
  144. return h(
  145. 'div',
  146. {
  147. class: 'c2',
  148. style: { fontWeight: 'bold' },
  149. },
  150. props.foo,
  151. )
  152. }
  153. Child.props = ['foo']
  154. const root = document.createElement('div')
  155. document.body.appendChild(root)
  156. render(h(Hello), root)
  157. const node = root.children[0] as HTMLElement
  158. expect(node.getAttribute('id')).toBe('test')
  159. expect(node.getAttribute('foo')).toBe(null) // declared as prop
  160. expect(node.getAttribute('class')).toBe('c2 c0')
  161. expect(node.style.color).toBe('green')
  162. expect(node.style.fontWeight).toBe('bold')
  163. node.dispatchEvent(new CustomEvent('click'))
  164. expect(click).toHaveBeenCalled()
  165. await nextTick()
  166. expect(childUpdated).toHaveBeenCalled()
  167. expect(node.getAttribute('id')).toBe('test')
  168. expect(node.getAttribute('foo')).toBe(null)
  169. expect(node.getAttribute('class')).toBe('c2 c1')
  170. expect(node.style.color).toBe('red')
  171. expect(node.style.fontWeight).toBe('bold')
  172. })
  173. it('should fallthrough for nested components', async () => {
  174. const click = vi.fn()
  175. const childUpdated = vi.fn()
  176. const grandChildUpdated = vi.fn()
  177. const Hello = {
  178. setup() {
  179. const count = ref(0)
  180. function inc() {
  181. count.value++
  182. click()
  183. }
  184. return () =>
  185. h(Child, {
  186. foo: 1,
  187. id: 'test',
  188. class: 'c' + count.value,
  189. style: { color: count.value ? 'red' : 'green' },
  190. onClick: inc,
  191. })
  192. },
  193. }
  194. const Child = {
  195. setup(props: any) {
  196. onUpdated(childUpdated)
  197. // HOC simply passing props down.
  198. // this will result in merging the same attrs, but should be deduped by
  199. // `mergeProps`.
  200. return () => h(GrandChild, props)
  201. },
  202. }
  203. const GrandChild = defineComponent({
  204. props: {
  205. id: String,
  206. foo: Number,
  207. },
  208. setup(props) {
  209. onUpdated(grandChildUpdated)
  210. return () =>
  211. h(
  212. 'div',
  213. {
  214. id: props.id,
  215. class: 'c2',
  216. style: { fontWeight: 'bold' },
  217. },
  218. props.foo,
  219. )
  220. },
  221. })
  222. const root = document.createElement('div')
  223. document.body.appendChild(root)
  224. render(h(Hello), root)
  225. const node = root.children[0] as HTMLElement
  226. // with declared props, any parent attr that isn't a prop falls through
  227. expect(node.getAttribute('id')).toBe('test')
  228. expect(node.getAttribute('class')).toBe('c2 c0')
  229. expect(node.style.color).toBe('green')
  230. expect(node.style.fontWeight).toBe('bold')
  231. node.dispatchEvent(new CustomEvent('click'))
  232. expect(click).toHaveBeenCalled()
  233. // ...while declared ones remain props
  234. expect(node.hasAttribute('foo')).toBe(false)
  235. await nextTick()
  236. expect(childUpdated).toHaveBeenCalled()
  237. expect(grandChildUpdated).toHaveBeenCalled()
  238. expect(node.getAttribute('id')).toBe('test')
  239. expect(node.getAttribute('class')).toBe('c2 c1')
  240. expect(node.style.color).toBe('red')
  241. expect(node.style.fontWeight).toBe('bold')
  242. expect(node.hasAttribute('foo')).toBe(false)
  243. })
  244. it('should not fallthrough with inheritAttrs: false', () => {
  245. const Parent = {
  246. render() {
  247. return h(Child, { foo: 1, class: 'parent' })
  248. },
  249. }
  250. const Child = defineComponent({
  251. props: ['foo'],
  252. inheritAttrs: false,
  253. render() {
  254. return h('div', this.foo)
  255. },
  256. })
  257. const root = document.createElement('div')
  258. document.body.appendChild(root)
  259. render(h(Parent), root)
  260. // should not contain class
  261. expect(root.innerHTML).toMatch(`<div>1</div>`)
  262. })
  263. // #3741
  264. it('should not fallthrough with inheritAttrs: false from mixins', () => {
  265. const Parent = {
  266. render() {
  267. return h(Child, { foo: 1, class: 'parent' })
  268. },
  269. }
  270. const mixin = {
  271. inheritAttrs: false,
  272. }
  273. const Child = defineComponent({
  274. mixins: [mixin],
  275. props: ['foo'],
  276. render() {
  277. return h('div', this.foo)
  278. },
  279. })
  280. const root = document.createElement('div')
  281. document.body.appendChild(root)
  282. render(h(Parent), root)
  283. // should not contain class
  284. expect(root.innerHTML).toMatch(`<div>1</div>`)
  285. })
  286. it('explicit spreading with inheritAttrs: false', () => {
  287. const Parent = {
  288. render() {
  289. return h(Child, { foo: 1, class: 'parent' })
  290. },
  291. }
  292. const Child = defineComponent({
  293. props: ['foo'],
  294. inheritAttrs: false,
  295. render() {
  296. return h(
  297. 'div',
  298. mergeProps(
  299. {
  300. class: 'child',
  301. },
  302. this.$attrs,
  303. ),
  304. this.foo,
  305. )
  306. },
  307. })
  308. const root = document.createElement('div')
  309. document.body.appendChild(root)
  310. render(h(Parent), root)
  311. // should merge parent/child classes
  312. expect(root.innerHTML).toMatch(`<div class="child parent">1</div>`)
  313. })
  314. it('should warn when fallthrough fails on non-single-root', () => {
  315. const Parent = {
  316. render() {
  317. return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
  318. },
  319. }
  320. const Child = defineComponent({
  321. props: ['foo'],
  322. render() {
  323. return [h('div'), h('div')]
  324. },
  325. })
  326. const root = document.createElement('div')
  327. document.body.appendChild(root)
  328. render(h(Parent), root)
  329. expect(`Extraneous non-props attributes (class)`).toHaveBeenWarned()
  330. expect(`Extraneous non-emits event listeners`).toHaveBeenWarned()
  331. })
  332. it('should dedupe same listeners when $attrs is used during render', () => {
  333. const click = vi.fn()
  334. const count = ref(0)
  335. function inc() {
  336. count.value++
  337. click()
  338. }
  339. const Parent = {
  340. render() {
  341. return h(Child, { onClick: inc })
  342. },
  343. }
  344. const Child = defineComponent({
  345. render() {
  346. return h(
  347. 'div',
  348. mergeProps(
  349. {
  350. onClick: withModifiers(() => {}, ['prevent', 'stop']),
  351. },
  352. this.$attrs,
  353. ),
  354. )
  355. },
  356. })
  357. const root = document.createElement('div')
  358. document.body.appendChild(root)
  359. render(h(Parent), root)
  360. const node = root.children[0] as HTMLElement
  361. node.dispatchEvent(new CustomEvent('click'))
  362. expect(click).toHaveBeenCalledTimes(1)
  363. expect(count.value).toBe(1)
  364. })
  365. it('should not warn when $attrs is used during render', () => {
  366. const Parent = {
  367. render() {
  368. return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
  369. },
  370. }
  371. const Child = defineComponent({
  372. props: ['foo'],
  373. render() {
  374. return [h('div'), h('div', this.$attrs)]
  375. },
  376. })
  377. const root = document.createElement('div')
  378. document.body.appendChild(root)
  379. render(h(Parent), root)
  380. expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
  381. expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
  382. expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
  383. })
  384. it('should not warn when context.attrs is used during render', () => {
  385. const Parent = {
  386. render() {
  387. return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
  388. },
  389. }
  390. const Child = defineComponent({
  391. props: ['foo'],
  392. setup(_props, { attrs }) {
  393. return () => [h('div'), h('div', attrs)]
  394. },
  395. })
  396. const root = document.createElement('div')
  397. document.body.appendChild(root)
  398. render(h(Parent), root)
  399. expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
  400. expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
  401. expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
  402. })
  403. it('should not warn when context.attrs is used during render (functional)', () => {
  404. const Parent = {
  405. render() {
  406. return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
  407. },
  408. }
  409. const Child: FunctionalComponent = (_, { attrs }) => [
  410. h('div'),
  411. h('div', attrs),
  412. ]
  413. Child.props = ['foo']
  414. const root = document.createElement('div')
  415. document.body.appendChild(root)
  416. render(h(Parent), root)
  417. expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
  418. expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
  419. expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
  420. })
  421. it('should not warn when functional component has optional props', () => {
  422. const Parent = {
  423. render() {
  424. return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
  425. },
  426. }
  427. const Child = (props: any) => [h('div'), h('div', { class: props.class })]
  428. const root = document.createElement('div')
  429. document.body.appendChild(root)
  430. render(h(Parent), root)
  431. expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
  432. expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
  433. expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
  434. })
  435. it('should warn when functional component has props and does not use attrs', () => {
  436. const Parent = {
  437. render() {
  438. return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
  439. },
  440. }
  441. const Child: FunctionalComponent = () => [h('div'), h('div')]
  442. Child.props = ['foo']
  443. const root = document.createElement('div')
  444. document.body.appendChild(root)
  445. render(h(Parent), root)
  446. expect(`Extraneous non-props attributes`).toHaveBeenWarned()
  447. expect(`Extraneous non-emits event listeners`).toHaveBeenWarned()
  448. expect(root.innerHTML).toBe(`<div></div><div></div>`)
  449. })
  450. // #677
  451. it('should update merged dynamic attrs on optimized child root', async () => {
  452. const aria = ref('true')
  453. const cls = ref('bar')
  454. const Parent = {
  455. render() {
  456. return h(Child, { 'aria-hidden': aria.value, class: cls.value })
  457. },
  458. }
  459. const Child = {
  460. props: [],
  461. render() {
  462. return openBlock(), createBlock('div')
  463. },
  464. }
  465. const root = document.createElement('div')
  466. document.body.appendChild(root)
  467. render(h(Parent), root)
  468. expect(root.innerHTML).toBe(`<div aria-hidden="true" class="bar"></div>`)
  469. aria.value = 'false'
  470. await nextTick()
  471. expect(root.innerHTML).toBe(`<div aria-hidden="false" class="bar"></div>`)
  472. cls.value = 'barr'
  473. await nextTick()
  474. expect(root.innerHTML).toBe(`<div aria-hidden="false" class="barr"></div>`)
  475. })
  476. it('should not let listener fallthrough when declared in emits (stateful)', () => {
  477. const Child = defineComponent({
  478. emits: ['click'],
  479. render() {
  480. return h(
  481. 'button',
  482. {
  483. onClick: () => {
  484. this.$emit('click', 'custom')
  485. },
  486. },
  487. 'hello',
  488. )
  489. },
  490. })
  491. const onClick = vi.fn()
  492. const App = {
  493. render() {
  494. return h(Child, {
  495. onClick,
  496. })
  497. },
  498. }
  499. const root = document.createElement('div')
  500. document.body.appendChild(root)
  501. render(h(App), root)
  502. const node = root.children[0] as HTMLElement
  503. node.dispatchEvent(new CustomEvent('click'))
  504. expect(onClick).toHaveBeenCalledTimes(1)
  505. expect(onClick).toHaveBeenCalledWith('custom')
  506. })
  507. it('should not let listener fallthrough when declared in emits (functional)', () => {
  508. const Child: FunctionalComponent<{}, { click: any }> = (_, { emit }) => {
  509. // should not be in props
  510. expect((_ as any).onClick).toBeUndefined()
  511. return h('button', {
  512. onClick: () => {
  513. emit('click', 'custom')
  514. },
  515. })
  516. }
  517. Child.emits = ['click']
  518. const onClick = vi.fn()
  519. const App = {
  520. render() {
  521. return h(Child, {
  522. onClick,
  523. })
  524. },
  525. }
  526. const root = document.createElement('div')
  527. document.body.appendChild(root)
  528. render(h(App), root)
  529. const node = root.children[0] as HTMLElement
  530. node.dispatchEvent(new CustomEvent('click'))
  531. expect(onClick).toHaveBeenCalledTimes(1)
  532. expect(onClick).toHaveBeenCalledWith('custom')
  533. })
  534. it('should support fallthrough for fragments with single element + comments', () => {
  535. const click = vi.fn()
  536. const Hello = {
  537. setup() {
  538. return () => h(Child, { class: 'foo', onClick: click })
  539. },
  540. }
  541. const Child = {
  542. setup() {
  543. return () => (
  544. openBlock(),
  545. createBlock(
  546. Fragment,
  547. null,
  548. [
  549. createCommentVNode('hello'),
  550. h('button'),
  551. createCommentVNode('world'),
  552. ],
  553. PatchFlags.STABLE_FRAGMENT | PatchFlags.DEV_ROOT_FRAGMENT,
  554. )
  555. )
  556. },
  557. }
  558. const root = document.createElement('div')
  559. document.body.appendChild(root)
  560. render(h(Hello), root)
  561. expect(root.innerHTML).toBe(
  562. `<!--hello--><button class="foo"></button><!--world-->`,
  563. )
  564. const button = root.children[0] as HTMLElement
  565. button.dispatchEvent(new CustomEvent('click'))
  566. expect(click).toHaveBeenCalled()
  567. })
  568. // #1989
  569. it('should not fallthrough v-model listeners with corresponding declared prop', () => {
  570. let textFoo = ''
  571. let textBar = ''
  572. const click = vi.fn()
  573. const App = defineComponent({
  574. setup() {
  575. return () =>
  576. h(Child, {
  577. modelValue: textFoo,
  578. 'onUpdate:modelValue': (val: string) => {
  579. textFoo = val
  580. },
  581. })
  582. },
  583. })
  584. const Child = defineComponent({
  585. props: ['modelValue'],
  586. setup(_props, { emit }) {
  587. return () =>
  588. h(GrandChild, {
  589. modelValue: textBar,
  590. 'onUpdate:modelValue': (val: string) => {
  591. textBar = val
  592. emit('update:modelValue', 'from Child')
  593. },
  594. })
  595. },
  596. })
  597. const GrandChild = defineComponent({
  598. props: ['modelValue'],
  599. setup(_props, { emit }) {
  600. return () =>
  601. h('button', {
  602. onClick() {
  603. click()
  604. emit('update:modelValue', 'from GrandChild')
  605. },
  606. })
  607. },
  608. })
  609. const root = document.createElement('div')
  610. document.body.appendChild(root)
  611. render(h(App), root)
  612. const node = root.children[0] as HTMLElement
  613. node.dispatchEvent(new CustomEvent('click'))
  614. expect(click).toHaveBeenCalled()
  615. expect(textBar).toBe('from GrandChild')
  616. expect(textFoo).toBe('from Child')
  617. })
  618. })