rendererAttrsFallthrough.spec.ts 20 KB

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