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

test: 测试例优化

上级 2c6afff2
...@@ -9,6 +9,50 @@ ...@@ -9,6 +9,50 @@
import { state, setLifeCycleNum } from '../store/index.uts' import { state, setLifeCycleNum } from '../store/index.uts'
const title = ref('component for composition API lifecycle test') const title = ref('component for composition API lifecycle test')
const emit = defineEmits<{
(e : 'updateIsScroll', val : boolean) : void
}>()
onLoad((_ : OnLoadOptions) => {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 100)
})
onPageShow(() => {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 10)
})
onReady(() => {
// 自动化测试
// TODO: onReady 未触发
setLifeCycleNum(state.lifeCycleNum + 10)
})
onPullDownRefresh(() => {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 10)
})
onPageScroll((_) => {
// 自动化测试
emit('updateIsScroll', true)
})
onReachBottom(() => {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 10)
})
onBackPress((_ : OnBackPressOptions) : boolean | null => {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 10)
return null
})
onPageHide(() => {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 10)
})
onUnload(() => {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 100)
})
onBeforeMount(() => { onBeforeMount(() => {
// 自动化测试 // 自动化测试
...@@ -46,9 +90,9 @@ ...@@ -46,9 +90,9 @@
console.log('component for lifecycle test unmounted') console.log('component for lifecycle test unmounted')
}) })
// 测试无法触发 // TODO: app-android 不触发
onActivated(() => { }) onActivated(() => { })
// 测试无法触发 // TODO: app-android 不触发
onDeactivated(() => { }) onDeactivated(() => { })
const updateTitle = () => { const updateTitle = () => {
......
...@@ -335,7 +335,7 @@ ...@@ -335,7 +335,7 @@
"navigationBarTitleText": "render function" "navigationBarTitleText": "render function"
} }
}, },
// #ifdef APP // #ifdef APP
{ {
"path": "pages/composition/mixins/mixins", "path": "pages/composition/mixins/mixins",
"style": { "style": {
...@@ -348,14 +348,14 @@ ...@@ -348,14 +348,14 @@
"navigationBarTitleText": "mixins-page-2" "navigationBarTitleText": "mixins-page-2"
} }
}, },
// #endif // #endif
// #ifdef WEB // #ifdef WEB
{ {
"path": "pages/composition/mixins/mixins-web", "path": "pages/composition/mixins/mixins-web",
"style": { "style": {
"navigationBarTitleText": "mixins" "navigationBarTitleText": "mixins"
} }
}, },
// #endif // #endif
{ {
"path": "pages/composition/provide/provide", "path": "pages/composition/provide/provide",
...@@ -600,36 +600,37 @@ ...@@ -600,36 +600,37 @@
"style": { "style": {
"navigationBarTitleText": "onScopeDispose" "navigationBarTitleText": "onScopeDispose"
} }
}, },
{ {
"path" : "pages/composition-api/lifecycle/page-lifecycle/page-lifecycle", "path" : "pages/composition-api/lifecycle/page-lifecycle/page-lifecycle",
"style" : "style" :
{ {
"navigationBarTitleText" : "页面生命周期", "navigationBarTitleText" : "页面生命周期",
"enablePullDownRefresh": true "enablePullDownRefresh": true
} }
}, },
{ {
"path" : "pages/composition-api/lifecycle/component-lifecycle/component-lifecycle", "path" : "pages/composition-api/lifecycle/component-lifecycle/component-lifecycle",
"style" : "style" :
{ {
"navigationBarTitleText" : "组件生命周期" "navigationBarTitleText": "组件生命周期",
} "enablePullDownRefresh": true
}, }
{ },
"path" : "pages/composition-api/dependency-injection/provide/provide", {
"style" : "path" : "pages/composition-api/dependency-injection/provide/provide",
{ "style" :
"navigationBarTitleText" : "依赖注入" {
} "navigationBarTitleText" : "依赖注入"
}, }
{ },
"path" : "pages/built-in-component/teleport/teleport", {
"style" : "path" : "pages/built-in-component/teleport/teleport",
{ "style" :
"navigationBarTitleText" : "teleport", {
"enablePullDownRefresh" : false "navigationBarTitleText" : "teleport",
} "enablePullDownRefresh" : false
}
} }
], ],
"tabBar": { "tabBar": {
......
<template>
<view id="teleport" class="sub-container">
<view>
<text class="text">红色区域为子组件</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
}
}
</script>
<style>
.sub-container {
background-color:red;
height:200px;
padding:10px;
}
.text {
color:#fff;
}
</style>
\ No newline at end of file
...@@ -9,9 +9,7 @@ describe('teleport', () => { ...@@ -9,9 +9,7 @@ describe('teleport', () => {
it('teleport', async () => { it('teleport', async () => {
await page.waitFor(500) await page.waitFor(500)
const image = await program.screenshot({ const image = await program.screenshot();
fullPage: true
});
expect(image).toMatchImageSnapshot(); expect(image).toMatchImageSnapshot();
}) })
}) })
...@@ -4,14 +4,12 @@ ...@@ -4,14 +4,12 @@
<!-- #endif --> <!-- #endif -->
<view id="container" class="container"> <view id="container" class="container">
<text class="text">.container 区域</text> <text class="text">.container 区域</text>
<!-- <subContainer></subContainer> -->
<view id="content1" class="content content-1"> <view id="content1" class="content content-1">
<text class="text">content1 区域</text> <text class="text">content1 区域</text>
</view> </view>
<view id="content2" class="content content-2"> <view id="content2" class="content content-2">
<text class="text">content2 区域</text> <text class="text">content2 区域</text>
</view> </view>
<!-- <button class="btn" @click="changePosition">点击穿梭</button> -->
<teleport v-if="show" to="#content1" :disabled="disabled"> <teleport v-if="show" to="#content1" :disabled="disabled">
<view class="send-content"> <view class="send-content">
<text class="send-content-text">内容在.container中,使用teleport在#content1元素中展示</text> <text class="send-content-text">内容在.container中,使用teleport在#content1元素中展示</text>
...@@ -24,9 +22,7 @@ ...@@ -24,9 +22,7 @@
</template> </template>
<script> <script>
// import subContainer from './sub-container.uvue'
export default { export default {
// components: { subContainer },
data() { data() {
return { return {
to: 'content1', to: 'content1',
......
const PAGE_PATH = '/pages/composition-api/basic/define-props/define-props' const PAGE_PATH = '/pages/composition-api/basic/define-props/define-props'
describe('defineProps', () => { describe('defineProps', () => {
console.log('process.env.uniTestPlatformInfo---------------', process.env.uniTestPlatformInfo)
const isSafari = process.env.uniTestPlatformInfo.indexOf('safari') > -1 const isSafari = process.env.uniTestPlatformInfo.indexOf('safari') > -1
let page = null let page = null
beforeAll(async () => { beforeAll(async () => {
......
const PAGE_PATH = '/pages/composition-api/lifecycle/component-lifecycle/component-lifecycle' const PAGE_PATH = '/pages/composition-api/lifecycle/component-lifecycle/component-lifecycle'
const INTER_PAGE_PATH = '/pages/app-instance/index/index'
const HOME_PATH = '/pages/tab-bar/options-api' const HOME_PATH = '/pages/tab-bar/options-api'
describe('component-lifecycle', () => { describe('component-lifecycle', () => {
let page let page
let lifeCycleNum let lifeCycleNum
beforeAll(async () => { beforeAll(async () => {
page = await program.reLaunch(HOME_PATH) page = await program.reLaunch(HOME_PATH)
await page.waitFor(700) await page.waitFor(700)
const initLifecycleNum = 0 const initLifecycleNum = 0
await page.callMethod('setLifeCycleNum', initLifecycleNum) await page.callMethod('setLifeCycleNum', initLifecycleNum)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(initLifecycleNum) expect(lifeCycleNum).toBe(initLifecycleNum)
page = await program.navigateTo(PAGE_PATH) page = await program.navigateTo(PAGE_PATH)
await page.waitFor(700) await page.waitFor(700)
}) })
afterAll(async () => { // afterAll(async () => {
const resetLifecycleNum = 1100 // const resetLifecycleNum = 1100
await page.callMethod('setLifeCycleNum', resetLifecycleNum) // await page.callMethod('setLifeCycleNum', resetLifecycleNum)
lifeCycleNum = await page.callMethod('getLifeCycleNum') // lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(resetLifecycleNum) // expect(lifeCycleNum).toBe(resetLifecycleNum)
}) // })
it('onBeforeMount onMounted', async () => { it('onLoad onPageShow onReady onBeforeMount onMounted', async () => {
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(2) expect(lifeCycleNum).toBe(112)
}) await page.callMethod('pageSetlifeCycleNum', 0)
it('onBeforeUpdate onUpdated', async () => { })
const updateTitleBtn = await page.$('.component-lifecycle-btn') it('onBeforeUpdate onUpdated', async () => {
await updateTitleBtn.tap() const updateTitleBtn = await page.$('.component-lifecycle-btn')
lifeCycleNum = await page.callMethod('getLifeCycleNum') await updateTitleBtn.tap()
expect(lifeCycleNum).toBe(4) lifeCycleNum = await page.callMethod('getLifeCycleNum')
}) expect(lifeCycleNum).toBe(2)
it('beforeUnmount unmounted', async () => { await page.callMethod('pageSetlifeCycleNum', 0)
page = await program.navigateBack() })
lifeCycleNum = await page.callMethod('getLifeCycleNum') it('onPullDownRefresh', async () => {
expect(lifeCycleNum).toBe(2) await page.callMethod('pullDownRefresh')
}) await page.waitFor(1500)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(10)
await page.callMethod('pageSetlifeCycleNum', 0)
})
it('onPageScroll onReachBottom', async () => {
await program.pageScrollTo(2000)
// 测试 web 端组件内监听 onPageScroll 不触发
if (process.env.uniTestPlatformInfo.startsWith('android')) {
const isScrolled = await page.callMethod('getIsScrolled')
expect(isScrolled).toBe(true)
}
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(10)
await page.callMethod('pageSetlifeCycleNum', 0)
})
it('onHide', async () => {
page = await program.navigateTo(INTER_PAGE_PATH)
await page.waitFor('view')
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(-10)
page = await program.navigateBack()
await page.waitFor('view')
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(0)
await page.callMethod('pageSetlifeCycleNum', 0)
})
it('beforeUnmount unmounted onUnload onBackPress', async () => {
page = await program.navigateBack()
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(-112)
await page.callMethod('setLifeCycleNum', 0)
})
}) })
\ No newline at end of file
<template> <template>
<view class="page"> <!-- #ifdef APP -->
<text class="uni-common-mb">component lifecycle</text> <scroll-view style="flex:1">
<component-lifecycle class="component-lifecycle" /> <!-- #endif -->
</view> <view class="pag container">
<text class="uni-common-mb">component lifecycle</text>
<component-lifecycle class="component-lifecycle" @updateIsScroll="updateIsScroll" />
<button class="uni-common-mt" @click="scrollToBottom">scrollToBottom</button>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template> </template>
<script setup> <script setup>
import ComponentLifecycle from '@/components/CompositionAPILifecycle.uvue' import ComponentLifecycle from '@/components/CompositionAPILifecycle.uvue'
import { state } from '@/store/index.uts' import { state, setLifeCycleNum } from '@/store/index.uts'
const isScrolled = ref(false)
// 自动化测试 // 自动化测试
const getLifeCycleNum = () : number => { const getLifeCycleNum = () : number => {
return state.lifeCycleNum return state.lifeCycleNum
} }
// 自动化测试
const pageSetlifeCycleNum = (num : number) => {
setLifeCycleNum(num)
}
// 自动化测试
const pullDownRefresh = () => {
uni.startPullDownRefresh({
success() {
setTimeout(() => {
uni.stopPullDownRefresh()
}, 1000)
},
})
}
const scrollToBottom = () => {
uni.pageScrollTo({
scrollTop: 3000,
})
}
const updateIsScroll = (val : boolean) => {
isScrolled.value = val
}
// 自动化测试
const getIsScrolled = () : boolean => {
return isScrolled.value
}
defineExpose({ defineExpose({
getLifeCycleNum getLifeCycleNum,
pageSetlifeCycleNum,
pullDownRefresh,
scrollToBottom,
getIsScrolled
}) })
</script> </script>
\ No newline at end of file
<style>
.container {
height: 1200px;
}
</style>
\ No newline at end of file
...@@ -5,13 +5,6 @@ let page ...@@ -5,13 +5,6 @@ let page
let lifeCycleNum let lifeCycleNum
describe('page-lifecycle', () => { describe('page-lifecycle', () => {
if (process.env.uniTestPlatformInfo.startsWith('web')) {
// TODO: onPageShow onPageHide 别名暂不支持
it('web', async () => {
expect(1).toBe(1)
})
return
}
beforeAll(async () => { beforeAll(async () => {
page = await program.reLaunch(HOME_PATH) page = await program.reLaunch(HOME_PATH)
await page.waitFor(700) await page.waitFor(700)
...@@ -29,14 +22,14 @@ describe('page-lifecycle', () => { ...@@ -29,14 +22,14 @@ describe('page-lifecycle', () => {
await page.waitFor(700) await page.waitFor(700)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(120) expect(lifeCycleNum).toBe(120)
await page.callMethod('pageSetlifeCycleNum', 0) await page.callMethod('pageSetLifeCycleNum', 0)
}) })
it('onPullDownRefresh', async () => { it('onPullDownRefresh', async () => {
await page.callMethod('pullDownRefresh') await page.callMethod('pullDownRefresh')
await page.waitFor(1500) await page.waitFor(1500)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(10) expect(lifeCycleNum).toBe(10)
await page.callMethod('pageSetlifeCycleNum', 0) await page.callMethod('pageSetLifeCycleNum', 0)
}) })
it('onPageScroll onReachBottom', async () => { it('onPageScroll onReachBottom', async () => {
await program.pageScrollTo(2000) await program.pageScrollTo(2000)
...@@ -44,7 +37,7 @@ describe('page-lifecycle', () => { ...@@ -44,7 +37,7 @@ describe('page-lifecycle', () => {
expect(isScrolled).toBe(true) expect(isScrolled).toBe(true)
lifeCycleNum = await page.callMethod('getLifeCycleNum') lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(10) expect(lifeCycleNum).toBe(10)
await page.callMethod('pageSetlifeCycleNum', 0) await page.callMethod('pageSetLifeCycleNum', 0)
}) })
it('onHide', async () => { it('onHide', async () => {
page = await program.navigateTo(INTER_PAGE_PATH) page = await program.navigateTo(INTER_PAGE_PATH)
......
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
return state.lifeCycleNum return state.lifeCycleNum
} }
// 自动化测试 // 自动化测试
const pageSetlifeCycleNum = (num: number) => { const pageSetLifeCycleNum = (num: number) => {
setLifeCycleNum(num) setLifeCycleNum(num)
} }
...@@ -91,7 +91,7 @@ ...@@ -91,7 +91,7 @@
defineExpose({ defineExpose({
getLifeCycleNum, getLifeCycleNum,
pageSetlifeCycleNum, pageSetLifeCycleNum,
pullDownRefresh, pullDownRefresh,
scrollToBottom, scrollToBottom,
getIsScrolled getIsScrolled
......
...@@ -7,8 +7,11 @@ ...@@ -7,8 +7,11 @@
</template> </template>
<script setup> <script setup>
const useCustomRef = (value : UTSJSONObject) : Ref<UTSJSONObject> => { import type { Ref } from 'vue'
return customRef((track, trigger) : UTSJSONObject => {
const useCustomRef = (value : UTSJSONObject) : Ref<UTSJSONObject> => {
// @ts-ignore
return customRef<UTSJSONObject>((track, trigger) : UTSJSONObject => {
return { return {
get() : UTSJSONObject { get() : UTSJSONObject {
track() track()
...@@ -18,11 +21,11 @@ ...@@ -18,11 +21,11 @@
value = newValue value = newValue
trigger() trigger()
} }
} } as UTSJSONObject
}) })
} }
const state = useCustomRef({ count: 0 }) const state = useCustomRef({ count: 0 } as UTSJSONObject)
const increment = () => { const increment = () => {
(state.value as UTSJSONObject)['count'] = ((state.value as UTSJSONObject)['count'] as number) + 1 (state.value as UTSJSONObject)['count'] = ((state.value as UTSJSONObject)['count'] as number) + 1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册