Skip to content

Commit

Permalink
fix(transition): consider async placeholder as valid child to return, f…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkzing committed Aug 15, 2017
1 parent 3cac5c7 commit e448fd4
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/platforms/web/runtime/components/transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// supports transition mode (out-in / in-out)

import { warn } from 'core/util/index'
import { camelize, extend, isPrimitive } from 'shared/util'
import { mergeVNodeHook, getFirstComponentChild } from 'core/vdom/helpers/index'
import { camelize, extend, isPrimitive, isDef } from 'shared/util'
import { mergeVNodeHook } from 'core/vdom/helpers/index'

export const transitionProps = {
name: String,
Expand All @@ -25,12 +25,25 @@ export const transitionProps = {
duration: [Number, String, Object]
}

// similar with getFirstComponentChild
// but consider async placeholder as valid child
function getFirstValidChild (children: ?Array<VNode>): ?VNode {
if (Array.isArray(children)) {
for (let i = 0; i < children.length; i++) {
const c = children[i]
if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {
return c
}
}
}
}

// in case the child is also an abstract component, e.g. <keep-alive>
// we want to recursively retrieve the real component to be rendered
function getRealChild (vnode: ?VNode): ?VNode {
const compOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
if (compOptions && compOptions.Ctor.options.abstract) {
return getRealChild(getFirstComponentChild(compOptions.children))
return getRealChild(getFirstValidChild(compOptions.children))
} else {
return vnode
}
Expand Down
84 changes: 84 additions & 0 deletions test/unit/features/component/component-keep-alive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,5 +862,89 @@ describe('Component keep-alive', () => {
)
}).then(done)
})

it('async components with transition-mode out-in', done => {
const barResolve = jasmine.createSpy('bar resolved')
let next
const foo = (resolve) => {
setTimeout(() => {
resolve(one)
Vue.nextTick(next)
}, duration / 2)
}
const bar = (resolve) => {
setTimeout(() => {
resolve(two)
barResolve()
}, duration / 2)
}
components = {
foo,
bar
}
const vm = new Vue({
template: `<div>
<transition name="test" mode="out-in" @after-enter="afterEnter" @after-leave="afterLeave">
<keep-alive>
<component :is="view" class="test"></component>
</keep-alive>
</transition>
</div>`,
data: {
view: 'foo'
},
components,
methods: {
afterEnter () {
next()
},
afterLeave () {
next()
}
}
}).$mount(el)
expect(vm.$el.textContent).toBe('')
next = () => {
assertHookCalls(one, [1, 1, 1, 0, 0])
assertHookCalls(two, [0, 0, 0, 0, 0])
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe(
'<div class="test test-enter test-enter-active">one</div>'
)
}).thenWaitFor(nextFrame).then(() => {
expect(vm.$el.innerHTML).toBe(
'<div class="test test-enter-active test-enter-to">one</div>'
)
}).thenWaitFor(_next => { next = _next }).then(() => {
// foo afterEnter get called
expect(vm.$el.innerHTML).toBe('<div class="test">one</div>')
vm.view = 'bar'
}).thenWaitFor(nextFrame).then(() => {
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [0, 0, 0, 0, 0])
expect(vm.$el.innerHTML).toBe(
'<div class="test test-leave-active test-leave-to">one</div><!---->'
)
}).thenWaitFor(_next => { next = _next }).then(() => {
// foo afterLeave get called
// and bar has already been resolved before afterLeave get called
expect(barResolve.calls.count()).toBe(1)
expect(vm.$el.innerHTML).toBe('<!---->')
}).thenWaitFor(nextFrame).then(() => {
expect(vm.$el.innerHTML).toBe(
'<div class="test test-enter test-enter-active">two</div>'
)
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
}).thenWaitFor(nextFrame).then(() => {
expect(vm.$el.innerHTML).toBe(
'<div class="test test-enter-active test-enter-to">two</div>'
)
}).thenWaitFor(_next => { next = _next }).then(() => {
// bar afterEnter get called
expect(vm.$el.innerHTML).toBe('<div class="test">two</div>')
}).then(done)
}
})
}
})

0 comments on commit e448fd4

Please sign in to comment.