Skip to content

fix(KeepAlive): when exclude prop change, it should prune cache that not matched #2111

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

Merged
merged 1 commit into from
Sep 15, 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
57 changes: 55 additions & 2 deletions packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,25 @@ describe('KeepAlive', () => {
return { viewRef, includeRef }
}

test('on include/exclude change', async () => {
function setupExclude() {
const viewRef = ref('one')
const excludeRef = ref('')
const App = {
render() {
return h(
KeepAlive,
{
exclude: excludeRef.value
},
() => h(views[viewRef.value])
)
}
}
render(h(App), root)
return { viewRef, excludeRef }
}

test('on include change', async () => {
const { viewRef, includeRef } = setup()

viewRef.value = 'two'
Expand All @@ -415,7 +433,26 @@ describe('KeepAlive', () => {
assertHookCalls(two, [1, 1, 1, 1, 0])
})

test('on include/exclude change + view switch', async () => {
test('on exclude change', async () => {
const { viewRef, excludeRef } = setupExclude()

viewRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])

excludeRef.value = 'one'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 1])
assertHookCalls(two, [1, 1, 1, 0, 0])

viewRef.value = 'one'
await nextTick()
assertHookCalls(one, [2, 2, 1, 1, 1])
assertHookCalls(two, [1, 1, 1, 1, 0])
})

test('on include change + view switch', async () => {
const { viewRef, includeRef } = setup()

viewRef.value = 'two'
Expand All @@ -431,6 +468,22 @@ describe('KeepAlive', () => {
assertHookCalls(two, [1, 1, 1, 1, 1])
})

test('on exclude change + view switch', async () => {
const { viewRef, excludeRef } = setupExclude()

viewRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])

excludeRef.value = 'two'
viewRef.value = 'one'
await nextTick()
assertHookCalls(one, [1, 1, 2, 1, 0])
// two should be pruned
assertHookCalls(two, [1, 1, 1, 1, 1])
})

test('should not prune current active instance', async () => {
const { viewRef, includeRef } = setup()

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const KeepAliveImpl = {
() => [props.include, props.exclude],
([include, exclude]) => {
include && pruneCache(name => matches(include, name))
exclude && pruneCache(name => matches(exclude, name))
exclude && pruneCache(name => !matches(exclude, name))
}
)

Expand Down