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

feat(composition api): toRefs

上级 21982966
...@@ -9,26 +9,26 @@ describe('toRef', () => { ...@@ -9,26 +9,26 @@ describe('toRef', () => {
}) })
it('basic', async () => { it('basic', async () => {
const count = await page.$('#count') const count = await page.$('#count')
expect(await count.text()).toBe('count: 0') expect(await count.text()).toBe('count: 0')
const isRefCount = await page.$('#is-ref-count') const isRefCount = await page.$('#is-ref-count')
expect(await isRefCount.text()).toBe('isRef count: false') expect(await isRefCount.text()).toBe('isRef count: false')
const refCount = await page.$('#ref-count') const refCount = await page.$('#ref-count')
expect(await refCount.text()).toBe('ref count: 0') expect(await refCount.text()).toBe('ref count: 0')
const isRefRefCount = await page.$('#is-ref-ref-count') const isRefRefCount = await page.$('#is-ref-ref-count')
expect(await isRefRefCount.text()).toBe('isRef ref count: true') expect(await isRefRefCount.text()).toBe('isRef ref count: true')
const objNum = await page.$('#obj-num') const objNum = await page.$('#obj-num')
expect(await objNum.text()).toBe('obj.num: 0') expect(await objNum.text()).toBe('obj.num: 0')
const toRefObjNum = await page.$('#to-ref-obj-num') const toRefObjNum = await page.$('#to-ref-obj-num')
expect(await toRefObjNum.text()).toBe('toRef(obj, "num"): 0') expect(await toRefObjNum.text()).toBe('toRef(obj, "num"): 0')
const toRefFnObjNum = await page.$('#to-ref-fn-obj-num') const toRefFnObjNum = await page.$('#to-ref-fn-obj-num')
expect(await toRefFnObjNum.text()).toBe('toRef(() => obj.num): 0') expect(await toRefFnObjNum.text()).toBe('toRef(() => obj.num): 0')
const incrementBtn = await page.$('#increment-btn') const incrementBtn = await page.$('#increment-btn')
await incrementBtn.tap() await incrementBtn.tap()
expect(await objNum.text()).toBe('obj.num: 2') expect(await objNum.text()).toBe('obj.num: 2')
expect(await toRefObjNum.text()).toBe('toRef(obj, "num"): 2') expect(await toRefObjNum.text()).toBe('toRef(obj, "num"): 2')
expect(await toRefFnObjNum.text()).toBe('toRef(() => obj.num): 2') expect(await toRefFnObjNum.text()).toBe('toRef(() => obj.num): 2')
}) })
} else { } else {
......
const PAGE_PATH = '/pages/composition-api/reactivity/to-refs/to-refs'
describe('toRefs', () => {
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 stateNum = await page.$('#state-num')
expect(await stateNum.text()).toBe('state.num: 0')
const stateStr = await page.$('#state-str')
expect(await stateStr.text()).toBe('state.str: str-0')
const stateAsRefsNum = await page.$('#state-as-refs-num')
expect(await stateAsRefsNum.text()).toBe('stateAsRefs.num: 0')
const stateAsRefsStr = await page.$('#state-as-refs-str')
expect(await stateAsRefsStr.text()).toBe('stateAsRefs.str: str-0')
const updateStateBtn = await page.$('#update-state-btn')
await updateStateBtn.tap()
expect(await stateNum.text()).toBe('state.num: 1')
expect(await stateStr.text()).toBe('state.str: str-1')
expect(await stateAsRefsNum.text()).toBe('stateAsRefs.num: 1')
expect(await stateAsRefsStr.text()).toBe('stateAsRefs.str: str-1')
})
} else {
it('other platform', () => {
expect(1).toBe(1)
})
}
})
\ No newline at end of file
<template><view class="page">toRefs</view></template> <template>
\ No newline at end of file <view class="page">
<text id="state-num">state.num: {{ state['num'] }}</text>
<text class="uni-common-mt" id="state-str">state.str: {{ state['str'] }}</text>
<text class="uni-common-mt" id="state-as-refs-num">stateAsRefs.num:
{{ (stateAsRefs['num'] as Ref<number>).value }}</text>
<text class="uni-common-mt" id="state-as-refs-str">stateAsRefs.str:
{{ (stateAsRefs['str'] as Ref<string>).value}}</text>
<button class="uni-common-mt" id="update-state-btn" @click="updateState">update state</button>
</view>
</template>
<script setup>
// toRefs 仅支持 array 和 UTSJSONObject, 不支持自定义类型
const state = reactive({
num: 0,
str: 'str-0'
})
const stateAsRefs = toRefs(state)
const updateState = () => {
state['num'] = (state['num'] as number) + 1;
(stateAsRefs['str'] as Ref<string>).value = `str-${(stateAsRefs['num'] as Ref<number>).value}`
}
</script>
\ No newline at end of file
...@@ -146,7 +146,7 @@ ...@@ -146,7 +146,7 @@
{ {
name: 'toRefs', name: 'toRefs',
url: 'to-refs', url: 'to-refs',
enable: false, enable: true,
}, },
{ {
name: 'isProxy', name: 'isProxy',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册