Skip to content

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 16, 2024
1 parent aad376c commit 3d993ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
1 change: 0 additions & 1 deletion packages/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ declare var __ESM_BUNDLER__: boolean
declare var __ESM_BROWSER__: boolean
declare var __CJS__: boolean
declare var __SSR__: boolean
declare var __VUE_SSR_SETTERS__: Array<(v: boolean) => void>
declare var __COMMIT__: string
declare var __VERSION__: string
declare var __COMPAT__: boolean
Expand Down
52 changes: 20 additions & 32 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
toRef,
triggerRef,
} from '@vue/reactivity'
import { renderToString } from '@vue/server-renderer'

describe('api: watch', () => {
it('effect', async () => {
Expand Down Expand Up @@ -374,15 +375,6 @@ describe('api: watch', () => {
})

it('stopping the watcher (SSR)', async () => {
type SetBoolean = (v: boolean) => void
const setSSR = (ssr: boolean) => {
__SSR__ = ssr
__VUE_SSR_SETTERS__.forEach((setInSSRSetupState: SetBoolean) => {
setInSSRSetupState(ssr)
})
}
setSSR(true)

let dummy = 0
const count = ref<number>(1)
const captureValue = (value: number) => {
Expand All @@ -391,36 +383,32 @@ describe('api: watch', () => {
const watchCallback = vi.fn(newValue => {
captureValue(newValue)
})
const scenario = () => {
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 }
},
})
const root = nodeOps.createElement('div')
render(h(Comp), root)
}

expect(scenario).not.toThrowError(/stop is not a function/)
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)

setSSR(false)
})

it('stopping the watcher (with source)', async () => {
Expand Down

0 comments on commit 3d993ec

Please sign in to comment.