提交 fa0f47af 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat(composition api): effectScope

上级 70460407
const PAGE_PATH = '/pages/composition-api/reactivity/effect-scope/effect-scope'
describe('effectScope', () => {
if (process.env.uniTestPlatformInfo.startsWith('android')) {
let page = null
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor('view')
})
it('basic', async () => {
const counter = await page.$('#counter')
expect(await counter.text()).toBe('counter: 0')
const watchCounterRes = await page.$('#watch-counter-res')
expect(await watchCounterRes.text()).toBe('watch counter result: ')
const watchEffectCounterRes = await page.$('#watch-effect-counter-res')
expect(await watchEffectCounterRes.text()).toBe('watchEffect counter result: counter: 0')
const incrementCounterBtn = await page.$('#increment-counter-btn')
await incrementCounterBtn.tap()
expect(await counter.text()).toBe('counter: 1')
expect(await watchCounterRes.text()).toBe('watch counter result: newVal: 1, oldVal: 0')
expect(await watchEffectCounterRes.text()).toBe('watchEffect counter result: counter: 1')
const stopEffectScopeBtn = await page.$('#stop-effect-scope-btn')
await stopEffectScopeBtn.tap()
await incrementCounterBtn.tap()
expect(await counter.text()).toBe('counter: 2')
expect(await watchCounterRes.text()).toBe('watch counter result: newVal: 1, oldVal: 0')
expect(await watchEffectCounterRes.text()).toBe('watchEffect counter result: counter: 1')
})
} else {
it('other platform', () => {
expect(1).toBe(1)
})
}
})
\ No newline at end of file
<template><view class="page">getCurrentScope</view></template>
\ No newline at end of file
<template>
<view class="page">
<text id="counter">counter: {{ counter }}</text>
<text id="watch-counter-res" class="uni-common-mt">watch counter result: {{ watchCounterRes }}</text>
<text id="watch-effect-counter-res" class="uni-common-mt">watchEffect counter result:
{{ watchEffectCounterRes }}</text>
<button id="increment-counter-btn" class="uni-common-mt" @click="() => {counter++}">increment counter</button>
<button id="stop-effect-scope-btn" class="uni-common-mt" @click="stopEffectScope">stop effect scope</button>
</view>
</template>
<script setup>
const scope = effectScope()
const counter = ref(0)
const watchCounterRes = ref('')
const watchEffectCounterRes = ref('')
scope.run(() => {
watch(counter, (newVal : number, oldVal : number) => {
watchCounterRes.value = `newVal: ${newVal}, oldVal: ${oldVal}`
})
watchEffect(() => {
watchEffectCounterRes.value = `counter: ${counter.value}`
})
})
const stopEffectScope = () => scope.stop()
</script>
\ No newline at end of file
......@@ -201,7 +201,7 @@
{
name: 'effectScope',
url: 'effect-scope',
enable: false,
enable: true,
},
{
name: 'getCurrentScope',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册