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

refactor: 优化示例及测试

上级 ee96f308
<template> <template>
<view class="page"> <view class="page">
<template v-if="dataInfo.isShow"> <template v-if="dataInfo.isShow">
<view class="title">{{ title }}</view> <text id="title">{{ title }}</text>
</template> </template>
<view class="show-botton" @click="handleShow">{{ dataInfo.isShow ? '点击隐藏' : '点击显示' }}</view> <button id="show-botton" @click="handleShow">{{ dataInfo.isShow ? '点击隐藏' : '点击显示' }}</button>
<template v-for="(item, index) in list" :key="index"> <template v-for="(item, index) in list" :key="index">
<view class="item">{{ index + 1 }}.{{ item.name }}</view> <view class="item">{{ index + 1 }}.{{ item.name }}</view>
</template> </template>
...@@ -31,14 +31,14 @@ const list = ref<ListItem[]>([ ...@@ -31,14 +31,14 @@ const list = ref<ListItem[]>([
{ {
name: 'foo2' name: 'foo2'
} }
] as ListItem[]) ])
const handleShow = () => { const handleShow = () => {
dataInfo.isShow = !dataInfo.isShow dataInfo.isShow = !dataInfo.isShow
} }
const goMapStyle = () => { const goMapStyle = () => {
uni.navigateTo({ url: '/pages/built-in/component/template/template-map-style-composition' }) uni.navigateTo({ url: '/pages/built-in/special-elements/template/template-map-style-composition' })
} }
defineExpose({ defineExpose({
......
<template> <template>
<view class="page"> <view class="page">
<template v-if="dataInfo.isShow"> <template v-if="dataInfo.isShow">
<view class="title">{{ title }}</view> <text id="title">{{ title }}</text>
</template> </template>
<view class="show-botton" @click="handleShow">{{ dataInfo.isShow ? '点击隐藏' : '点击显示' }}</view> <button id="show-botton" @click="handleShow">{{ dataInfo.isShow ? '点击隐藏' : '点击显示' }}</button>
<template v-for="(item, index) in list" :key="index"> <template v-for="(item, index) in list" :key="index">
<view class="item">{{ index + 1 }}.{{ item.name }}</view> <view class="item">{{ index + 1 }}.{{ item.name }}</view>
</template> </template>
...@@ -39,7 +39,7 @@ export default { ...@@ -39,7 +39,7 @@ export default {
this.dataInfo.isShow = !this.dataInfo.isShow this.dataInfo.isShow = !this.dataInfo.isShow
}, },
goMapStyle() { goMapStyle() {
uni.navigateTo({ url: '/pages/built-in/component/template/template-map-style-options' }) uni.navigateTo({ url: '/pages/built-in/special-elements/template/template-map-style-options' })
} }
} }
} }
......
...@@ -5,16 +5,15 @@ describe('built-in/special-elements/component', () => { ...@@ -5,16 +5,15 @@ describe('built-in/special-elements/component', () => {
let page let page
const test = async () => { const test = async () => {
await page.waitFor('view') await page.waitFor('view')
expect.assertions(4); const showBtn = await page.$('#show-botton')
const showBtn = await page.$('.show-botton') expect(await showBtn.text()).toBe("点击显示")
await showBtn.tap() await showBtn.tap()
const dataInfo = await page.data('dataInfo') const dataInfo = await page.data('dataInfo')
expect(dataInfo.isShow).toBeTruthy() expect(dataInfo.isShow).toBeTruthy()
const getTitle = await page.$('.title') const getTitle = await page.$('#title')
expect(await getTitle.text()).toBe("hello") expect(await getTitle.text()).toBe("hello")
const getShow = await page.$('.show-botton') expect(await showBtn.text()).toBe("点击隐藏")
expect(await getShow.text()).toBe("点击隐藏")
expect((await page.$$('.item')).length).toBe(2) expect((await page.$$('.item')).length).toBe(2)
} }
it('template Options API', async () => { it('template Options API', async () => {
......
...@@ -15,12 +15,12 @@ describe('shallowReactive', () => { ...@@ -15,12 +15,12 @@ describe('shallowReactive', () => {
const stateNestedCount = await page.$('#state-nested-count') const stateNestedCount = await page.$('#state-nested-count')
expect(await stateNestedCount.text()).toBe('0') expect(await stateNestedCount.text()).toBe('0')
const incrementStateNestedCountBtn = await page.$('.increment-state-nested-count-btn') const incrementStateNestedCountBtn = await page.$('#increment-state-nested-count-btn')
await incrementStateNestedCountBtn.tap() await incrementStateNestedCountBtn.tap()
expect(await stateNestedCount.text()).toBe('0') expect(await stateNestedCount.text()).toBe('0')
const incrementStateCountBtn = await page.$('.increment-state-count-btn') const incrementStateCountBtn = await page.$('#increment-state-count-btn')
await incrementStateCountBtn.tap() await incrementStateCountBtn.tap()
if (isWeb) { if (isWeb) {
......
...@@ -8,13 +8,14 @@ ...@@ -8,13 +8,14 @@
<text>state.nested.count:</text> <text>state.nested.count:</text>
<text id="state-nested-count">{{ state.nested.count }}</text> <text id="state-nested-count">{{ state.nested.count }}</text>
</view> </view>
<button <button
class="increment-state-count-btn mb-10" id="increment-state-count-btn"
class="mb-10"
@click="incrementStateCount"> @click="incrementStateCount">
increment state.count increment state.count
</button> </button>
<button <button
class="increment-state-nested-count-btn" id="increment-state-nested-count-btn"
@click="incrementStateNestedCount"> @click="incrementStateNestedCount">
increment state.nested.count increment state.nested.count
</button> </button>
...@@ -28,13 +29,14 @@ type StateNested = { ...@@ -28,13 +29,14 @@ type StateNested = {
type State = { type State = {
count : number, count : number,
nested : StateNested nested : StateNested
} }
const state = shallowReactive({ // 可通过泛型指定类型
const state = shallowReactive<State>({
count: 0, count: 0,
nested: { nested: {
count: 0 count: 0
} as StateNested }
} as State) })
const incrementStateCount = () => { const incrementStateCount = () => {
state.count++ state.count++
......
...@@ -35,13 +35,14 @@ type StateNested = { ...@@ -35,13 +35,14 @@ type StateNested = {
type State = { type State = {
count : number, count : number,
nested : StateNested nested : StateNested
} }
const state = shallowReadonly({ // 可通过泛型指定类型
const state = shallowReadonly<State>({
count: 0, count: 0,
nested: { nested: {
count: 0 count: 0
} as StateNested }
} as State) })
// #ifdef APP // #ifdef APP
const incrementStateCount = () => { const incrementStateCount = () => {
......
...@@ -10,12 +10,12 @@ describe('shallowRef', () => { ...@@ -10,12 +10,12 @@ describe('shallowRef', () => {
const stateCount = await page.$('#state-count') const stateCount = await page.$('#state-count')
expect(await stateCount.text()).toBe('0') expect(await stateCount.text()).toBe('0')
const incrementStateCountBtn = await page.$('.increment-state-count-btn') const incrementStateCountBtn = await page.$('#increment-state-count-btn')
await incrementStateCountBtn.tap() await incrementStateCountBtn.tap()
expect(await stateCount.text()).toBe('0') expect(await stateCount.text()).toBe('0')
const updateStateBtn = await page.$('.update-state-btn') const updateStateBtn = await page.$('#update-state-btn')
await updateStateBtn.tap() await updateStateBtn.tap()
expect(await stateCount.text()).toBe('1') expect(await stateCount.text()).toBe('1')
......
...@@ -4,12 +4,13 @@ ...@@ -4,12 +4,13 @@
<text>state.count:</text> <text>state.count:</text>
<text id="state-count">{{ state.count }}</text> <text id="state-count">{{ state.count }}</text>
</view> </view>
<button <button
class="increment-state-count-btn mb-10" id="increment-state-count-btn"
class="mb-10"
@click="incrementStateCount"> @click="incrementStateCount">
increment state.count increment state.count
</button> </button>
<button class="update-state-btn" @click="updateState">update state</button> <button id="update-state-btn" @click="updateState">update state</button>
</view> </view>
</template> </template>
...@@ -17,10 +18,10 @@ ...@@ -17,10 +18,10 @@
type State = { type State = {
count: number count: number
} }
// 可通过泛型指定类型
const state = shallowRef({ const state = shallowRef<State>({
count: 0 count: 0
} as State) })
const incrementStateCount = () => { const incrementStateCount = () => {
state.value.count++ state.value.count++
......
...@@ -19,7 +19,7 @@ describe('reactive', () => { ...@@ -19,7 +19,7 @@ describe('reactive', () => {
const objArr = await page.$('#obj-arr') const objArr = await page.$('#obj-arr')
expect(await objArr.text()).toBe('["a","b","c"]') expect(await objArr.text()).toBe('["a","b","c"]')
const updateBtn = await page.$('.update-btn') const updateBtn = await page.$('#update-btn')
await updateBtn.tap() await updateBtn.tap()
expect(await count.text()).toBe('2') expect(await count.text()).toBe('2')
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<text>obj.arr:</text> <text>obj.arr:</text>
<text id="obj-arr">{{ JSON.stringify(obj['arr']) }}</text> <text id="obj-arr">{{ JSON.stringify(obj['arr']) }}</text>
</view> </view>
<button class="update-btn" @click="update">update</button> <button id="update-btn" @click="update">update</button>
</view> </view>
</template> </template>
......
...@@ -40,14 +40,14 @@ type Data = { ...@@ -40,14 +40,14 @@ type Data = {
num : number, num : number,
arr : string[] arr : string[]
} }
// 可通过泛型指定类型
const data = reactive({ const data = reactive<Data>({
str: 'default str', str: 'default str',
num: 0, num: 0,
arr: ['a', 'b', 'c'] arr: ['a', 'b', 'c']
} as Data) })
// 可通过泛型指定类型
const readonlyData = readonly(data) const readonlyData = readonly<Data>(data)
const updateData = () => { const updateData = () => {
data.str = 'new str' data.str = 'new str'
......
...@@ -14,7 +14,7 @@ describe('ref', () => { ...@@ -14,7 +14,7 @@ describe('ref', () => {
const counterCount = await page.$('#counter-count') const counterCount = await page.$('#counter-count')
expect(await counterCount.text()).toBe('counter.count: 0') expect(await counterCount.text()).toBe('counter.count: 0')
const incrementBtn = await page.$('.increment-btn') const incrementBtn = await page.$('#increment-btn')
await incrementBtn.tap() await incrementBtn.tap()
expect(await count1.text()).toBe('count1: 2') expect(await count1.text()).toBe('count1: 2')
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<text id="count1" class="mb-10">count1: {{ count1 }}</text> <text id="count1" class="mb-10">count1: {{ count1 }}</text>
<text id="count2" class="mb-10">count2: {{ count2 }}</text> <text id="count2" class="mb-10">count2: {{ count2 }}</text>
<text id="counter-count" class="mb-10">counter.count: {{ counter.count }}</text> <text id="counter-count" class="mb-10">counter.count: {{ counter.count }}</text>
<button class="increment-btn" @click="increment">increment</button> <button id="increment-btn" @click="increment">increment</button>
</view> </view>
</template> </template>
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
type Counter = { type Counter = {
count : number count : number
} }
// TODO: Android 端暂不支持通过泛型指定类型,可通过 as 方式指定类型 // 可通过泛型指定类型
const counter = ref({ const counter = ref<Counter>({
count: 0 count: 0
} as Counter) })
const increment = () => { const increment = () => {
count1.value++ count1.value++
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册