TransitionGroup.spec.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. import { E2E_TIMEOUT, setupPuppeteer } from './e2eUtils'
  2. import path from 'node:path'
  3. import { createApp, ref } from 'vue'
  4. describe('e2e: TransitionGroup', () => {
  5. const { page, html, nextFrame, timeout } = setupPuppeteer()
  6. const baseUrl = `file://${path.resolve(__dirname, './transition.html')}`
  7. const duration = process.env.CI ? 200 : 50
  8. const buffer = process.env.CI ? 20 : 5
  9. const htmlWhenTransitionStart = () =>
  10. page().evaluate(() => {
  11. ;(document.querySelector('#toggleBtn') as any)!.click()
  12. return Promise.resolve().then(() => {
  13. return document.querySelector('#container')!.innerHTML
  14. })
  15. })
  16. const transitionFinish = (time = duration) => timeout(time + buffer)
  17. beforeEach(async () => {
  18. await page().goto(baseUrl)
  19. await page().waitForSelector('#app')
  20. })
  21. test(
  22. 'enter',
  23. async () => {
  24. await page().evaluate(() => {
  25. const { createApp, ref } = (window as any).Vue
  26. createApp({
  27. template: `
  28. <div id="container">
  29. <transition-group name="test">
  30. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  31. </transition-group>
  32. </div>
  33. <button id="toggleBtn" @click="click">button</button>
  34. `,
  35. setup: () => {
  36. const items = ref(['a', 'b', 'c'])
  37. const click = () => items.value.push('d', 'e')
  38. return { click, items }
  39. },
  40. }).mount('#app')
  41. })
  42. expect(await html('#container')).toBe(
  43. `<div class="test">a</div>` +
  44. `<div class="test">b</div>` +
  45. `<div class="test">c</div>`,
  46. )
  47. expect(await htmlWhenTransitionStart()).toBe(
  48. `<div class="test">a</div>` +
  49. `<div class="test">b</div>` +
  50. `<div class="test">c</div>` +
  51. `<div class="test test-enter-from test-enter-active">d</div>` +
  52. `<div class="test test-enter-from test-enter-active">e</div>`,
  53. )
  54. await nextFrame()
  55. expect(await html('#container')).toBe(
  56. `<div class="test">a</div>` +
  57. `<div class="test">b</div>` +
  58. `<div class="test">c</div>` +
  59. `<div class="test test-enter-active test-enter-to">d</div>` +
  60. `<div class="test test-enter-active test-enter-to">e</div>`,
  61. )
  62. await transitionFinish()
  63. expect(await html('#container')).toBe(
  64. `<div class="test">a</div>` +
  65. `<div class="test">b</div>` +
  66. `<div class="test">c</div>` +
  67. `<div class="test">d</div>` +
  68. `<div class="test">e</div>`,
  69. )
  70. },
  71. E2E_TIMEOUT,
  72. )
  73. test(
  74. 'leave',
  75. async () => {
  76. await page().evaluate(() => {
  77. const { createApp, ref } = (window as any).Vue
  78. createApp({
  79. template: `
  80. <div id="container">
  81. <transition-group name="test">
  82. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  83. </transition-group>
  84. </div>
  85. <button id="toggleBtn" @click="click">button</button>
  86. `,
  87. setup: () => {
  88. const items = ref(['a', 'b', 'c'])
  89. const click = () => (items.value = ['b'])
  90. return { click, items }
  91. },
  92. }).mount('#app')
  93. })
  94. expect(await html('#container')).toBe(
  95. `<div class="test">a</div>` +
  96. `<div class="test">b</div>` +
  97. `<div class="test">c</div>`,
  98. )
  99. expect(await htmlWhenTransitionStart()).toBe(
  100. `<div class="test test-leave-from test-leave-active">a</div>` +
  101. `<div class="test">b</div>` +
  102. `<div class="test test-leave-from test-leave-active">c</div>`,
  103. )
  104. await nextFrame()
  105. expect(await html('#container')).toBe(
  106. `<div class="test test-leave-active test-leave-to">a</div>` +
  107. `<div class="test">b</div>` +
  108. `<div class="test test-leave-active test-leave-to">c</div>`,
  109. )
  110. await transitionFinish()
  111. expect(await html('#container')).toBe(`<div class="test">b</div>`)
  112. },
  113. E2E_TIMEOUT,
  114. )
  115. test(
  116. 'enter + leave',
  117. async () => {
  118. await page().evaluate(() => {
  119. const { createApp, ref } = (window as any).Vue
  120. createApp({
  121. template: `
  122. <div id="container">
  123. <transition-group name="test">
  124. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  125. </transition-group>
  126. </div>
  127. <button id="toggleBtn" @click="click">button</button>
  128. `,
  129. setup: () => {
  130. const items = ref(['a', 'b', 'c'])
  131. const click = () => (items.value = ['b', 'c', 'd'])
  132. return { click, items }
  133. },
  134. }).mount('#app')
  135. })
  136. expect(await html('#container')).toBe(
  137. `<div class="test">a</div>` +
  138. `<div class="test">b</div>` +
  139. `<div class="test">c</div>`,
  140. )
  141. expect(await htmlWhenTransitionStart()).toBe(
  142. `<div class="test test-leave-from test-leave-active">a</div>` +
  143. `<div class="test">b</div>` +
  144. `<div class="test">c</div>` +
  145. `<div class="test test-enter-from test-enter-active">d</div>`,
  146. )
  147. await nextFrame()
  148. expect(await html('#container')).toBe(
  149. `<div class="test test-leave-active test-leave-to">a</div>` +
  150. `<div class="test">b</div>` +
  151. `<div class="test">c</div>` +
  152. `<div class="test test-enter-active test-enter-to">d</div>`,
  153. )
  154. await transitionFinish()
  155. expect(await html('#container')).toBe(
  156. `<div class="test">b</div>` +
  157. `<div class="test">c</div>` +
  158. `<div class="test">d</div>`,
  159. )
  160. },
  161. E2E_TIMEOUT,
  162. )
  163. test(
  164. 'appear',
  165. async () => {
  166. const appearHtml = await page().evaluate(() => {
  167. const { createApp, ref } = (window as any).Vue
  168. createApp({
  169. template: `
  170. <div id="container">
  171. <transition-group appear
  172. appear-from-class="test-appear-from"
  173. appear-to-class="test-appear-to"
  174. appear-active-class="test-appear-active"
  175. name="test">
  176. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  177. </transition-group>
  178. </div>
  179. <button id="toggleBtn" @click="click">button</button>
  180. `,
  181. setup: () => {
  182. const items = ref(['a', 'b', 'c'])
  183. const click = () => items.value.push('d', 'e')
  184. return { click, items }
  185. },
  186. }).mount('#app')
  187. return Promise.resolve().then(() => {
  188. return document.querySelector('#container')!.innerHTML
  189. })
  190. })
  191. // appear
  192. expect(appearHtml).toBe(
  193. `<div class="test test-appear-from test-appear-active">a</div>` +
  194. `<div class="test test-appear-from test-appear-active">b</div>` +
  195. `<div class="test test-appear-from test-appear-active">c</div>`,
  196. )
  197. await nextFrame()
  198. expect(await html('#container')).toBe(
  199. `<div class="test test-appear-active test-appear-to">a</div>` +
  200. `<div class="test test-appear-active test-appear-to">b</div>` +
  201. `<div class="test test-appear-active test-appear-to">c</div>`,
  202. )
  203. await transitionFinish()
  204. expect(await html('#container')).toBe(
  205. `<div class="test">a</div>` +
  206. `<div class="test">b</div>` +
  207. `<div class="test">c</div>`,
  208. )
  209. // enter
  210. expect(await htmlWhenTransitionStart()).toBe(
  211. `<div class="test">a</div>` +
  212. `<div class="test">b</div>` +
  213. `<div class="test">c</div>` +
  214. `<div class="test test-enter-from test-enter-active">d</div>` +
  215. `<div class="test test-enter-from test-enter-active">e</div>`,
  216. )
  217. await nextFrame()
  218. expect(await html('#container')).toBe(
  219. `<div class="test">a</div>` +
  220. `<div class="test">b</div>` +
  221. `<div class="test">c</div>` +
  222. `<div class="test test-enter-active test-enter-to">d</div>` +
  223. `<div class="test test-enter-active test-enter-to">e</div>`,
  224. )
  225. await transitionFinish()
  226. expect(await html('#container')).toBe(
  227. `<div class="test">a</div>` +
  228. `<div class="test">b</div>` +
  229. `<div class="test">c</div>` +
  230. `<div class="test">d</div>` +
  231. `<div class="test">e</div>`,
  232. )
  233. },
  234. E2E_TIMEOUT,
  235. )
  236. test(
  237. 'move',
  238. async () => {
  239. await page().evaluate(() => {
  240. const { createApp, ref } = (window as any).Vue
  241. createApp({
  242. template: `
  243. <div id="container">
  244. <transition-group name="group">
  245. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  246. </transition-group>
  247. </div>
  248. <button id="toggleBtn" @click="click">button</button>
  249. `,
  250. setup: () => {
  251. const items = ref(['a', 'b', 'c'])
  252. const click = () => (items.value = ['d', 'b', 'a'])
  253. return { click, items }
  254. },
  255. }).mount('#app')
  256. })
  257. expect(await html('#container')).toBe(
  258. `<div class="test">a</div>` +
  259. `<div class="test">b</div>` +
  260. `<div class="test">c</div>`,
  261. )
  262. expect(await htmlWhenTransitionStart()).toBe(
  263. `<div class="test group-enter-from group-enter-active">d</div>` +
  264. `<div class="test">b</div>` +
  265. `<div class="test group-move" style="">a</div>` +
  266. `<div class="test group-leave-from group-leave-active group-move" style="">c</div>`,
  267. )
  268. await nextFrame()
  269. expect(await html('#container')).toBe(
  270. `<div class="test group-enter-active group-enter-to">d</div>` +
  271. `<div class="test">b</div>` +
  272. `<div class="test group-move" style="">a</div>` +
  273. `<div class="test group-leave-active group-move group-leave-to" style="">c</div>`,
  274. )
  275. await transitionFinish(duration * 2)
  276. expect(await html('#container')).toBe(
  277. `<div class="test">d</div>` +
  278. `<div class="test">b</div>` +
  279. `<div class="test" style="">a</div>`,
  280. )
  281. },
  282. E2E_TIMEOUT,
  283. )
  284. test(
  285. 'move while entering',
  286. async () => {
  287. await page().evaluate(duration => {
  288. const { createApp, ref, onMounted } = (window as any).Vue
  289. createApp({
  290. template: `
  291. <transition-group name="toasts" tag="div" id="toasts">
  292. <div class="toast" v-for="toast in list" :key="toast.id">
  293. {{ toast.text }} #{{ toast.id }}
  294. </div>
  295. </transition-group>
  296. <button id="addBtn" @click="add">button</button>
  297. `,
  298. setup: () => {
  299. const list = ref([])
  300. let id = 0
  301. const add = () => {
  302. if (list.value.length > 3) {
  303. list.value.splice(0, 1)
  304. }
  305. list.value.push({
  306. id,
  307. type: 'error',
  308. text: 'Test message',
  309. })
  310. id++
  311. }
  312. onMounted(() => {
  313. const styleNode = document.createElement('style')
  314. styleNode.innerHTML = `
  315. #toasts {
  316. position: absolute;
  317. bottom: 0;
  318. left: 0;
  319. }
  320. #toasts > .toast {
  321. width: 150px;
  322. margin-bottom: 10px;
  323. height: 30px;
  324. color: white;
  325. background: black;
  326. }
  327. .toasts-leave-active {
  328. position: absolute;
  329. }
  330. .toasts-move { transition: transform ${duration}ms ease; }
  331. `
  332. document.body.appendChild(styleNode)
  333. })
  334. return { list, add }
  335. },
  336. }).mount('#app')
  337. }, duration)
  338. const overlapDelay = Math.max(10, Math.floor(duration / 2))
  339. const { midTop, finalTop } = await page().evaluate(
  340. ({ overlapDelay, duration, buffer }) => {
  341. ;(document.querySelector('#addBtn') as any)!.click()
  342. return new Promise<{ midTop: number; finalTop: number }>(resolve => {
  343. setTimeout(() => {
  344. ;(document.querySelector('#addBtn') as any)!.click()
  345. Promise.resolve().then(() => {
  346. const nodes = Array.from(
  347. document.querySelectorAll('#toasts .toast'),
  348. ) as HTMLElement[]
  349. const firstToast = nodes.find(node =>
  350. node.textContent?.includes('#0'),
  351. )
  352. const midTop = firstToast
  353. ? firstToast.getBoundingClientRect().top
  354. : NaN
  355. setTimeout(() => {
  356. const finalTop = firstToast
  357. ? firstToast.getBoundingClientRect().top
  358. : NaN
  359. resolve({ midTop, finalTop })
  360. }, duration + buffer)
  361. })
  362. }, overlapDelay)
  363. })
  364. },
  365. { overlapDelay, duration, buffer },
  366. )
  367. expect(midTop).toBeGreaterThan(finalTop)
  368. },
  369. E2E_TIMEOUT,
  370. )
  371. test(
  372. 'dynamic name',
  373. async () => {
  374. await page().evaluate(() => {
  375. const { createApp, ref } = (window as any).Vue
  376. createApp({
  377. template: `
  378. <div id="container">
  379. <transition-group :name="name">
  380. <div v-for="item in items" :key="item" >{{item}}</div>
  381. </transition-group>
  382. </div>
  383. <button id="toggleBtn" @click="click">button</button>
  384. <button id="changeNameBtn" @click="changeName">button</button>
  385. `,
  386. setup: () => {
  387. const items = ref(['a', 'b', 'c'])
  388. const name = ref('invalid')
  389. const click = () => (items.value = ['b', 'c', 'a'])
  390. const changeName = () => {
  391. name.value = 'group'
  392. items.value = ['a', 'b', 'c']
  393. }
  394. return { click, items, name, changeName }
  395. },
  396. }).mount('#app')
  397. })
  398. expect(await html('#container')).toBe(
  399. `<div>a</div>` + `<div>b</div>` + `<div>c</div>`,
  400. )
  401. // invalid name
  402. expect(await htmlWhenTransitionStart()).toBe(
  403. `<div>b</div>` + `<div>c</div>` + `<div>a</div>`,
  404. )
  405. // change name
  406. const moveHtml = await page().evaluate(() => {
  407. ;(document.querySelector('#changeNameBtn') as any).click()
  408. return Promise.resolve().then(() => {
  409. return document.querySelector('#container')!.innerHTML
  410. })
  411. })
  412. expect(moveHtml).toBe(
  413. `<div class="group-move" style="">a</div>` +
  414. `<div class="group-move" style="">b</div>` +
  415. `<div class="group-move" style="">c</div>`,
  416. )
  417. // not sure why but we just have to wait really long for this to
  418. // pass consistently :/
  419. await transitionFinish(duration * 4 + buffer)
  420. expect(await html('#container')).toBe(
  421. `<div class="" style="">a</div>` +
  422. `<div class="" style="">b</div>` +
  423. `<div class="" style="">c</div>`,
  424. )
  425. },
  426. E2E_TIMEOUT,
  427. )
  428. test(
  429. 'events',
  430. async () => {
  431. const onLeaveSpy = vi.fn()
  432. const onEnterSpy = vi.fn()
  433. const onAppearSpy = vi.fn()
  434. const beforeLeaveSpy = vi.fn()
  435. const beforeEnterSpy = vi.fn()
  436. const beforeAppearSpy = vi.fn()
  437. const afterLeaveSpy = vi.fn()
  438. const afterEnterSpy = vi.fn()
  439. const afterAppearSpy = vi.fn()
  440. await page().exposeFunction('onLeaveSpy', onLeaveSpy)
  441. await page().exposeFunction('onEnterSpy', onEnterSpy)
  442. await page().exposeFunction('onAppearSpy', onAppearSpy)
  443. await page().exposeFunction('beforeLeaveSpy', beforeLeaveSpy)
  444. await page().exposeFunction('beforeEnterSpy', beforeEnterSpy)
  445. await page().exposeFunction('beforeAppearSpy', beforeAppearSpy)
  446. await page().exposeFunction('afterLeaveSpy', afterLeaveSpy)
  447. await page().exposeFunction('afterEnterSpy', afterEnterSpy)
  448. await page().exposeFunction('afterAppearSpy', afterAppearSpy)
  449. const appearHtml = await page().evaluate(() => {
  450. const {
  451. beforeAppearSpy,
  452. onAppearSpy,
  453. afterAppearSpy,
  454. beforeEnterSpy,
  455. onEnterSpy,
  456. afterEnterSpy,
  457. beforeLeaveSpy,
  458. onLeaveSpy,
  459. afterLeaveSpy,
  460. } = window as any
  461. const { createApp, ref } = (window as any).Vue
  462. createApp({
  463. template: `
  464. <div id="container">
  465. <transition-group name="test"
  466. appear
  467. appear-from-class="test-appear-from"
  468. appear-to-class="test-appear-to"
  469. appear-active-class="test-appear-active"
  470. @before-enter="beforeEnterSpy()"
  471. @enter="onEnterSpy()"
  472. @after-enter="afterEnterSpy()"
  473. @before-leave="beforeLeaveSpy()"
  474. @leave="onLeaveSpy()"
  475. @after-leave="afterLeaveSpy()"
  476. @before-appear="beforeAppearSpy()"
  477. @appear="onAppearSpy()"
  478. @after-appear="afterAppearSpy()">
  479. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  480. </transition-group>
  481. </div>
  482. <button id="toggleBtn" @click="click">button</button>
  483. `,
  484. setup: () => {
  485. const items = ref(['a', 'b', 'c'])
  486. const click = () => (items.value = ['b', 'c', 'd'])
  487. return {
  488. click,
  489. items,
  490. beforeAppearSpy,
  491. onAppearSpy,
  492. afterAppearSpy,
  493. beforeEnterSpy,
  494. onEnterSpy,
  495. afterEnterSpy,
  496. beforeLeaveSpy,
  497. onLeaveSpy,
  498. afterLeaveSpy,
  499. }
  500. },
  501. }).mount('#app')
  502. return Promise.resolve().then(() => {
  503. return document.querySelector('#container')!.innerHTML
  504. })
  505. })
  506. expect(beforeAppearSpy).toBeCalled()
  507. expect(onAppearSpy).toBeCalled()
  508. expect(afterAppearSpy).not.toBeCalled()
  509. expect(appearHtml).toBe(
  510. `<div class="test test-appear-from test-appear-active">a</div>` +
  511. `<div class="test test-appear-from test-appear-active">b</div>` +
  512. `<div class="test test-appear-from test-appear-active">c</div>`,
  513. )
  514. await nextFrame()
  515. expect(afterAppearSpy).not.toBeCalled()
  516. expect(await html('#container')).toBe(
  517. `<div class="test test-appear-active test-appear-to">a</div>` +
  518. `<div class="test test-appear-active test-appear-to">b</div>` +
  519. `<div class="test test-appear-active test-appear-to">c</div>`,
  520. )
  521. await transitionFinish()
  522. expect(afterAppearSpy).toBeCalled()
  523. expect(await html('#container')).toBe(
  524. `<div class="test">a</div>` +
  525. `<div class="test">b</div>` +
  526. `<div class="test">c</div>`,
  527. )
  528. // enter + leave
  529. expect(await htmlWhenTransitionStart()).toBe(
  530. `<div class="test test-leave-from test-leave-active">a</div>` +
  531. `<div class="test">b</div>` +
  532. `<div class="test">c</div>` +
  533. `<div class="test test-enter-from test-enter-active">d</div>`,
  534. )
  535. expect(beforeLeaveSpy).toBeCalled()
  536. expect(onLeaveSpy).toBeCalled()
  537. expect(afterLeaveSpy).not.toBeCalled()
  538. expect(beforeEnterSpy).toBeCalled()
  539. expect(onEnterSpy).toBeCalled()
  540. expect(afterEnterSpy).not.toBeCalled()
  541. await nextFrame()
  542. expect(await html('#container')).toBe(
  543. `<div class="test test-leave-active test-leave-to">a</div>` +
  544. `<div class="test">b</div>` +
  545. `<div class="test">c</div>` +
  546. `<div class="test test-enter-active test-enter-to">d</div>`,
  547. )
  548. expect(afterLeaveSpy).not.toBeCalled()
  549. expect(afterEnterSpy).not.toBeCalled()
  550. await transitionFinish()
  551. expect(await html('#container')).toBe(
  552. `<div class="test">b</div>` +
  553. `<div class="test">c</div>` +
  554. `<div class="test">d</div>`,
  555. )
  556. expect(afterLeaveSpy).toBeCalled()
  557. expect(afterEnterSpy).toBeCalled()
  558. },
  559. E2E_TIMEOUT,
  560. )
  561. test('warn unkeyed children', () => {
  562. createApp({
  563. template: `
  564. <transition-group name="test">
  565. <div v-for="item in items" class="test">{{item}}</div>
  566. </transition-group>
  567. `,
  568. setup: () => {
  569. const items = ref(['a', 'b', 'c'])
  570. return { items }
  571. },
  572. }).mount(document.createElement('div'))
  573. expect(`<TransitionGroup> children must be keyed`).toHaveBeenWarned()
  574. })
  575. test('not warn unkeyed text children w/ whitespace preserve', () => {
  576. const app = createApp({
  577. template: `
  578. <transition-group name="test">
  579. <p key="1">1</p>
  580. <p key="2" v-if="false">2</p>
  581. </transition-group>
  582. `,
  583. })
  584. app.config.compilerOptions.whitespace = 'preserve'
  585. app.mount(document.createElement('div'))
  586. expect(`<TransitionGroup> children must be keyed`).not.toHaveBeenWarned()
  587. })
  588. // #5168, #7898, #9067
  589. test(
  590. 'avoid set transition hooks for comment node',
  591. async () => {
  592. await page().evaluate(duration => {
  593. const { createApp, ref, h, createCommentVNode } = (window as any).Vue
  594. const show = ref(false)
  595. createApp({
  596. template: `
  597. <div id="container">
  598. <transition-group name="test">
  599. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  600. <Child key="child"/>
  601. </transition-group>
  602. </div>
  603. <button id="toggleBtn" @click="click">button</button>
  604. `,
  605. components: {
  606. Child: {
  607. setup() {
  608. return () =>
  609. show.value
  610. ? h('div', { class: 'test' }, 'child')
  611. : createCommentVNode('v-if', true)
  612. },
  613. },
  614. },
  615. setup: () => {
  616. const items = ref([])
  617. const click = () => {
  618. items.value = ['a', 'b', 'c']
  619. setTimeout(() => {
  620. show.value = true
  621. }, duration)
  622. }
  623. return { click, items }
  624. },
  625. }).mount('#app')
  626. }, duration)
  627. expect(await html('#container')).toBe(`<!--v-if-->`)
  628. expect(await htmlWhenTransitionStart()).toBe(
  629. `<div class="test test-enter-from test-enter-active">a</div>` +
  630. `<div class="test test-enter-from test-enter-active">b</div>` +
  631. `<div class="test test-enter-from test-enter-active">c</div>` +
  632. `<!--v-if-->`,
  633. )
  634. await transitionFinish(duration)
  635. await nextFrame()
  636. expect(await html('#container')).toBe(
  637. `<div class="test">a</div>` +
  638. `<div class="test">b</div>` +
  639. `<div class="test">c</div>` +
  640. `<div class="test test-enter-active test-enter-to">child</div>`,
  641. )
  642. await transitionFinish(duration)
  643. expect(await html('#container')).toBe(
  644. `<div class="test">a</div>` +
  645. `<div class="test">b</div>` +
  646. `<div class="test">c</div>` +
  647. `<div class="test">child</div>`,
  648. )
  649. },
  650. E2E_TIMEOUT,
  651. )
  652. // #4621, #4622, #5153
  653. test(
  654. 'avoid set transition hooks for text node',
  655. async () => {
  656. await page().evaluate(() => {
  657. const { createApp, ref } = (window as any).Vue
  658. const app = createApp({
  659. template: `
  660. <div id="container">
  661. <transition-group name="test">
  662. <div class="test">foo</div>
  663. <div class="test" v-if="show">bar</div>
  664. </transition-group>
  665. </div>
  666. <button id="toggleBtn" @click="click">button</button>
  667. `,
  668. setup: () => {
  669. const show = ref(false)
  670. const click = () => {
  671. show.value = true
  672. }
  673. return { show, click }
  674. },
  675. })
  676. app.config.compilerOptions.whitespace = 'preserve'
  677. app.mount('#app')
  678. })
  679. expect(await html('#container')).toBe(`<div class="test">foo</div>` + ` `)
  680. expect(await htmlWhenTransitionStart()).toBe(
  681. `<div class="test">foo</div>` +
  682. ` ` +
  683. `<div class="test test-enter-from test-enter-active">bar</div>`,
  684. )
  685. await nextFrame()
  686. expect(await html('#container')).toBe(
  687. `<div class="test">foo</div>` +
  688. ` ` +
  689. `<div class="test test-enter-active test-enter-to">bar</div>`,
  690. )
  691. await transitionFinish(duration)
  692. expect(await html('#container')).toBe(
  693. `<div class="test">foo</div>` + ` ` + `<div class="test">bar</div>`,
  694. )
  695. },
  696. E2E_TIMEOUT,
  697. )
  698. // #6105
  699. test(
  700. 'with scale',
  701. async () => {
  702. await page().evaluate(() => {
  703. const { createApp, ref, onMounted } = (window as any).Vue
  704. createApp({
  705. template: `
  706. <div id="container">
  707. <div class="scale" style="transform: scale(2) translateX(50%) translateY(50%)">
  708. <transition-group tag="ul">
  709. <li v-for="item in items" :key="item">{{item}}</li>
  710. </transition-group>
  711. <button id="toggleBtn" @click="click">button</button>
  712. </div>
  713. </div>
  714. `,
  715. setup: () => {
  716. const items = ref(['a', 'b', 'c'])
  717. const click = () => {
  718. items.value.reverse()
  719. }
  720. onMounted(() => {
  721. const styleNode = document.createElement('style')
  722. styleNode.innerHTML = `.v-move {
  723. transition: transform 0.5s ease;
  724. }`
  725. document.body.appendChild(styleNode)
  726. })
  727. return { items, click }
  728. },
  729. }).mount('#app')
  730. })
  731. const original_top = await page().$eval('ul li:nth-child(1)', node => {
  732. return node.getBoundingClientRect().top
  733. })
  734. const new_top = await page().evaluate(() => {
  735. const el = document.querySelector('ul li:nth-child(1)')
  736. const p = new Promise(resolve => {
  737. el!.addEventListener('transitionstart', () => {
  738. const new_top = el!.getBoundingClientRect().top
  739. resolve(new_top)
  740. })
  741. })
  742. ;(document.querySelector('#toggleBtn') as any)!.click()
  743. return p
  744. })
  745. expect(original_top).toBeLessThan(new_top as number)
  746. },
  747. E2E_TIMEOUT,
  748. )
  749. test(
  750. 'not leaking after children unmounted',
  751. async () => {
  752. const client = await page().createCDPSession()
  753. await page().evaluate(async () => {
  754. const { createApp, ref, nextTick } = (window as any).Vue
  755. const show = ref(true)
  756. createApp({
  757. components: {
  758. Child: {
  759. setup: () => {
  760. // Big arrays kick GC earlier
  761. const test = ref([...Array(3000)].map((_, i) => ({ i })))
  762. // @ts-expect-error - Custom property and same lib as runtime is used
  763. window.__REF__ = new WeakRef(test)
  764. return { test }
  765. },
  766. template: `
  767. <p>{{ test.length }}</p>
  768. `,
  769. },
  770. },
  771. template: `
  772. <transition-group>
  773. <Child v-if="show" />
  774. </transition-group>
  775. `,
  776. setup() {
  777. return { show }
  778. },
  779. }).mount('#app')
  780. show.value = false
  781. await nextTick()
  782. })
  783. const isCollected = async () =>
  784. // @ts-expect-error - Custom property
  785. await page().evaluate(() => window.__REF__.deref() === undefined)
  786. while ((await isCollected()) === false) {
  787. await client.send('HeapProfiler.collectGarbage')
  788. }
  789. expect(await isCollected()).toBe(true)
  790. },
  791. E2E_TIMEOUT,
  792. )
  793. })