提交 96eabdd6 编写于 作者: DCloud-WZF's avatar DCloud-WZF :speech_balloon:

refactor(reactivity): shallowReadonly

......@@ -607,6 +607,11 @@ export default {
name: 'shallowReactive',
url: 'shallow-reactive/shallow-reactive'
},
{
id: 'shallow-readonly',
name: 'shallowReadonly',
url: 'shallow-readonly/shallow-readonly'
},
]
}
] as Page[]
......
......@@ -22,6 +22,7 @@ describe('shallowReactive', () => {
await incrementStateCountBtn.tap()
expect(await stateCount.text()).toBe('1')
// TODO: web 失败,获取到的还是 0
expect(await stateNestedCount.text()).toBe('1')
})
})
\ No newline at end of file
const PAGE_PATH = '/pages/composition-api/reactivity/shallow-readonly/shallow-readonly'
const PAGE_PATH = '/pages/reactivity/advanced/shallow-readonly/shallow-readonly'
describe('shallowReadonly', () => {
let page = null
......@@ -8,10 +8,10 @@ describe('shallowReadonly', () => {
})
it('basic', async () => {
let stateCount = await page.$('#state-count')
expect(await stateCount.text()).toBe('state.count: 0')
expect(await stateCount.text()).toBe('0')
let stateNestedCount = await page.$('#state-nested-count')
expect(await stateNestedCount.text()).toBe('state.nested.count: 0')
expect(await stateNestedCount.text()).toBe('0')
if (process.env.uniTestPlatformInfo.startsWith('web')) {
// web端操作readonly对象会直接编译失败,以下测试无法执行
......@@ -24,15 +24,15 @@ describe('shallowReadonly', () => {
const incrementStateNestedCountBtn = await page.$('#increment-state-nested-count-btn')
await incrementStateNestedCountBtn.tap()
expect(await stateCount.text()).toBe('state.count: 0')
expect(await stateNestedCount.text()).toBe('state.nested.count: 0')
expect(await stateCount.text()).toBe('0')
expect(await stateNestedCount.text()).toBe('0')
const updatePageRenderBtn = await page.$('#update-page-render-btn')
await updatePageRenderBtn.tap()
stateCount = await page.$('#state-count')
expect(await stateCount.text()).toBe('state.count: 0')
expect(await stateCount.text()).toBe('0')
stateNestedCount = await page.$('#state-nested-count')
expect(await stateNestedCount.text()).toBe('state.nested.count: 1')
expect(await stateNestedCount.text()).toBe('1')
})
})
\ No newline at end of file
<template>
<view :key="pageKey" class="page">
<text id="state-count" class="mb-10">state.count: {{ state.count }}</text>
<text id="state-nested-count" class="mb-10">state.nested.count: {{ state.nested.count }}</text>
<button id="increment-state-count-btn" class="mb-10" @click="incrementStateCount">
<view class="flex justify-between flex-row mb-10">
<text>state.count:</text>
<text id="state-count">{{ state.count }}</text>
</view>
<view class="flex justify-between flex-row mb-10">
<text>state.nested.count:</text>
<text id="state-nested-count">{{ state.nested.count }}</text>
</view>
<button
id="increment-state-count-btn"
class="mb-10"
@click="incrementStateCount">
increment state.count
</button>
<button id="increment-state-nested-count-btn" class="mb-10" @click="incrementStateNestedCount">
<button
id="increment-state-nested-count-btn"
class="mb-10"
@click="incrementStateNestedCount">
increment state.nested.count
</button>
<button id="update-page-render-btn" @click="updatePageRender">update page render</button>
<button id="update-page-render-btn" @click="updatePageRender">
update page render
</button>
</view>
</template>
<script setup>
let pageKey = ref<number>(0)
<script setup lang="uts">
let pageKey = ref<number>(0)
type StateNested = {
count : number
}
type State = {
count : number,
nested : StateNested
}
const state = shallowReadonly({
count: 0,
nested: {
count: 0
} as StateNested
} as State)
type StateNested = {
count : number
}
type State = {
count : number,
nested : StateNested
}
const state = shallowReadonly({
count: 0,
nested: {
count: 0
} as StateNested
} as State)
// #ifdef APP
const incrementStateCount = () => {
state.count++
}
// #ifdef APP
const incrementStateCount = () => {
state.count++
}
const incrementStateNestedCount = () => {
state.nested.count++
}
// #endif
const incrementStateNestedCount = () => {
state.nested.count++
}
// #endif
const updatePageRender = () => {
pageKey.value = Date.now()
}
</script>
\ No newline at end of file
const updatePageRender = () => {
pageKey.value = Date.now()
}
</script>
......@@ -146,7 +146,7 @@ function transform(fileInfo, api) {
- [x] markRaw
- [x] onScopeDispose
- [x] shallowReactive
- [ ] shallowReadonly
- [x] shallowReadonly
- [ ] shallowRef
- [ ] toRaw
- [ ] triggerRef
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册