TransitionGroup.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import { vi } from 'vitest'
  2. import { E2E_TIMEOUT, setupPuppeteer } from './e2eUtils'
  3. import path from 'path'
  4. import { createApp, ref } from 'vue'
  5. describe('e2e: TransitionGroup', () => {
  6. const { page, html, nextFrame, timeout } = setupPuppeteer()
  7. const baseUrl = `file://${path.resolve(__dirname, './transition.html')}`
  8. const duration = process.env.CI ? 200 : 50
  9. const buffer = process.env.CI ? 20 : 5
  10. const htmlWhenTransitionStart = () =>
  11. page().evaluate(() => {
  12. ;(document.querySelector('#toggleBtn') as any)!.click()
  13. return Promise.resolve().then(() => {
  14. return document.querySelector('#container')!.innerHTML
  15. })
  16. })
  17. const transitionFinish = (time = duration) => timeout(time + buffer)
  18. beforeEach(async () => {
  19. await page().goto(baseUrl)
  20. await page().waitForSelector('#app')
  21. })
  22. test(
  23. 'enter',
  24. async () => {
  25. await page().evaluate(() => {
  26. const { createApp, ref } = (window as any).Vue
  27. createApp({
  28. template: `
  29. <div id="container">
  30. <transition-group name="test">
  31. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  32. </transition-group>
  33. </div>
  34. <button id="toggleBtn" @click="click">button</button>
  35. `,
  36. setup: () => {
  37. const items = ref(['a', 'b', 'c'])
  38. const click = () => items.value.push('d', 'e')
  39. return { click, items }
  40. }
  41. }).mount('#app')
  42. })
  43. expect(await html('#container')).toBe(
  44. `<div class="test">a</div>` +
  45. `<div class="test">b</div>` +
  46. `<div class="test">c</div>`
  47. )
  48. expect(await htmlWhenTransitionStart()).toBe(
  49. `<div class="test">a</div>` +
  50. `<div class="test">b</div>` +
  51. `<div class="test">c</div>` +
  52. `<div class="test test-enter-from test-enter-active">d</div>` +
  53. `<div class="test test-enter-from test-enter-active">e</div>`
  54. )
  55. await nextFrame()
  56. expect(await html('#container')).toBe(
  57. `<div class="test">a</div>` +
  58. `<div class="test">b</div>` +
  59. `<div class="test">c</div>` +
  60. `<div class="test test-enter-active test-enter-to">d</div>` +
  61. `<div class="test test-enter-active test-enter-to">e</div>`
  62. )
  63. await transitionFinish()
  64. expect(await html('#container')).toBe(
  65. `<div class="test">a</div>` +
  66. `<div class="test">b</div>` +
  67. `<div class="test">c</div>` +
  68. `<div class="test">d</div>` +
  69. `<div class="test">e</div>`
  70. )
  71. },
  72. E2E_TIMEOUT
  73. )
  74. test(
  75. 'leave',
  76. async () => {
  77. await page().evaluate(() => {
  78. const { createApp, ref } = (window as any).Vue
  79. createApp({
  80. template: `
  81. <div id="container">
  82. <transition-group name="test">
  83. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  84. </transition-group>
  85. </div>
  86. <button id="toggleBtn" @click="click">button</button>
  87. `,
  88. setup: () => {
  89. const items = ref(['a', 'b', 'c'])
  90. const click = () => (items.value = ['b'])
  91. return { click, items }
  92. }
  93. }).mount('#app')
  94. })
  95. expect(await html('#container')).toBe(
  96. `<div class="test">a</div>` +
  97. `<div class="test">b</div>` +
  98. `<div class="test">c</div>`
  99. )
  100. expect(await htmlWhenTransitionStart()).toBe(
  101. `<div class="test test-leave-from test-leave-active">a</div>` +
  102. `<div class="test">b</div>` +
  103. `<div class="test test-leave-from test-leave-active">c</div>`
  104. )
  105. await nextFrame()
  106. expect(await html('#container')).toBe(
  107. `<div class="test test-leave-active test-leave-to">a</div>` +
  108. `<div class="test">b</div>` +
  109. `<div class="test test-leave-active test-leave-to">c</div>`
  110. )
  111. await transitionFinish()
  112. expect(await html('#container')).toBe(`<div class="test">b</div>`)
  113. },
  114. E2E_TIMEOUT
  115. )
  116. test(
  117. 'enter + leave',
  118. async () => {
  119. await page().evaluate(() => {
  120. const { createApp, ref } = (window as any).Vue
  121. createApp({
  122. template: `
  123. <div id="container">
  124. <transition-group name="test">
  125. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  126. </transition-group>
  127. </div>
  128. <button id="toggleBtn" @click="click">button</button>
  129. `,
  130. setup: () => {
  131. const items = ref(['a', 'b', 'c'])
  132. const click = () => (items.value = ['b', 'c', 'd'])
  133. return { click, items }
  134. }
  135. }).mount('#app')
  136. })
  137. expect(await html('#container')).toBe(
  138. `<div class="test">a</div>` +
  139. `<div class="test">b</div>` +
  140. `<div class="test">c</div>`
  141. )
  142. expect(await htmlWhenTransitionStart()).toBe(
  143. `<div class="test test-leave-from test-leave-active">a</div>` +
  144. `<div class="test">b</div>` +
  145. `<div class="test">c</div>` +
  146. `<div class="test test-enter-from test-enter-active">d</div>`
  147. )
  148. await nextFrame()
  149. expect(await html('#container')).toBe(
  150. `<div class="test test-leave-active test-leave-to">a</div>` +
  151. `<div class="test">b</div>` +
  152. `<div class="test">c</div>` +
  153. `<div class="test test-enter-active test-enter-to">d</div>`
  154. )
  155. await transitionFinish()
  156. expect(await html('#container')).toBe(
  157. `<div class="test">b</div>` +
  158. `<div class="test">c</div>` +
  159. `<div class="test">d</div>`
  160. )
  161. },
  162. E2E_TIMEOUT
  163. )
  164. test(
  165. 'appear',
  166. async () => {
  167. const appearHtml = await page().evaluate(() => {
  168. const { createApp, ref } = (window as any).Vue
  169. createApp({
  170. template: `
  171. <div id="container">
  172. <transition-group appear
  173. appear-from-class="test-appear-from"
  174. appear-to-class="test-appear-to"
  175. appear-active-class="test-appear-active"
  176. name="test">
  177. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  178. </transition-group>
  179. </div>
  180. <button id="toggleBtn" @click="click">button</button>
  181. `,
  182. setup: () => {
  183. const items = ref(['a', 'b', 'c'])
  184. const click = () => items.value.push('d', 'e')
  185. return { click, items }
  186. }
  187. }).mount('#app')
  188. return Promise.resolve().then(() => {
  189. return document.querySelector('#container')!.innerHTML
  190. })
  191. })
  192. // appear
  193. expect(appearHtml).toBe(
  194. `<div class="test test-appear-from test-appear-active">a</div>` +
  195. `<div class="test test-appear-from test-appear-active">b</div>` +
  196. `<div class="test test-appear-from test-appear-active">c</div>`
  197. )
  198. await nextFrame()
  199. expect(await html('#container')).toBe(
  200. `<div class="test test-appear-active test-appear-to">a</div>` +
  201. `<div class="test test-appear-active test-appear-to">b</div>` +
  202. `<div class="test test-appear-active test-appear-to">c</div>`
  203. )
  204. await transitionFinish()
  205. expect(await html('#container')).toBe(
  206. `<div class="test">a</div>` +
  207. `<div class="test">b</div>` +
  208. `<div class="test">c</div>`
  209. )
  210. // enter
  211. expect(await htmlWhenTransitionStart()).toBe(
  212. `<div class="test">a</div>` +
  213. `<div class="test">b</div>` +
  214. `<div class="test">c</div>` +
  215. `<div class="test test-enter-from test-enter-active">d</div>` +
  216. `<div class="test test-enter-from test-enter-active">e</div>`
  217. )
  218. await nextFrame()
  219. expect(await html('#container')).toBe(
  220. `<div class="test">a</div>` +
  221. `<div class="test">b</div>` +
  222. `<div class="test">c</div>` +
  223. `<div class="test test-enter-active test-enter-to">d</div>` +
  224. `<div class="test test-enter-active test-enter-to">e</div>`
  225. )
  226. await transitionFinish()
  227. expect(await html('#container')).toBe(
  228. `<div class="test">a</div>` +
  229. `<div class="test">b</div>` +
  230. `<div class="test">c</div>` +
  231. `<div class="test">d</div>` +
  232. `<div class="test">e</div>`
  233. )
  234. },
  235. E2E_TIMEOUT
  236. )
  237. test(
  238. 'move',
  239. async () => {
  240. await page().evaluate(() => {
  241. const { createApp, ref } = (window as any).Vue
  242. createApp({
  243. template: `
  244. <div id="container">
  245. <transition-group name="group">
  246. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  247. </transition-group>
  248. </div>
  249. <button id="toggleBtn" @click="click">button</button>
  250. `,
  251. setup: () => {
  252. const items = ref(['a', 'b', 'c'])
  253. const click = () => (items.value = ['d', 'b', 'a'])
  254. return { click, items }
  255. }
  256. }).mount('#app')
  257. })
  258. expect(await html('#container')).toBe(
  259. `<div class="test">a</div>` +
  260. `<div class="test">b</div>` +
  261. `<div class="test">c</div>`
  262. )
  263. expect(await htmlWhenTransitionStart()).toBe(
  264. `<div class="test group-enter-from group-enter-active">d</div>` +
  265. `<div class="test">b</div>` +
  266. `<div class="test group-move" style="">a</div>` +
  267. `<div class="test group-leave-from group-leave-active group-move" style="">c</div>`
  268. )
  269. await nextFrame()
  270. expect(await html('#container')).toBe(
  271. `<div class="test group-enter-active group-enter-to">d</div>` +
  272. `<div class="test">b</div>` +
  273. `<div class="test group-move" style="">a</div>` +
  274. `<div class="test group-leave-active group-move group-leave-to" style="">c</div>`
  275. )
  276. await transitionFinish(duration * 2)
  277. expect(await html('#container')).toBe(
  278. `<div class="test">d</div>` +
  279. `<div class="test">b</div>` +
  280. `<div class="test" style="">a</div>`
  281. )
  282. },
  283. E2E_TIMEOUT
  284. )
  285. test(
  286. 'dynamic name',
  287. async () => {
  288. await page().evaluate(() => {
  289. const { createApp, ref } = (window as any).Vue
  290. createApp({
  291. template: `
  292. <div id="container">
  293. <transition-group :name="name">
  294. <div v-for="item in items" :key="item" >{{item}}</div>
  295. </transition-group>
  296. </div>
  297. <button id="toggleBtn" @click="click">button</button>
  298. <button id="changeNameBtn" @click="changeName">button</button>
  299. `,
  300. setup: () => {
  301. const items = ref(['a', 'b', 'c'])
  302. const name = ref('invalid')
  303. const click = () => (items.value = ['b', 'c', 'a'])
  304. const changeName = () => {
  305. name.value = 'group'
  306. items.value = ['a', 'b', 'c']
  307. }
  308. return { click, items, name, changeName }
  309. }
  310. }).mount('#app')
  311. })
  312. expect(await html('#container')).toBe(
  313. `<div>a</div>` + `<div>b</div>` + `<div>c</div>`
  314. )
  315. // invalid name
  316. expect(await htmlWhenTransitionStart()).toBe(
  317. `<div>b</div>` + `<div>c</div>` + `<div>a</div>`
  318. )
  319. // change name
  320. const moveHtml = await page().evaluate(() => {
  321. ;(document.querySelector('#changeNameBtn') as any).click()
  322. return Promise.resolve().then(() => {
  323. return document.querySelector('#container')!.innerHTML
  324. })
  325. })
  326. expect(moveHtml).toBe(
  327. `<div class="group-move" style="">a</div>` +
  328. `<div class="group-move" style="">b</div>` +
  329. `<div class="group-move" style="">c</div>`
  330. )
  331. // not sure why but we just have to wait really long for this to
  332. // pass consistently :/
  333. await transitionFinish(duration * 4 + buffer)
  334. expect(await html('#container')).toBe(
  335. `<div class="" style="">a</div>` +
  336. `<div class="" style="">b</div>` +
  337. `<div class="" style="">c</div>`
  338. )
  339. },
  340. E2E_TIMEOUT
  341. )
  342. test(
  343. 'events',
  344. async () => {
  345. const onLeaveSpy = vi.fn()
  346. const onEnterSpy = vi.fn()
  347. const onAppearSpy = vi.fn()
  348. const beforeLeaveSpy = vi.fn()
  349. const beforeEnterSpy = vi.fn()
  350. const beforeAppearSpy = vi.fn()
  351. const afterLeaveSpy = vi.fn()
  352. const afterEnterSpy = vi.fn()
  353. const afterAppearSpy = vi.fn()
  354. await page().exposeFunction('onLeaveSpy', onLeaveSpy)
  355. await page().exposeFunction('onEnterSpy', onEnterSpy)
  356. await page().exposeFunction('onAppearSpy', onAppearSpy)
  357. await page().exposeFunction('beforeLeaveSpy', beforeLeaveSpy)
  358. await page().exposeFunction('beforeEnterSpy', beforeEnterSpy)
  359. await page().exposeFunction('beforeAppearSpy', beforeAppearSpy)
  360. await page().exposeFunction('afterLeaveSpy', afterLeaveSpy)
  361. await page().exposeFunction('afterEnterSpy', afterEnterSpy)
  362. await page().exposeFunction('afterAppearSpy', afterAppearSpy)
  363. const appearHtml = await page().evaluate(() => {
  364. const {
  365. beforeAppearSpy,
  366. onAppearSpy,
  367. afterAppearSpy,
  368. beforeEnterSpy,
  369. onEnterSpy,
  370. afterEnterSpy,
  371. beforeLeaveSpy,
  372. onLeaveSpy,
  373. afterLeaveSpy
  374. } = window as any
  375. const { createApp, ref } = (window as any).Vue
  376. createApp({
  377. template: `
  378. <div id="container">
  379. <transition-group name="test"
  380. appear
  381. appear-from-class="test-appear-from"
  382. appear-to-class="test-appear-to"
  383. appear-active-class="test-appear-active"
  384. @before-enter="beforeEnterSpy"
  385. @enter="onEnterSpy"
  386. @after-enter="afterEnterSpy"
  387. @before-leave="beforeLeaveSpy"
  388. @leave="onLeaveSpy"
  389. @after-leave="afterLeaveSpy"
  390. @before-appear="beforeAppearSpy"
  391. @appear="onAppearSpy"
  392. @after-appear="afterAppearSpy">
  393. <div v-for="item in items" :key="item" class="test">{{item}}</div>
  394. </transition-group>
  395. </div>
  396. <button id="toggleBtn" @click="click">button</button>
  397. `,
  398. setup: () => {
  399. const items = ref(['a', 'b', 'c'])
  400. const click = () => (items.value = ['b', 'c', 'd'])
  401. return {
  402. click,
  403. items,
  404. beforeAppearSpy,
  405. onAppearSpy,
  406. afterAppearSpy,
  407. beforeEnterSpy,
  408. onEnterSpy,
  409. afterEnterSpy,
  410. beforeLeaveSpy,
  411. onLeaveSpy,
  412. afterLeaveSpy
  413. }
  414. }
  415. }).mount('#app')
  416. return Promise.resolve().then(() => {
  417. return document.querySelector('#container')!.innerHTML
  418. })
  419. })
  420. expect(beforeAppearSpy).toBeCalled()
  421. expect(onAppearSpy).toBeCalled()
  422. expect(afterAppearSpy).not.toBeCalled()
  423. expect(appearHtml).toBe(
  424. `<div class="test test-appear-from test-appear-active">a</div>` +
  425. `<div class="test test-appear-from test-appear-active">b</div>` +
  426. `<div class="test test-appear-from test-appear-active">c</div>`
  427. )
  428. await nextFrame()
  429. expect(afterAppearSpy).not.toBeCalled()
  430. expect(await html('#container')).toBe(
  431. `<div class="test test-appear-active test-appear-to">a</div>` +
  432. `<div class="test test-appear-active test-appear-to">b</div>` +
  433. `<div class="test test-appear-active test-appear-to">c</div>`
  434. )
  435. await transitionFinish()
  436. expect(afterAppearSpy).toBeCalled()
  437. expect(await html('#container')).toBe(
  438. `<div class="test">a</div>` +
  439. `<div class="test">b</div>` +
  440. `<div class="test">c</div>`
  441. )
  442. // enter + leave
  443. expect(await htmlWhenTransitionStart()).toBe(
  444. `<div class="test test-leave-from test-leave-active">a</div>` +
  445. `<div class="test">b</div>` +
  446. `<div class="test">c</div>` +
  447. `<div class="test test-enter-from test-enter-active">d</div>`
  448. )
  449. expect(beforeLeaveSpy).toBeCalled()
  450. expect(onLeaveSpy).toBeCalled()
  451. expect(afterLeaveSpy).not.toBeCalled()
  452. expect(beforeEnterSpy).toBeCalled()
  453. expect(onEnterSpy).toBeCalled()
  454. expect(afterEnterSpy).not.toBeCalled()
  455. await nextFrame()
  456. expect(await html('#container')).toBe(
  457. `<div class="test test-leave-active test-leave-to">a</div>` +
  458. `<div class="test">b</div>` +
  459. `<div class="test">c</div>` +
  460. `<div class="test test-enter-active test-enter-to">d</div>`
  461. )
  462. expect(afterLeaveSpy).not.toBeCalled()
  463. expect(afterEnterSpy).not.toBeCalled()
  464. await transitionFinish()
  465. expect(await html('#container')).toBe(
  466. `<div class="test">b</div>` +
  467. `<div class="test">c</div>` +
  468. `<div class="test">d</div>`
  469. )
  470. expect(afterLeaveSpy).toBeCalled()
  471. expect(afterEnterSpy).toBeCalled()
  472. },
  473. E2E_TIMEOUT
  474. )
  475. test('warn unkeyed children', () => {
  476. createApp({
  477. template: `
  478. <transition-group name="test">
  479. <div v-for="item in items" class="test">{{item}}</div>
  480. </transition-group>
  481. `,
  482. setup: () => {
  483. const items = ref(['a', 'b', 'c'])
  484. return { items }
  485. }
  486. }).mount(document.createElement('div'))
  487. expect(`<TransitionGroup> children must be keyed`).toHaveBeenWarned()
  488. })
  489. })