mirror of https://github.com/vuejs/core.git
fix(watch): unwatch should be callable during SSR (#11925)
close #11924
This commit is contained in:
parent
bc3ddca9d0
commit
2d6adf78a0
|
@ -37,6 +37,7 @@ import {
|
||||||
toRef,
|
toRef,
|
||||||
triggerRef,
|
triggerRef,
|
||||||
} from '@vue/reactivity'
|
} from '@vue/reactivity'
|
||||||
|
import { renderToString } from '@vue/server-renderer'
|
||||||
|
|
||||||
describe('api: watch', () => {
|
describe('api: watch', () => {
|
||||||
it('effect', async () => {
|
it('effect', async () => {
|
||||||
|
@ -373,6 +374,43 @@ describe('api: watch', () => {
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('stopping the watcher (SSR)', async () => {
|
||||||
|
let dummy = 0
|
||||||
|
const count = ref<number>(1)
|
||||||
|
const captureValue = (value: number) => {
|
||||||
|
dummy = value
|
||||||
|
}
|
||||||
|
const watchCallback = vi.fn(newValue => {
|
||||||
|
captureValue(newValue)
|
||||||
|
})
|
||||||
|
const Comp = defineComponent({
|
||||||
|
created() {
|
||||||
|
const getter = () => this.count
|
||||||
|
captureValue(getter()) // sets dummy to 1
|
||||||
|
const stop = this.$watch(getter, watchCallback)
|
||||||
|
stop()
|
||||||
|
this.count = 2 // shouldn't trigger side effect
|
||||||
|
},
|
||||||
|
render() {
|
||||||
|
return h('div', this.count)
|
||||||
|
},
|
||||||
|
setup() {
|
||||||
|
return { count }
|
||||||
|
},
|
||||||
|
})
|
||||||
|
let html
|
||||||
|
html = await renderToString(h(Comp))
|
||||||
|
// should not throw here
|
||||||
|
expect(html).toBe(`<div>2</div>`)
|
||||||
|
expect(watchCallback).not.toHaveBeenCalled()
|
||||||
|
expect(dummy).toBe(1)
|
||||||
|
await nextTick()
|
||||||
|
count.value = 3 // shouldn't trigger side effect
|
||||||
|
await nextTick()
|
||||||
|
expect(watchCallback).not.toHaveBeenCalled()
|
||||||
|
expect(dummy).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
it('stopping the watcher (with source)', async () => {
|
it('stopping the watcher (with source)', async () => {
|
||||||
const state = reactive({ count: 0 })
|
const state = reactive({ count: 0 })
|
||||||
let dummy
|
let dummy
|
||||||
|
|
|
@ -179,11 +179,11 @@ function doWatch(
|
||||||
// immediately watch or watchEffect
|
// immediately watch or watchEffect
|
||||||
baseWatchOptions.once = true
|
baseWatchOptions.once = true
|
||||||
} else {
|
} else {
|
||||||
return {
|
const watchStopHandle = () => {}
|
||||||
stop: NOOP,
|
watchStopHandle.stop = NOOP
|
||||||
resume: NOOP,
|
watchStopHandle.resume = NOOP
|
||||||
pause: NOOP,
|
watchStopHandle.pause = NOOP
|
||||||
} as WatchHandle
|
return watchStopHandle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue