Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(keep-alive): fix #1621 and add test. #1622

Merged
merged 3 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ const KeepAliveImpl = {
// cache sub tree in beforeMount/Update (i.e. right after the render)
let pendingCacheKey: CacheKey | null = null
const cacheSubtree = () => {
if (pendingCacheKey) {
// fix #1621, the pendingCacheKey could be 0
if (pendingCacheKey != null) {
cache.set(pendingCacheKey, instance.subTree)
}
}
Expand Down
58 changes: 57 additions & 1 deletion packages/vue/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApp } from '../src'
import { createApp, ref, nextTick } from '../src'
import { mockWarn } from '@vue/shared'

describe('compiler + runtime integration', () => {
Expand All @@ -18,6 +18,62 @@ describe('compiler + runtime integration', () => {
expect(container.innerHTML).toBe(`0`)
})

it('keep-alive with compiler + runtime integration', async () => {
const container = document.createElement('div')
const one = {
name: 'one',
template: 'one',
created: jest.fn(),
mounted: jest.fn(),
activated: jest.fn(),
deactivated: jest.fn(),
destroyed: jest.fn()
}

const toggle = ref(true)

const App = {
template: `
<keep-alive>
<one v-if="toggle"></one>
</keep-alive>
`,
data() {
return {
toggle
}
},
components: {
One: one
}
}
createApp(App).mount(container)
expect(container.innerHTML).toBe(`one`)
expect(one.created).toHaveBeenCalledTimes(1)
expect(one.mounted).toHaveBeenCalledTimes(1)
expect(one.activated).toHaveBeenCalledTimes(1)
expect(one.deactivated).toHaveBeenCalledTimes(0)
expect(one.destroyed).toHaveBeenCalledTimes(0)

toggle.value = false;
await nextTick()
expect(container.innerHTML).toBe(`<!--v-if-->`)
expect(one.created).toHaveBeenCalledTimes(1)
expect(one.mounted).toHaveBeenCalledTimes(1)
expect(one.activated).toHaveBeenCalledTimes(1)
expect(one.deactivated).toHaveBeenCalledTimes(1)
expect(one.destroyed).toHaveBeenCalledTimes(0)

toggle.value = true;
await nextTick()
expect(container.innerHTML).toBe(`one`)
expect(one.created).toHaveBeenCalledTimes(1)
expect(one.mounted).toHaveBeenCalledTimes(1)
expect(one.activated).toHaveBeenCalledTimes(2)
expect(one.deactivated).toHaveBeenCalledTimes(1)
expect(one.destroyed).toHaveBeenCalledTimes(0)
})

it('should support runtime template via CSS ID selector', () => {
const container = document.createElement('div')
const template = document.createElement('div')
Expand Down