transition.spec.ts 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. import Vue from 'vue'
  2. import { injectStyles, waitForUpdate, nextFrame } from './helpers'
  3. describe('Transition basic', () => {
  4. const { duration, buffer } = injectStyles() as {
  5. duration: number
  6. buffer: number
  7. }
  8. const explicitDuration = duration * 2
  9. let el
  10. beforeEach(() => {
  11. el = document.createElement('div')
  12. document.body.appendChild(el)
  13. })
  14. it('basic transition', done => {
  15. const vm = new Vue({
  16. template:
  17. '<div><transition><div v-if="ok" class="test">foo</div></transition></div>',
  18. data: { ok: true }
  19. }).$mount(el)
  20. // should not apply transition on initial render by default
  21. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  22. vm.ok = false
  23. waitForUpdate(() => {
  24. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  25. })
  26. .thenWaitFor(nextFrame)
  27. .then(() => {
  28. expect(vm.$el.children[0].className).toBe(
  29. 'test v-leave-active v-leave-to'
  30. )
  31. })
  32. .thenWaitFor(duration + buffer)
  33. .then(() => {
  34. expect(vm.$el.children.length).toBe(0)
  35. vm.ok = true
  36. })
  37. .then(() => {
  38. expect(vm.$el.children[0].className).toBe('test v-enter v-enter-active')
  39. })
  40. .thenWaitFor(nextFrame)
  41. .then(() => {
  42. expect(vm.$el.children[0].className).toBe(
  43. 'test v-enter-active v-enter-to'
  44. )
  45. })
  46. .thenWaitFor(duration + buffer)
  47. .then(() => {
  48. expect(vm.$el.children[0].className).toBe('test')
  49. })
  50. .then(done)
  51. })
  52. it('named transition', done => {
  53. const vm = new Vue({
  54. template:
  55. '<div><transition name="test"><div v-if="ok" class="test">foo</div></transition></div>',
  56. data: { ok: true }
  57. }).$mount(el)
  58. // should not apply transition on initial render by default
  59. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  60. vm.ok = false
  61. waitForUpdate(() => {
  62. expect(vm.$el.children[0].className).toBe(
  63. 'test test-leave test-leave-active'
  64. )
  65. })
  66. .thenWaitFor(nextFrame)
  67. .then(() => {
  68. expect(vm.$el.children[0].className).toBe(
  69. 'test test-leave-active test-leave-to'
  70. )
  71. })
  72. .thenWaitFor(duration + buffer)
  73. .then(() => {
  74. expect(vm.$el.children.length).toBe(0)
  75. vm.ok = true
  76. })
  77. .then(() => {
  78. expect(vm.$el.children[0].className).toBe(
  79. 'test test-enter test-enter-active'
  80. )
  81. })
  82. .thenWaitFor(nextFrame)
  83. .then(() => {
  84. expect(vm.$el.children[0].className).toBe(
  85. 'test test-enter-active test-enter-to'
  86. )
  87. })
  88. .thenWaitFor(duration + buffer)
  89. .then(() => {
  90. expect(vm.$el.children[0].className).toBe('test')
  91. })
  92. .then(done)
  93. })
  94. it('custom transition classes', done => {
  95. const vm = new Vue({
  96. template: `
  97. <div>
  98. <transition
  99. enter-class="hello"
  100. enter-active-class="hello-active"
  101. enter-to-class="hello-to"
  102. leave-class="bye"
  103. leave-to-class="bye-to"
  104. leave-active-class="byebye active more ">
  105. <div v-if="ok" class="test">foo</div>
  106. </transition>
  107. </div>
  108. `,
  109. data: { ok: true }
  110. }).$mount(el)
  111. // should not apply transition on initial render by default
  112. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  113. vm.ok = false
  114. waitForUpdate(() => {
  115. expect(vm.$el.children[0].className).toBe('test bye byebye active more')
  116. })
  117. .thenWaitFor(nextFrame)
  118. .then(() => {
  119. expect(vm.$el.children[0].className).toBe(
  120. 'test byebye active more bye-to'
  121. )
  122. })
  123. .thenWaitFor(duration + buffer)
  124. .then(() => {
  125. expect(vm.$el.children.length).toBe(0)
  126. vm.ok = true
  127. })
  128. .then(() => {
  129. expect(vm.$el.children[0].className).toBe('test hello hello-active')
  130. })
  131. .thenWaitFor(nextFrame)
  132. .then(() => {
  133. expect(vm.$el.children[0].className).toBe('test hello-active hello-to')
  134. })
  135. .thenWaitFor(duration + buffer)
  136. .then(() => {
  137. expect(vm.$el.children[0].className).toBe('test')
  138. })
  139. .then(done)
  140. })
  141. it('dynamic transition', done => {
  142. const vm = new Vue({
  143. template: `
  144. <div>
  145. <transition :name="trans">
  146. <div v-if="ok" class="test">foo</div>
  147. </transition>
  148. </div>
  149. `,
  150. data: {
  151. ok: true,
  152. trans: 'test'
  153. }
  154. }).$mount(el)
  155. // should not apply transition on initial render by default
  156. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  157. vm.ok = false
  158. waitForUpdate(() => {
  159. expect(vm.$el.children[0].className).toBe(
  160. 'test test-leave test-leave-active'
  161. )
  162. })
  163. .thenWaitFor(nextFrame)
  164. .then(() => {
  165. expect(vm.$el.children[0].className).toBe(
  166. 'test test-leave-active test-leave-to'
  167. )
  168. })
  169. .thenWaitFor(duration + buffer)
  170. .then(() => {
  171. expect(vm.$el.children.length).toBe(0)
  172. vm.ok = true
  173. vm.trans = 'changed'
  174. })
  175. .then(() => {
  176. expect(vm.$el.children[0].className).toBe(
  177. 'test changed-enter changed-enter-active'
  178. )
  179. })
  180. .thenWaitFor(nextFrame)
  181. .then(() => {
  182. expect(vm.$el.children[0].className).toBe(
  183. 'test changed-enter-active changed-enter-to'
  184. )
  185. })
  186. .thenWaitFor(duration + buffer)
  187. .then(() => {
  188. expect(vm.$el.children[0].className).toBe('test')
  189. })
  190. .then(done)
  191. })
  192. it('inline transition object', done => {
  193. const enter = jasmine.createSpy()
  194. const leave = jasmine.createSpy()
  195. const vm = new Vue({
  196. render(h) {
  197. return h('div', null, [
  198. h(
  199. 'transition',
  200. {
  201. props: {
  202. name: 'inline',
  203. enterClass: 'hello',
  204. enterToClass: 'hello-to',
  205. enterActiveClass: 'hello-active',
  206. leaveClass: 'bye',
  207. leaveToClass: 'bye-to',
  208. leaveActiveClass: 'byebye active'
  209. },
  210. on: {
  211. enter,
  212. leave
  213. }
  214. },
  215. this.ok ? [h('div', { class: 'test' }, 'foo')] : undefined
  216. )
  217. ])
  218. },
  219. data: { ok: true }
  220. }).$mount(el)
  221. // should not apply transition on initial render by default
  222. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  223. vm.ok = false
  224. waitForUpdate(() => {
  225. expect(vm.$el.children[0].className).toBe('test bye byebye active')
  226. expect(leave).toHaveBeenCalled()
  227. })
  228. .thenWaitFor(nextFrame)
  229. .then(() => {
  230. expect(vm.$el.children[0].className).toBe('test byebye active bye-to')
  231. })
  232. .thenWaitFor(duration + buffer)
  233. .then(() => {
  234. expect(vm.$el.children.length).toBe(0)
  235. vm.ok = true
  236. })
  237. .then(() => {
  238. expect(vm.$el.children[0].className).toBe('test hello hello-active')
  239. expect(enter).toHaveBeenCalled()
  240. })
  241. .thenWaitFor(nextFrame)
  242. .then(() => {
  243. expect(vm.$el.children[0].className).toBe('test hello-active hello-to')
  244. })
  245. .thenWaitFor(duration + buffer)
  246. .then(() => {
  247. expect(vm.$el.children[0].className).toBe('test')
  248. })
  249. .then(done)
  250. })
  251. it('transition events', done => {
  252. const onLeaveSpy = jasmine.createSpy()
  253. const onEnterSpy = jasmine.createSpy()
  254. const beforeLeaveSpy = jasmine.createSpy()
  255. const beforeEnterSpy = jasmine.createSpy()
  256. const afterLeaveSpy = jasmine.createSpy()
  257. const afterEnterSpy = jasmine.createSpy()
  258. const vm = new Vue({
  259. template: `
  260. <div>
  261. <transition
  262. name="test"
  263. @before-enter="beforeEnter"
  264. @enter="enter"
  265. @after-enter="afterEnter"
  266. @before-leave="beforeLeave"
  267. @leave="leave"
  268. @after-leave="afterLeave">
  269. <div v-if="ok" class="test">foo</div>
  270. </transition>
  271. </div>
  272. `,
  273. data: { ok: true },
  274. methods: {
  275. beforeLeave: el => {
  276. expect(el).toBe(vm.$el.children[0])
  277. expect(el.className).toBe('test')
  278. beforeLeaveSpy(el)
  279. },
  280. leave: el => onLeaveSpy(el),
  281. afterLeave: el => afterLeaveSpy(el),
  282. beforeEnter: el => {
  283. expect(vm.$el.contains(el)).toBe(false)
  284. expect(el.className).toBe('test')
  285. beforeEnterSpy(el)
  286. },
  287. enter: el => {
  288. expect(vm.$el.contains(el)).toBe(true)
  289. onEnterSpy(el)
  290. },
  291. afterEnter: el => afterEnterSpy(el)
  292. }
  293. }).$mount(el)
  294. // should not apply transition on initial render by default
  295. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  296. let _el = vm.$el.children[0]
  297. vm.ok = false
  298. waitForUpdate(() => {
  299. expect(beforeLeaveSpy).toHaveBeenCalledWith(_el)
  300. expect(onLeaveSpy).toHaveBeenCalledWith(_el)
  301. expect(vm.$el.children[0].className).toBe(
  302. 'test test-leave test-leave-active'
  303. )
  304. })
  305. .thenWaitFor(nextFrame)
  306. .then(() => {
  307. expect(afterLeaveSpy).not.toHaveBeenCalled()
  308. expect(vm.$el.children[0].className).toBe(
  309. 'test test-leave-active test-leave-to'
  310. )
  311. })
  312. .thenWaitFor(duration + buffer)
  313. .then(() => {
  314. expect(afterLeaveSpy).toHaveBeenCalledWith(_el)
  315. expect(vm.$el.children.length).toBe(0)
  316. vm.ok = true
  317. })
  318. .then(() => {
  319. _el = vm.$el.children[0]
  320. expect(beforeEnterSpy).toHaveBeenCalledWith(_el)
  321. expect(onEnterSpy).toHaveBeenCalledWith(_el)
  322. expect(vm.$el.children[0].className).toBe(
  323. 'test test-enter test-enter-active'
  324. )
  325. })
  326. .thenWaitFor(nextFrame)
  327. .then(() => {
  328. expect(afterEnterSpy).not.toHaveBeenCalled()
  329. expect(vm.$el.children[0].className).toBe(
  330. 'test test-enter-active test-enter-to'
  331. )
  332. })
  333. .thenWaitFor(duration + buffer)
  334. .then(() => {
  335. expect(afterEnterSpy).toHaveBeenCalledWith(_el)
  336. expect(vm.$el.children[0].className).toBe('test')
  337. })
  338. .then(done)
  339. })
  340. it('transition events (v-show)', done => {
  341. const onLeaveSpy = jasmine.createSpy()
  342. const onEnterSpy = jasmine.createSpy()
  343. const beforeLeaveSpy = jasmine.createSpy()
  344. const beforeEnterSpy = jasmine.createSpy()
  345. const afterLeaveSpy = jasmine.createSpy()
  346. const afterEnterSpy = jasmine.createSpy()
  347. const vm = new Vue({
  348. template: `
  349. <div>
  350. <transition
  351. name="test"
  352. @before-enter="beforeEnter"
  353. @enter="enter"
  354. @after-enter="afterEnter"
  355. @before-leave="beforeLeave"
  356. @leave="leave"
  357. @after-leave="afterLeave">
  358. <div v-show="ok" class="test">foo</div>
  359. </transition>
  360. </div>
  361. `,
  362. data: { ok: true },
  363. methods: {
  364. beforeLeave: el => {
  365. expect(el.style.display).toBe('')
  366. expect(el).toBe(vm.$el.children[0])
  367. expect(el.className).toBe('test')
  368. beforeLeaveSpy(el)
  369. },
  370. leave: el => {
  371. expect(el.style.display).toBe('')
  372. onLeaveSpy(el)
  373. },
  374. afterLeave: el => {
  375. expect(el.style.display).toBe('none')
  376. afterLeaveSpy(el)
  377. },
  378. beforeEnter: el => {
  379. expect(el.className).toBe('test')
  380. expect(el.style.display).toBe('none')
  381. beforeEnterSpy(el)
  382. },
  383. enter: el => {
  384. expect(el.style.display).toBe('')
  385. onEnterSpy(el)
  386. },
  387. afterEnter: el => {
  388. expect(el.style.display).toBe('')
  389. afterEnterSpy(el)
  390. }
  391. }
  392. }).$mount(el)
  393. // should not apply transition on initial render by default
  394. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  395. let _el = vm.$el.children[0]
  396. vm.ok = false
  397. waitForUpdate(() => {
  398. expect(beforeLeaveSpy).toHaveBeenCalledWith(_el)
  399. expect(onLeaveSpy).toHaveBeenCalledWith(_el)
  400. expect(vm.$el.children[0].className).toBe(
  401. 'test test-leave test-leave-active'
  402. )
  403. })
  404. .thenWaitFor(nextFrame)
  405. .then(() => {
  406. expect(afterLeaveSpy).not.toHaveBeenCalled()
  407. expect(vm.$el.children[0].className).toBe(
  408. 'test test-leave-active test-leave-to'
  409. )
  410. })
  411. .thenWaitFor(duration + buffer)
  412. .then(() => {
  413. expect(afterLeaveSpy).toHaveBeenCalledWith(_el)
  414. expect(vm.$el.children[0].style.display).toBe('none')
  415. vm.ok = true
  416. })
  417. .then(() => {
  418. _el = vm.$el.children[0]
  419. expect(beforeEnterSpy).toHaveBeenCalledWith(_el)
  420. expect(onEnterSpy).toHaveBeenCalledWith(_el)
  421. expect(vm.$el.children[0].className).toBe(
  422. 'test test-enter test-enter-active'
  423. )
  424. })
  425. .thenWaitFor(nextFrame)
  426. .then(() => {
  427. expect(afterEnterSpy).not.toHaveBeenCalled()
  428. expect(vm.$el.children[0].className).toBe(
  429. 'test test-enter-active test-enter-to'
  430. )
  431. })
  432. .thenWaitFor(duration + buffer)
  433. .then(() => {
  434. expect(afterEnterSpy).toHaveBeenCalledWith(_el)
  435. expect(vm.$el.children[0].className).toBe('test')
  436. })
  437. .then(done)
  438. })
  439. it('explicit user callback in JavaScript hooks', done => {
  440. let next
  441. const vm = new Vue({
  442. template: `<div>
  443. <transition name="test" @enter="enter" @leave="leave">
  444. <div v-if="ok" class="test">foo</div>
  445. </transition>
  446. </div>`,
  447. data: { ok: true },
  448. methods: {
  449. enter: (el, cb) => {
  450. next = cb
  451. },
  452. leave: (el, cb) => {
  453. next = cb
  454. }
  455. }
  456. }).$mount(el)
  457. vm.ok = false
  458. waitForUpdate(() => {
  459. expect(vm.$el.children[0].className).toBe(
  460. 'test test-leave test-leave-active'
  461. )
  462. })
  463. .thenWaitFor(nextFrame)
  464. .then(() => {
  465. expect(vm.$el.children[0].className).toBe(
  466. 'test test-leave-active test-leave-to'
  467. )
  468. })
  469. .thenWaitFor(duration + buffer)
  470. .then(() => {
  471. expect(vm.$el.children[0].className).toBe(
  472. 'test test-leave-active test-leave-to'
  473. )
  474. expect(next).toBeTruthy()
  475. next()
  476. expect(vm.$el.children.length).toBe(0)
  477. })
  478. .then(() => {
  479. vm.ok = true
  480. })
  481. .then(() => {
  482. expect(vm.$el.children[0].className).toBe(
  483. 'test test-enter test-enter-active'
  484. )
  485. })
  486. .thenWaitFor(nextFrame)
  487. .then(() => {
  488. expect(vm.$el.children[0].className).toBe(
  489. 'test test-enter-active test-enter-to'
  490. )
  491. })
  492. .thenWaitFor(duration + buffer)
  493. .then(() => {
  494. expect(vm.$el.children[0].className).toBe(
  495. 'test test-enter-active test-enter-to'
  496. )
  497. expect(next).toBeTruthy()
  498. next()
  499. expect(vm.$el.children[0].className).toBe('test')
  500. })
  501. .then(done)
  502. })
  503. it('css: false', done => {
  504. const enterSpy = jasmine.createSpy()
  505. const leaveSpy = jasmine.createSpy()
  506. const vm = new Vue({
  507. template: `
  508. <div>
  509. <transition :css="false" name="test" @enter="enter" @leave="leave">
  510. <div v-if="ok" class="test">foo</div>
  511. </transition>
  512. </div>
  513. `,
  514. data: { ok: true },
  515. methods: {
  516. enter: enterSpy,
  517. leave: leaveSpy
  518. }
  519. }).$mount(el)
  520. vm.ok = false
  521. waitForUpdate(() => {
  522. expect(leaveSpy).toHaveBeenCalled()
  523. expect(vm.$el.innerHTML).toBe('<!---->')
  524. vm.ok = true
  525. })
  526. .then(() => {
  527. expect(enterSpy).toHaveBeenCalled()
  528. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  529. })
  530. .then(done)
  531. })
  532. it('no transition detected', done => {
  533. const enterSpy = jasmine.createSpy()
  534. const leaveSpy = jasmine.createSpy()
  535. const vm = new Vue({
  536. template:
  537. '<div><transition name="nope" @enter="enter" @leave="leave"><div v-if="ok">foo</div></transition></div>',
  538. data: { ok: true },
  539. methods: {
  540. enter: enterSpy,
  541. leave: leaveSpy
  542. }
  543. }).$mount(el)
  544. vm.ok = false
  545. waitForUpdate(() => {
  546. expect(leaveSpy).toHaveBeenCalled()
  547. expect(vm.$el.innerHTML).toBe(
  548. '<div class="nope-leave nope-leave-active">foo</div><!---->'
  549. )
  550. })
  551. .thenWaitFor(nextFrame)
  552. .then(() => {
  553. expect(vm.$el.innerHTML).toBe('<!---->')
  554. vm.ok = true
  555. })
  556. .then(() => {
  557. expect(enterSpy).toHaveBeenCalled()
  558. expect(vm.$el.innerHTML).toBe(
  559. '<div class="nope-enter nope-enter-active">foo</div>'
  560. )
  561. })
  562. .thenWaitFor(nextFrame)
  563. .then(() => {
  564. expect(vm.$el.innerHTML).toBe('<div>foo</div>')
  565. })
  566. .then(done)
  567. })
  568. it('enterCancelled', done => {
  569. const spy = jasmine.createSpy()
  570. const vm = new Vue({
  571. template: `
  572. <div>
  573. <transition name="test" @enter-cancelled="enterCancelled">
  574. <div v-if="ok" class="test">foo</div>
  575. </transition>
  576. </div>
  577. `,
  578. data: { ok: false },
  579. methods: {
  580. enterCancelled: spy
  581. }
  582. }).$mount(el)
  583. expect(vm.$el.innerHTML).toBe('<!---->')
  584. vm.ok = true
  585. waitForUpdate(() => {
  586. expect(vm.$el.children[0].className).toBe(
  587. 'test test-enter test-enter-active'
  588. )
  589. })
  590. .thenWaitFor(nextFrame)
  591. .then(() => {
  592. expect(vm.$el.children[0].className).toBe(
  593. 'test test-enter-active test-enter-to'
  594. )
  595. })
  596. .thenWaitFor(duration / 2)
  597. .then(() => {
  598. vm.ok = false
  599. })
  600. .then(() => {
  601. expect(spy).toHaveBeenCalled()
  602. expect(vm.$el.children[0].className).toBe(
  603. 'test test-leave test-leave-active'
  604. )
  605. })
  606. .thenWaitFor(nextFrame)
  607. .then(() => {
  608. expect(vm.$el.children[0].className).toBe(
  609. 'test test-leave-active test-leave-to'
  610. )
  611. })
  612. .thenWaitFor(duration + buffer)
  613. .then(() => {
  614. expect(vm.$el.children.length).toBe(0)
  615. })
  616. .then(done)
  617. })
  618. it('should remove stale leaving elements', done => {
  619. const spy = jasmine.createSpy()
  620. const vm = new Vue({
  621. template: `
  622. <div>
  623. <transition name="test" @after-leave="afterLeave">
  624. <div v-if="ok" class="test">foo</div>
  625. </transition>
  626. </div>
  627. `,
  628. data: { ok: true },
  629. methods: {
  630. afterLeave: spy
  631. }
  632. }).$mount(el)
  633. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  634. vm.ok = false
  635. waitForUpdate(() => {
  636. expect(vm.$el.children[0].className).toBe(
  637. 'test test-leave test-leave-active'
  638. )
  639. })
  640. .thenWaitFor(duration / 2)
  641. .then(() => {
  642. vm.ok = true
  643. })
  644. .then(() => {
  645. expect(spy).toHaveBeenCalled()
  646. expect(vm.$el.children.length).toBe(1) // should have removed leaving element
  647. expect(vm.$el.children[0].className).toBe(
  648. 'test test-enter test-enter-active'
  649. )
  650. })
  651. .thenWaitFor(nextFrame)
  652. .then(() => {
  653. expect(vm.$el.children[0].className).toBe(
  654. 'test test-enter-active test-enter-to'
  655. )
  656. })
  657. .thenWaitFor(duration + buffer)
  658. .then(() => {
  659. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  660. })
  661. .then(done)
  662. })
  663. it('transition with v-show', done => {
  664. const vm = new Vue({
  665. template: `
  666. <div>
  667. <transition name="test">
  668. <div v-show="ok" class="test">foo</div>
  669. </transition>
  670. </div>
  671. `,
  672. data: { ok: true }
  673. }).$mount(el)
  674. // should not apply transition on initial render by default
  675. expect(vm.$el.textContent).toBe('foo')
  676. expect(vm.$el.children[0].style.display).toBe('')
  677. expect(vm.$el.children[0].className).toBe('test')
  678. vm.ok = false
  679. waitForUpdate(() => {
  680. expect(vm.$el.children[0].className).toBe(
  681. 'test test-leave test-leave-active'
  682. )
  683. })
  684. .thenWaitFor(nextFrame)
  685. .then(() => {
  686. expect(vm.$el.children[0].className).toBe(
  687. 'test test-leave-active test-leave-to'
  688. )
  689. })
  690. .thenWaitFor(duration + buffer)
  691. .then(() => {
  692. expect(vm.$el.children[0].style.display).toBe('none')
  693. vm.ok = true
  694. })
  695. .then(() => {
  696. expect(vm.$el.children[0].style.display).toBe('')
  697. expect(vm.$el.children[0].className).toBe(
  698. 'test test-enter test-enter-active'
  699. )
  700. })
  701. .thenWaitFor(nextFrame)
  702. .then(() => {
  703. expect(vm.$el.children[0].className).toBe(
  704. 'test test-enter-active test-enter-to'
  705. )
  706. })
  707. .thenWaitFor(duration + buffer)
  708. .then(() => {
  709. expect(vm.$el.children[0].className).toBe('test')
  710. })
  711. .then(done)
  712. })
  713. it('transition with v-show, inside child component', done => {
  714. const vm = new Vue({
  715. template: `
  716. <div>
  717. <test v-show="ok"></test>
  718. </div>
  719. `,
  720. data: { ok: true },
  721. components: {
  722. test: {
  723. template: `<transition name="test"><div class="test">foo</div></transition>`
  724. }
  725. }
  726. }).$mount(el)
  727. // should not apply transition on initial render by default
  728. expect(vm.$el.textContent).toBe('foo')
  729. expect(vm.$el.children[0].style.display).toBe('')
  730. vm.ok = false
  731. waitForUpdate(() => {
  732. expect(vm.$el.children[0].className).toBe(
  733. 'test test-leave test-leave-active'
  734. )
  735. })
  736. .thenWaitFor(nextFrame)
  737. .then(() => {
  738. expect(vm.$el.children[0].className).toBe(
  739. 'test test-leave-active test-leave-to'
  740. )
  741. })
  742. .thenWaitFor(duration + buffer)
  743. .then(() => {
  744. expect(vm.$el.children[0].style.display).toBe('none')
  745. vm.ok = true
  746. })
  747. .then(() => {
  748. expect(vm.$el.children[0].style.display).toBe('')
  749. expect(vm.$el.children[0].className).toBe(
  750. 'test test-enter test-enter-active'
  751. )
  752. })
  753. .thenWaitFor(nextFrame)
  754. .then(() => {
  755. expect(vm.$el.children[0].className).toBe(
  756. 'test test-enter-active test-enter-to'
  757. )
  758. })
  759. .thenWaitFor(duration + buffer)
  760. .then(() => {
  761. expect(vm.$el.children[0].className).toBe('test')
  762. })
  763. .then(done)
  764. })
  765. it('leaveCancelled (v-show only)', done => {
  766. const spy = jasmine.createSpy()
  767. const vm = new Vue({
  768. template: `
  769. <div>
  770. <transition name="test" @leave-cancelled="leaveCancelled">
  771. <div v-show="ok" class="test">foo</div>
  772. </transition>
  773. </div>
  774. `,
  775. data: { ok: true },
  776. methods: {
  777. leaveCancelled: spy
  778. }
  779. }).$mount(el)
  780. expect(vm.$el.children[0].style.display).toBe('')
  781. vm.ok = false
  782. waitForUpdate(() => {
  783. expect(vm.$el.children[0].className).toBe(
  784. 'test test-leave test-leave-active'
  785. )
  786. })
  787. .thenWaitFor(nextFrame)
  788. .then(() => {
  789. expect(vm.$el.children[0].className).toBe(
  790. 'test test-leave-active test-leave-to'
  791. )
  792. })
  793. .thenWaitFor(10)
  794. .then(() => {
  795. vm.ok = true
  796. })
  797. .then(() => {
  798. expect(spy).toHaveBeenCalled()
  799. expect(vm.$el.children[0].className).toBe(
  800. 'test test-enter test-enter-active'
  801. )
  802. })
  803. .thenWaitFor(nextFrame)
  804. .then(() => {
  805. expect(vm.$el.children[0].className).toBe(
  806. 'test test-enter-active test-enter-to'
  807. )
  808. })
  809. .thenWaitFor(duration + buffer)
  810. .then(() => {
  811. expect(vm.$el.children[0].style.display).toBe('')
  812. })
  813. .then(done)
  814. })
  815. it('leave transition with v-show: cancelled on next frame', done => {
  816. const vm = new Vue({
  817. template: `
  818. <div>
  819. <transition name="test">
  820. <div v-show="ok" class="test">foo</div>
  821. </transition>
  822. </div>
  823. `,
  824. data: { ok: true }
  825. }).$mount(el)
  826. vm.ok = false
  827. waitForUpdate(() => {
  828. vm.ok = true
  829. })
  830. .thenWaitFor(nextFrame)
  831. .then(() => {
  832. expect(vm.$el.children[0].className).toBe(
  833. 'test test-enter-active test-enter-to'
  834. )
  835. })
  836. .thenWaitFor(duration + buffer)
  837. .then(() => {
  838. expect(vm.$el.children[0].className).toBe('test')
  839. })
  840. .then(done)
  841. })
  842. it('enter transition with v-show: cancelled on next frame', done => {
  843. const vm = new Vue({
  844. template: `
  845. <div>
  846. <transition name="test">
  847. <div v-show="ok" class="test">foo</div>
  848. </transition>
  849. </div>
  850. `,
  851. data: { ok: false }
  852. }).$mount(el)
  853. vm.ok = true
  854. waitForUpdate(() => {
  855. vm.ok = false
  856. })
  857. .thenWaitFor(nextFrame)
  858. .then(() => {
  859. expect(vm.$el.children[0].className).toBe(
  860. 'test test-leave-active test-leave-to'
  861. )
  862. })
  863. .thenWaitFor(duration + buffer)
  864. .then(() => {
  865. expect(vm.$el.children[0].className).toBe('test')
  866. })
  867. .then(done)
  868. })
  869. it('animations', done => {
  870. const vm = new Vue({
  871. template: `
  872. <div>
  873. <transition name="test-anim">
  874. <div v-if="ok">foo</div>
  875. </transition>
  876. </div>
  877. `,
  878. data: { ok: true }
  879. }).$mount(el)
  880. // should not apply transition on initial render by default
  881. expect(vm.$el.innerHTML).toBe('<div>foo</div>')
  882. vm.ok = false
  883. waitForUpdate(() => {
  884. expect(vm.$el.children[0].className).toBe(
  885. 'test-anim-leave test-anim-leave-active'
  886. )
  887. })
  888. .thenWaitFor(nextFrame)
  889. .then(() => {
  890. expect(vm.$el.children[0].className).toBe(
  891. 'test-anim-leave-active test-anim-leave-to'
  892. )
  893. })
  894. .thenWaitFor(duration + buffer)
  895. .then(() => {
  896. expect(vm.$el.children.length).toBe(0)
  897. vm.ok = true
  898. })
  899. .then(() => {
  900. expect(vm.$el.children[0].className).toBe(
  901. 'test-anim-enter test-anim-enter-active'
  902. )
  903. })
  904. .thenWaitFor(nextFrame)
  905. .then(() => {
  906. expect(vm.$el.children[0].className).toBe(
  907. 'test-anim-enter-active test-anim-enter-to'
  908. )
  909. })
  910. .thenWaitFor(duration + buffer)
  911. .then(() => {
  912. expect(vm.$el.children[0].className).toBe('')
  913. })
  914. .then(done)
  915. })
  916. it('explicit transition type', done => {
  917. const vm = new Vue({
  918. template: `
  919. <div>
  920. <transition name="test-anim-long" type="animation">
  921. <div v-if="ok" class="test">foo</div>
  922. </transition>
  923. </div>
  924. `,
  925. data: { ok: true }
  926. }).$mount(el)
  927. // should not apply transition on initial render by default
  928. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  929. vm.ok = false
  930. waitForUpdate(() => {
  931. expect(vm.$el.children[0].className).toBe(
  932. 'test test-anim-long-leave test-anim-long-leave-active'
  933. )
  934. })
  935. .thenWaitFor(nextFrame)
  936. .then(() => {
  937. expect(vm.$el.children[0].className).toBe(
  938. 'test test-anim-long-leave-active test-anim-long-leave-to'
  939. )
  940. })
  941. .thenWaitFor(duration + 5)
  942. .then(() => {
  943. // should not end early due to transition presence
  944. expect(vm.$el.children[0].className).toBe(
  945. 'test test-anim-long-leave-active test-anim-long-leave-to'
  946. )
  947. })
  948. .thenWaitFor(duration + 5)
  949. .then(() => {
  950. expect(vm.$el.children.length).toBe(0)
  951. vm.ok = true
  952. })
  953. .then(() => {
  954. expect(vm.$el.children[0].className).toBe(
  955. 'test test-anim-long-enter test-anim-long-enter-active'
  956. )
  957. })
  958. .thenWaitFor(nextFrame)
  959. .then(() => {
  960. expect(vm.$el.children[0].className).toBe(
  961. 'test test-anim-long-enter-active test-anim-long-enter-to'
  962. )
  963. })
  964. .thenWaitFor(duration + 5)
  965. .then(() => {
  966. expect(vm.$el.children[0].className).toBe(
  967. 'test test-anim-long-enter-active test-anim-long-enter-to'
  968. )
  969. })
  970. .thenWaitFor(duration + 5)
  971. .then(() => {
  972. expect(vm.$el.children[0].className).toBe('test')
  973. })
  974. .then(done)
  975. })
  976. it('transition on appear', done => {
  977. const vm = new Vue({
  978. template: `
  979. <div>
  980. <transition name="test"
  981. appear
  982. appear-class="test-appear"
  983. appear-to-class="test-appear-to"
  984. appear-active-class="test-appear-active">
  985. <div v-if="ok" class="test">foo</div>
  986. </transition>
  987. </div>
  988. `,
  989. data: { ok: true }
  990. }).$mount(el)
  991. waitForUpdate(() => {
  992. expect(vm.$el.children[0].className).toBe(
  993. 'test test-appear test-appear-active'
  994. )
  995. })
  996. .thenWaitFor(nextFrame)
  997. .then(() => {
  998. expect(vm.$el.children[0].className).toBe(
  999. 'test test-appear-active test-appear-to'
  1000. )
  1001. })
  1002. .thenWaitFor(duration + buffer)
  1003. .then(() => {
  1004. expect(vm.$el.children[0].className).toBe('test')
  1005. })
  1006. .then(done)
  1007. })
  1008. it('transition on appear with v-show', done => {
  1009. const vm = new Vue({
  1010. template: `
  1011. <div>
  1012. <transition name="test" appear>
  1013. <div v-show="ok" class="test">foo</div>
  1014. </transition>
  1015. </div>
  1016. `,
  1017. data: { ok: true }
  1018. }).$mount(el)
  1019. waitForUpdate(() => {
  1020. expect(vm.$el.children[0].className).toBe(
  1021. 'test test-enter test-enter-active'
  1022. )
  1023. })
  1024. .thenWaitFor(nextFrame)
  1025. .then(() => {
  1026. expect(vm.$el.children[0].className).toBe(
  1027. 'test test-enter-active test-enter-to'
  1028. )
  1029. })
  1030. .thenWaitFor(duration + buffer)
  1031. .then(() => {
  1032. expect(vm.$el.children[0].className).toBe('test')
  1033. })
  1034. .then(done)
  1035. })
  1036. it('transition on SVG elements', done => {
  1037. const vm = new Vue({
  1038. template: `
  1039. <svg>
  1040. <transition>
  1041. <circle cx="0" cy="0" r="10" v-if="ok" class="test"></circle>
  1042. </transition>
  1043. </svg>
  1044. `,
  1045. data: { ok: true }
  1046. }).$mount(el)
  1047. // should not apply transition on initial render by default
  1048. expect(vm.$el.childNodes[0].getAttribute('class')).toBe('test')
  1049. vm.ok = false
  1050. waitForUpdate(() => {
  1051. expect(vm.$el.childNodes[0].getAttribute('class')).toBe(
  1052. 'test v-leave v-leave-active'
  1053. )
  1054. })
  1055. .thenWaitFor(nextFrame)
  1056. .then(() => {
  1057. expect(vm.$el.childNodes[0].getAttribute('class')).toBe(
  1058. 'test v-leave-active v-leave-to'
  1059. )
  1060. })
  1061. .thenWaitFor(duration + buffer)
  1062. .then(() => {
  1063. expect(vm.$el.childNodes.length).toBe(1)
  1064. expect(vm.$el.childNodes[0].nodeType).toBe(8) // should be an empty comment node
  1065. expect(vm.$el.childNodes[0].textContent).toBe('')
  1066. vm.ok = true
  1067. })
  1068. .then(() => {
  1069. expect(vm.$el.childNodes[0].getAttribute('class')).toBe(
  1070. 'test v-enter v-enter-active'
  1071. )
  1072. })
  1073. .thenWaitFor(nextFrame)
  1074. .then(() => {
  1075. expect(vm.$el.childNodes[0].getAttribute('class')).toBe(
  1076. 'test v-enter-active v-enter-to'
  1077. )
  1078. })
  1079. .thenWaitFor(duration + buffer)
  1080. .then(() => {
  1081. expect(vm.$el.childNodes[0].getAttribute('class')).toBe('test')
  1082. })
  1083. .then(done)
  1084. })
  1085. it('transition on child components', done => {
  1086. const vm = new Vue({
  1087. template: `
  1088. <div>
  1089. <transition>
  1090. <test v-if="ok" class="test"></test>
  1091. </transition>
  1092. </div>
  1093. `,
  1094. data: { ok: true },
  1095. components: {
  1096. test: {
  1097. template: `
  1098. <transition name="test">
  1099. <div>foo</div>
  1100. </transition>
  1101. ` // test transition override from parent
  1102. }
  1103. }
  1104. }).$mount(el)
  1105. // should not apply transition on initial render by default
  1106. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  1107. vm.ok = false
  1108. waitForUpdate(() => {
  1109. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1110. })
  1111. .thenWaitFor(nextFrame)
  1112. .then(() => {
  1113. expect(vm.$el.children[0].className).toBe(
  1114. 'test v-leave-active v-leave-to'
  1115. )
  1116. })
  1117. .thenWaitFor(duration + buffer)
  1118. .then(() => {
  1119. expect(vm.$el.children.length).toBe(0)
  1120. vm.ok = true
  1121. })
  1122. .then(() => {
  1123. expect(vm.$el.children[0].className).toBe('test v-enter v-enter-active')
  1124. })
  1125. .thenWaitFor(nextFrame)
  1126. .then(() => {
  1127. expect(vm.$el.children[0].className).toBe(
  1128. 'test v-enter-active v-enter-to'
  1129. )
  1130. })
  1131. .thenWaitFor(duration + buffer)
  1132. .then(() => {
  1133. expect(vm.$el.children[0].className).toBe('test')
  1134. })
  1135. .then(done)
  1136. })
  1137. it('transition inside child component with v-if', done => {
  1138. const vm = new Vue({
  1139. template: `
  1140. <div>
  1141. <test v-if="ok" class="test"></test>
  1142. </div>
  1143. `,
  1144. data: { ok: true },
  1145. components: {
  1146. test: {
  1147. template: `
  1148. <transition>
  1149. <div>foo</div>
  1150. </transition>
  1151. `
  1152. }
  1153. }
  1154. }).$mount(el)
  1155. // should not apply transition on initial render by default
  1156. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  1157. vm.ok = false
  1158. waitForUpdate(() => {
  1159. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1160. })
  1161. .thenWaitFor(nextFrame)
  1162. .then(() => {
  1163. expect(vm.$el.children[0].className).toBe(
  1164. 'test v-leave-active v-leave-to'
  1165. )
  1166. })
  1167. .thenWaitFor(duration + buffer)
  1168. .then(() => {
  1169. expect(vm.$el.children.length).toBe(0)
  1170. vm.ok = true
  1171. })
  1172. .then(() => {
  1173. expect(vm.$el.children[0].className).toBe('test')
  1174. })
  1175. .then(done)
  1176. })
  1177. it('transition with appear inside child component with v-if', done => {
  1178. const vm = new Vue({
  1179. template: `
  1180. <div>
  1181. <test v-if="ok" class="test"></test>
  1182. </div>
  1183. `,
  1184. data: { ok: true },
  1185. components: {
  1186. test: {
  1187. template: `
  1188. <transition appear
  1189. appear-class="test-appear"
  1190. appear-to-class="test-appear-to"
  1191. appear-active-class="test-appear-active">
  1192. <div>foo</div>
  1193. </transition>
  1194. `
  1195. }
  1196. }
  1197. }).$mount(el)
  1198. waitForUpdate(() => {
  1199. expect(vm.$el.children[0].className).toBe(
  1200. 'test test-appear test-appear-active'
  1201. )
  1202. })
  1203. .thenWaitFor(nextFrame)
  1204. .then(() => {
  1205. expect(vm.$el.children[0].className).toBe(
  1206. 'test test-appear-active test-appear-to'
  1207. )
  1208. })
  1209. .thenWaitFor(duration + buffer)
  1210. .then(() => {
  1211. expect(vm.$el.children[0].className).toBe('test')
  1212. vm.ok = false
  1213. })
  1214. .then(() => {
  1215. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1216. })
  1217. .thenWaitFor(nextFrame)
  1218. .then(() => {
  1219. expect(vm.$el.children[0].className).toBe(
  1220. 'test v-leave-active v-leave-to'
  1221. )
  1222. })
  1223. .thenWaitFor(duration + buffer)
  1224. .then(() => {
  1225. expect(vm.$el.children.length).toBe(0)
  1226. })
  1227. .then(done)
  1228. })
  1229. it('transition inside nested child component with v-if', done => {
  1230. const vm = new Vue({
  1231. template: `
  1232. <div>
  1233. <foo v-if="ok" class="test"></foo>
  1234. </div>
  1235. `,
  1236. data: { ok: true },
  1237. components: {
  1238. foo: {
  1239. template: '<bar></bar>',
  1240. components: {
  1241. bar: {
  1242. template: '<transition><div>foo</div></transition>'
  1243. }
  1244. }
  1245. }
  1246. }
  1247. }).$mount(el)
  1248. // should not apply transition on initial render by default
  1249. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  1250. vm.ok = false
  1251. waitForUpdate(() => {
  1252. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1253. })
  1254. .thenWaitFor(nextFrame)
  1255. .then(() => {
  1256. expect(vm.$el.children[0].className).toBe(
  1257. 'test v-leave-active v-leave-to'
  1258. )
  1259. })
  1260. .thenWaitFor(duration + buffer)
  1261. .then(() => {
  1262. expect(vm.$el.children.length).toBe(0)
  1263. vm.ok = true
  1264. })
  1265. .then(() => {
  1266. expect(vm.$el.children[0].className).toBe('test')
  1267. })
  1268. .then(done)
  1269. })
  1270. it('transition with appear inside nested child component with v-if', done => {
  1271. const vm = new Vue({
  1272. template: `
  1273. <div>
  1274. <foo v-if="ok" class="test"></foo>
  1275. </div>
  1276. `,
  1277. data: { ok: true },
  1278. components: {
  1279. foo: {
  1280. template: '<bar></bar>',
  1281. components: {
  1282. bar: {
  1283. template: `
  1284. <transition appear
  1285. appear-class="test-appear"
  1286. appear-to-class="test-appear-to"
  1287. appear-active-class="test-appear-active">
  1288. <div>foo</div>
  1289. </transition>
  1290. `
  1291. }
  1292. }
  1293. }
  1294. }
  1295. }).$mount(el)
  1296. waitForUpdate(() => {
  1297. expect(vm.$el.children[0].className).toBe(
  1298. 'test test-appear test-appear-active'
  1299. )
  1300. })
  1301. .thenWaitFor(nextFrame)
  1302. .then(() => {
  1303. expect(vm.$el.children[0].className).toBe(
  1304. 'test test-appear-active test-appear-to'
  1305. )
  1306. })
  1307. .thenWaitFor(duration + buffer)
  1308. .then(() => {
  1309. expect(vm.$el.children[0].className).toBe('test')
  1310. vm.ok = false
  1311. })
  1312. .then(() => {
  1313. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1314. })
  1315. .thenWaitFor(nextFrame)
  1316. .then(() => {
  1317. expect(vm.$el.children[0].className).toBe(
  1318. 'test v-leave-active v-leave-to'
  1319. )
  1320. })
  1321. .thenWaitFor(duration + buffer)
  1322. .then(() => {
  1323. expect(vm.$el.children.length).toBe(0)
  1324. })
  1325. .then(done)
  1326. })
  1327. it('custom transition higher-order component', done => {
  1328. const vm = new Vue({
  1329. template:
  1330. '<div><my-transition><div v-if="ok" class="test">foo</div></my-transition></div>',
  1331. data: { ok: true },
  1332. components: {
  1333. 'my-transition': {
  1334. functional: true,
  1335. render(h, { data, children }) {
  1336. ;(data.props || (data.props = {})).name = 'test'
  1337. return h('transition', data, children)
  1338. }
  1339. }
  1340. }
  1341. }).$mount(el)
  1342. // should not apply transition on initial render by default
  1343. expect(vm.$el.innerHTML).toBe('<div class="test">foo</div>')
  1344. vm.ok = false
  1345. waitForUpdate(() => {
  1346. expect(vm.$el.children[0].className).toBe(
  1347. 'test test-leave test-leave-active'
  1348. )
  1349. })
  1350. .thenWaitFor(nextFrame)
  1351. .then(() => {
  1352. expect(vm.$el.children[0].className).toBe(
  1353. 'test test-leave-active test-leave-to'
  1354. )
  1355. })
  1356. .thenWaitFor(duration + buffer)
  1357. .then(() => {
  1358. expect(vm.$el.children.length).toBe(0)
  1359. vm.ok = true
  1360. })
  1361. .then(() => {
  1362. expect(vm.$el.children[0].className).toBe(
  1363. 'test test-enter test-enter-active'
  1364. )
  1365. })
  1366. .thenWaitFor(nextFrame)
  1367. .then(() => {
  1368. expect(vm.$el.children[0].className).toBe(
  1369. 'test test-enter-active test-enter-to'
  1370. )
  1371. })
  1372. .thenWaitFor(duration + buffer)
  1373. .then(() => {
  1374. expect(vm.$el.children[0].className).toBe('test')
  1375. })
  1376. .then(done)
  1377. })
  1378. it('warn when used on multiple elements', () => {
  1379. new Vue({
  1380. template: `<transition><p>1</p><p>2</p></transition>`
  1381. }).$mount()
  1382. expect(
  1383. `<transition> can only be used on a single element`
  1384. ).toHaveBeenWarned()
  1385. })
  1386. describe('explicit durations -', () => {
  1387. it('single value', done => {
  1388. const vm = new Vue({
  1389. template: `
  1390. <div>
  1391. <transition duration="${explicitDuration}">
  1392. <div v-if="ok" class="test">foo</div>
  1393. </transition>
  1394. </div>
  1395. `,
  1396. data: { ok: true }
  1397. }).$mount(el)
  1398. vm.ok = false
  1399. waitForUpdate(() => {
  1400. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1401. })
  1402. .thenWaitFor(nextFrame)
  1403. .then(() => {
  1404. expect(vm.$el.children[0].className).toBe(
  1405. 'test v-leave-active v-leave-to'
  1406. )
  1407. })
  1408. .thenWaitFor(explicitDuration + buffer)
  1409. .then(() => {
  1410. expect(vm.$el.children.length).toBe(0)
  1411. vm.ok = true
  1412. })
  1413. .then(() => {
  1414. expect(vm.$el.children[0].className).toBe(
  1415. 'test v-enter v-enter-active'
  1416. )
  1417. })
  1418. .thenWaitFor(nextFrame)
  1419. .then(() => {
  1420. expect(vm.$el.children[0].className).toBe(
  1421. 'test v-enter-active v-enter-to'
  1422. )
  1423. })
  1424. .thenWaitFor(explicitDuration + buffer)
  1425. .then(() => {
  1426. expect(vm.$el.children[0].className).toBe('test')
  1427. })
  1428. .then(done)
  1429. })
  1430. it('enter and auto leave', done => {
  1431. const vm = new Vue({
  1432. template: `
  1433. <div>
  1434. <transition :duration="{ enter: ${explicitDuration} }">
  1435. <div v-if="ok" class="test">foo</div>
  1436. </transition>
  1437. </div>
  1438. `,
  1439. data: { ok: true }
  1440. }).$mount(el)
  1441. vm.ok = false
  1442. waitForUpdate(() => {
  1443. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1444. })
  1445. .thenWaitFor(nextFrame)
  1446. .then(() => {
  1447. expect(vm.$el.children[0].className).toBe(
  1448. 'test v-leave-active v-leave-to'
  1449. )
  1450. })
  1451. .thenWaitFor(duration + buffer)
  1452. .then(() => {
  1453. expect(vm.$el.children.length).toBe(0)
  1454. vm.ok = true
  1455. })
  1456. .then(() => {
  1457. expect(vm.$el.children[0].className).toBe(
  1458. 'test v-enter v-enter-active'
  1459. )
  1460. })
  1461. .thenWaitFor(nextFrame)
  1462. .then(() => {
  1463. expect(vm.$el.children[0].className).toBe(
  1464. 'test v-enter-active v-enter-to'
  1465. )
  1466. })
  1467. .thenWaitFor(explicitDuration + buffer)
  1468. .then(() => {
  1469. expect(vm.$el.children[0].className).toBe('test')
  1470. })
  1471. .then(done)
  1472. })
  1473. it('leave and auto enter', done => {
  1474. const vm = new Vue({
  1475. template: `
  1476. <div>
  1477. <transition :duration="{ leave: ${explicitDuration} }">
  1478. <div v-if="ok" class="test">foo</div>
  1479. </transition>
  1480. </div>
  1481. `,
  1482. data: { ok: true }
  1483. }).$mount(el)
  1484. vm.ok = false
  1485. waitForUpdate(() => {
  1486. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1487. })
  1488. .thenWaitFor(nextFrame)
  1489. .then(() => {
  1490. expect(vm.$el.children[0].className).toBe(
  1491. 'test v-leave-active v-leave-to'
  1492. )
  1493. })
  1494. .thenWaitFor(explicitDuration + buffer)
  1495. .then(() => {
  1496. expect(vm.$el.children.length).toBe(0)
  1497. vm.ok = true
  1498. })
  1499. .then(() => {
  1500. expect(vm.$el.children[0].className).toBe(
  1501. 'test v-enter v-enter-active'
  1502. )
  1503. })
  1504. .thenWaitFor(nextFrame)
  1505. .then(() => {
  1506. expect(vm.$el.children[0].className).toBe(
  1507. 'test v-enter-active v-enter-to'
  1508. )
  1509. })
  1510. .thenWaitFor(duration + buffer)
  1511. .then(() => {
  1512. expect(vm.$el.children[0].className).toBe('test')
  1513. })
  1514. .then(done)
  1515. })
  1516. it('separate enter and leave', done => {
  1517. const enter = explicitDuration
  1518. const leave = explicitDuration * 2
  1519. const vm = new Vue({
  1520. template: `
  1521. <div>
  1522. <transition :duration="{ enter: ${enter}, leave: ${leave} }">
  1523. <div v-if="ok" class="test">foo</div>
  1524. </transition>
  1525. </div>
  1526. `,
  1527. data: { ok: true }
  1528. }).$mount(el)
  1529. vm.ok = false
  1530. waitForUpdate(() => {
  1531. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1532. })
  1533. .thenWaitFor(nextFrame)
  1534. .then(() => {
  1535. expect(vm.$el.children[0].className).toBe(
  1536. 'test v-leave-active v-leave-to'
  1537. )
  1538. })
  1539. .thenWaitFor(leave + buffer)
  1540. .then(() => {
  1541. expect(vm.$el.children.length).toBe(0)
  1542. vm.ok = true
  1543. })
  1544. .then(() => {
  1545. expect(vm.$el.children[0].className).toBe(
  1546. 'test v-enter v-enter-active'
  1547. )
  1548. })
  1549. .thenWaitFor(nextFrame)
  1550. .then(() => {
  1551. expect(vm.$el.children[0].className).toBe(
  1552. 'test v-enter-active v-enter-to'
  1553. )
  1554. })
  1555. .thenWaitFor(enter + buffer)
  1556. .then(() => {
  1557. expect(vm.$el.children[0].className).toBe('test')
  1558. })
  1559. .then(done)
  1560. })
  1561. it('enter and leave + duration change', done => {
  1562. const enter1 = explicitDuration * 2
  1563. const enter2 = explicitDuration
  1564. const leave1 = explicitDuration * 0.5
  1565. const leave2 = explicitDuration * 3
  1566. const vm = new Vue({
  1567. template: `
  1568. <div>
  1569. <transition :duration="{ enter: enter, leave: leave }">
  1570. <div v-if="ok" class="test">foo</div>
  1571. </transition>
  1572. </div>
  1573. `,
  1574. data: {
  1575. ok: true,
  1576. enter: enter1,
  1577. leave: leave1
  1578. }
  1579. }).$mount(el)
  1580. vm.ok = false
  1581. waitForUpdate(() => {
  1582. expect(vm.$el.children[0].className).toBe('test v-leave v-leave-active')
  1583. })
  1584. .thenWaitFor(nextFrame)
  1585. .then(() => {
  1586. expect(vm.$el.children[0].className).toBe(
  1587. 'test v-leave-active v-leave-to'
  1588. )
  1589. })
  1590. .thenWaitFor(leave1 + buffer)
  1591. .then(() => {
  1592. expect(vm.$el.children.length).toBe(0)
  1593. vm.ok = true
  1594. })
  1595. .then(() => {
  1596. expect(vm.$el.children[0].className).toBe(
  1597. 'test v-enter v-enter-active'
  1598. )
  1599. })
  1600. .thenWaitFor(nextFrame)
  1601. .then(() => {
  1602. expect(vm.$el.children[0].className).toBe(
  1603. 'test v-enter-active v-enter-to'
  1604. )
  1605. })
  1606. .thenWaitFor(enter1 + buffer)
  1607. .then(() => {
  1608. expect(vm.$el.children[0].className).toBe('test')
  1609. vm.enter = enter2
  1610. vm.leave = leave2
  1611. })
  1612. .then(() => {
  1613. vm.ok = false
  1614. })
  1615. .then(() => {
  1616. expect(vm.$el.children[0].className).toBe(
  1617. 'test v-leave v-leave-active'
  1618. )
  1619. })
  1620. .thenWaitFor(nextFrame)
  1621. .then(() => {
  1622. expect(vm.$el.children[0].className).toBe(
  1623. 'test v-leave-active v-leave-to'
  1624. )
  1625. })
  1626. .thenWaitFor(leave2 + buffer)
  1627. .then(() => {
  1628. expect(vm.$el.children.length).toBe(0)
  1629. vm.ok = true
  1630. })
  1631. .then(() => {
  1632. expect(vm.$el.children[0].className).toBe(
  1633. 'test v-enter v-enter-active'
  1634. )
  1635. })
  1636. .thenWaitFor(nextFrame)
  1637. .then(() => {
  1638. expect(vm.$el.children[0].className).toBe(
  1639. 'test v-enter-active v-enter-to'
  1640. )
  1641. })
  1642. .thenWaitFor(enter2 + buffer)
  1643. .then(() => {
  1644. expect(vm.$el.children[0].className).toBe('test')
  1645. })
  1646. .then(done)
  1647. }, 10000)
  1648. it('warn invalid durations', done => {
  1649. const vm = new Vue({
  1650. template: `
  1651. <div>
  1652. <transition :duration="{ enter: NaN, leave: 'foo' }">
  1653. <div v-if="ok" class="test">foo</div>
  1654. </transition>
  1655. </div>
  1656. `,
  1657. data: {
  1658. ok: true
  1659. }
  1660. }).$mount(el)
  1661. vm.ok = false
  1662. waitForUpdate(() => {
  1663. expect(
  1664. `<transition> explicit leave duration is not a valid number - got "foo"`
  1665. ).toHaveBeenWarned()
  1666. })
  1667. .thenWaitFor(duration + buffer)
  1668. .then(() => {
  1669. vm.ok = true
  1670. })
  1671. .then(() => {
  1672. expect(
  1673. `<transition> explicit enter duration is NaN`
  1674. ).toHaveBeenWarned()
  1675. })
  1676. .then(done)
  1677. })
  1678. })
  1679. // #6687
  1680. it('transition on child components with empty root node', done => {
  1681. const vm = new Vue({
  1682. template: `
  1683. <div>
  1684. <transition mode="out-in">
  1685. <component class="test" :is="view"></component>
  1686. </transition>
  1687. </div>
  1688. `,
  1689. data: { view: 'one' },
  1690. components: {
  1691. one: {
  1692. template: '<div v-if="false">one</div>'
  1693. },
  1694. two: {
  1695. template: '<div>two</div>'
  1696. }
  1697. }
  1698. }).$mount(el)
  1699. // should not apply transition on initial render by default
  1700. expect(vm.$el.innerHTML).toBe('<!---->')
  1701. vm.view = 'two'
  1702. waitForUpdate(() => {
  1703. expect(vm.$el.innerHTML).toBe(
  1704. '<div class="test v-enter v-enter-active">two</div>'
  1705. )
  1706. })
  1707. .thenWaitFor(nextFrame)
  1708. .then(() => {
  1709. expect(vm.$el.children[0].className).toBe(
  1710. 'test v-enter-active v-enter-to'
  1711. )
  1712. })
  1713. .thenWaitFor(duration + buffer)
  1714. .then(() => {
  1715. expect(vm.$el.children[0].className).toBe('test')
  1716. vm.view = 'one'
  1717. })
  1718. .then(() => {
  1719. // incoming comment node is appended instantly because it doesn't have
  1720. // data and therefore doesn't go through the transition module.
  1721. expect(vm.$el.innerHTML).toBe(
  1722. '<div class="test v-leave v-leave-active">two</div><!---->'
  1723. )
  1724. })
  1725. .thenWaitFor(nextFrame)
  1726. .then(() => {
  1727. expect(vm.$el.children[0].className).toBe(
  1728. 'test v-leave-active v-leave-to'
  1729. )
  1730. })
  1731. .thenWaitFor(duration + buffer)
  1732. .then(() => {
  1733. expect(vm.$el.innerHTML).toBe('<!---->')
  1734. })
  1735. .then(done)
  1736. })
  1737. // #8199
  1738. it('should not throw error when replaced by v-html contents', done => {
  1739. const vm = new Vue({
  1740. template: `
  1741. <div>
  1742. <div v-if="ok" :class="ok">
  1743. <transition>
  1744. <span>a</span>
  1745. </transition>
  1746. </div>
  1747. <div v-else v-html="ok"></div>
  1748. </div>
  1749. `,
  1750. data: { ok: true }
  1751. }).$mount(el)
  1752. vm.ok = false
  1753. waitForUpdate(() => {
  1754. expect(vm.$el.children[0].innerHTML).toBe('false')
  1755. }).then(done)
  1756. })
  1757. })