fix(reactivity): effect shoud only recursively self trigger with explicit options

fix #2125
This commit is contained in:
Evan You
2020-09-16 10:52:31 -04:00
parent 89e9ab8a2a
commit 3810de7d6b
4 changed files with 57 additions and 6 deletions

View File

@@ -779,4 +779,17 @@ describe('api: watch', () => {
// should trigger now
expect(sideEffect).toBe(2)
})
// #2125
test('watchEffect should not recursively trigger itself', async () => {
const spy = jest.fn()
const price = ref(10)
const history = ref<number[]>([])
watchEffect(() => {
history.value.push(price.value)
spy()
})
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})
})

View File

@@ -5,7 +5,10 @@ import {
nodeOps,
serializeInner,
nextTick,
VNode
VNode,
provide,
inject,
Ref
} from '@vue/runtime-test'
describe('renderer: component', () => {
@@ -104,4 +107,34 @@ describe('renderer: component', () => {
)
expect(Comp1.updated).not.toHaveBeenCalled()
})
// #2043
test('component child synchronously updating parent state should trigger parent re-render', async () => {
const App = {
setup() {
const n = ref(0)
provide('foo', n)
return () => {
return [h('div', n.value), h(Child)]
}
}
}
const Child = {
setup() {
const n = inject<Ref<number>>('foo')!
n.value++
return () => {
return h('div', n.value)
}
}
}
const root = nodeOps.createElement('div')
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>0</div><div>1</div>`)
await nextTick()
expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
})
})