rendererAttrsFallthrough.spec.ts 17 KB

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