rendererAttrsFallthrough.spec.ts 18 KB

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