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

feat(component): is 补充内置组件示例

上级 b02fc844
<template> <template>
<view class="container"> <view class="container">
<component :is="currentComponentStr" /> <component :is="currentComponentInstance" />
<component :is="currentComponentInstance" /> <component :is="myInput" :value="inputValue" id="my-input" />
<button @click="changeCurrentComponent">change current component</button> <button @click="changeCurrentComponent">change current component</button>
</view> </view>
</template> </template>
<script lang="uts"> <script setup lang="uts">
import Foo from '../../../../components/Foo.uvue' import Foo from '../../../../components/Foo.uvue'
import Bar from '../../../../components/Bar.uvue' import Bar from '../../../../components/Bar.uvue'
export default {
components: { const currentComponentStr = ref('Foo')
Foo, const currentComponentInstance = ref(Foo)
Bar const myInput = ref('input')
}, const inputValue = ref('default value')
data() {
return { const changeCurrentComponent = () => {
currentComponentStr: 'Foo', if (currentComponentStr.value === 'Foo') {
currentComponentInstance: Foo currentComponentStr.value = 'Bar'
} currentComponentInstance.value = Bar
}, } else {
methods: { currentComponentStr.value = 'Foo'
changeCurrentComponent() { currentComponentInstance.value = Foo
if (this.currentComponentStr === 'Foo') { }
this.currentComponentStr = 'Bar' }
this.currentComponentInstance = Bar
} else { defineExpose({
this.currentComponentStr = 'Foo' changeCurrentComponent
this.currentComponentInstance = Foo })
} </script>
}
} <style>
} .item {
</script> display: flex;
flex-direction: row;
<style> margin: 15px;
.item { border: #eee solid 1px;
display: flex; }
flex-direction: row;
margin: 15px;
border: #eee solid 1px;
}
</style> </style>
\ No newline at end of file
<template> <template>
<view class="container"> <view class="container">
<component :is="currentComponentStr" /> <component :is="currentComponentStr" />
<component :is="currentComponentInstance" /> <component :is="currentComponentInstance" />
<button @click="changeCurrentComponent">change current component</button> <component :is="myInput" :value="inputValue" id="my-input" />
</view> <button @click="changeCurrentComponent">change current component</button>
</template> </view>
</template>
<script lang="uts">
import Foo from '../../../../components/Foo.uvue' <script lang="uts">
import Bar from '../../../../components/Bar.uvue' import Foo from '../../../../components/Foo.uvue'
export default { import Bar from '../../../../components/Bar.uvue'
components: { export default {
Foo, components: {
Bar Foo,
}, Bar
data() { },
return { data() {
currentComponentStr: 'Foo', return {
currentComponentInstance: Foo currentComponentStr: 'Foo',
} currentComponentInstance: Foo,
}, myInput: 'input',
methods: { inputValue: 'default value'
changeCurrentComponent() { }
if (this.currentComponentStr === 'Foo') { },
this.currentComponentStr = 'Bar' methods: {
this.currentComponentInstance = Bar changeCurrentComponent() {
} else { if (this.currentComponentStr === 'Foo') {
this.currentComponentStr = 'Foo' this.currentComponentStr = 'Bar'
this.currentComponentInstance = Foo this.currentComponentInstance = Bar
} } else {
} this.currentComponentStr = 'Foo'
} this.currentComponentInstance = Foo
} }
</script> }
}
<style> }
.item { </script>
display: flex;
flex-direction: row; <style>
margin: 15px; .item {
border: #eee solid 1px; display: flex;
} flex-direction: row;
margin: 15px;
border: #eee solid 1px;
}
</style> </style>
\ No newline at end of file
const PAGE_OPTIONS = '/pages/built-in/special-elements/component/component-options' const PAGE_OPTIONS = '/pages/built-in/special-elements/component/component-options'
const PAGE_COMPOSITION = '/pages/built-in/special-elements/component/component-composition' const PAGE_COMPOSITION = '/pages/built-in/special-elements/component/component-composition'
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
describe('built-in/component', () => { const isWeb = platformInfo.startsWith('web')
let page
const test = async () => { describe('built-in/component', () => {
await page.waitFor('view') let page
let fooList = await page.$$('.component-foo') const test = async (pagePath) => {
expect(fooList.length).toBe(2) page = await program.reLaunch(pagePath)
expect(await fooList[0].text()).toBe('this is component Foo') await page.waitFor('view')
expect(await fooList[1].text()).toBe('this is component Foo') const isOptionsPage = pagePath.indexOf('component-options') !== -1
let fooList = await page.$$('.component-foo')
let barList = await page.$$('.component-bar') expect(fooList.length).toBe(isOptionsPage ? 2 : 1)
expect(barList.length).toBe(0) expect(await fooList[0].text()).toBe('this is component Foo')
if (isOptionsPage) {
await page.callMethod('changeCurrentComponent') expect(await fooList[1].text()).toBe('this is component Foo')
}
fooList = await page.$$('.component-foo')
expect(fooList.length).toBe(0) let barList = await page.$$('.component-bar')
expect(barList.length).toBe(0)
barList = await page.$$('.component-bar')
expect(barList.length).toBe(2) await page.callMethod('changeCurrentComponent')
expect(await barList[0].text()).toBe('this is component Bar')
expect(await barList[1].text()).toBe('this is component Bar') fooList = await page.$$('.component-foo')
} expect(fooList.length).toBe(0)
it('component Options API', async () => {
page = await program.reLaunch(PAGE_OPTIONS) barList = await page.$$('.component-bar')
await test() expect(barList.length).toBe(isOptionsPage ? 2 : 1)
}) expect(await barList[0].text()).toBe('this is component Bar')
it('component Composition API', async () => { if (isOptionsPage) {
page = await program.reLaunch(PAGE_COMPOSITION) expect(await barList[1].text()).toBe('this is component Bar')
await test() }
}) // web 端自动化测试,无法获取非 uni 元素
if (!isWeb) {
const myInput = await page.$('#my-input')
expect(await myInput.property('value')).toBe('default value')
}
}
it('component Options API', async () => {
await test(PAGE_OPTIONS)
})
it('component Composition API', async () => {
await test(PAGE_COMPOSITION)
})
}); });
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册