提交 03aa2702 编写于 作者: 雪洛's avatar 雪洛

feat: 增加uniCloud使用泛型测试例

上级 04d14289
......@@ -14,7 +14,7 @@ describe('unicloud-call-function', () => {
await page.callMethod('callFunction')
const {
callFunctionResult,
callFunctionError
callFunctionError,
} = await page.data()
console.error(callFunctionResult)
console.error(callFunctionError)
......@@ -22,4 +22,12 @@ describe('unicloud-call-function', () => {
expect(callFunctionResult['event']['num']).toBe(1)
expect(callFunctionResult['event']['str']).toBe('ABC')
})
it('callFunctionWithGeneric', async () => {
await page.callMethod('callFunctionWithGeneric')
const {
genericDemoShowMessage,
} = await page.data()
expect(genericDemoShowMessage).toBe("Hello uniCloud function")
})
})
......@@ -7,6 +7,7 @@
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-btn-v uni-common-mt">
<button type="primary" @click="callFunction">请求云函数</button>
<button type="primary" @click="callFunctionWithGeneric">请求云函数传入泛型</button>
</view>
</view>
</view>
......@@ -22,6 +23,7 @@
title: '请求云函数',
callFunctionResult: {},
callFunctionError: {},
genericDemoShowMessage: '',
isUniTest: false
}
},
......@@ -44,6 +46,34 @@
console.log(title, content)
}
},
async callFunctionWithGeneric() : Promise<void> {
type EchoCfResult = {
showMessage: string
}
uni.showLoading({
title: '加载中...'
})
await uniCloud.callFunction<EchoCfResult>({
name: 'echo-cf',
data: {
num: 1,
str: 'ABC'
}
}).then(res => {
const result = res.result
uni.hideLoading()
this.genericDemoShowMessage = result.showMessage
this.notify(result.showMessage, '提示')
}).catch((err : any | null) => {
const error = err as UniCloudError
this.callFunctionError = {
errCode: error.errCode,
errMsg: error.errMsg
}
uni.hideLoading()
this.notify(error.errMsg, '错误')
})
},
async callFunction() : Promise<void> {
uni.showLoading({
title: '加载中...'
......
......@@ -11,6 +11,7 @@ describe('unicloud-import-object', () => {
})
it('importObject', async () => {
await page.callMethod('addTodo')
await page.callMethod('addTodoWithGeneric')
await page.callMethod('fail')
await page.callMethod('failWithNumberErrCode')
await page.callMethod('success')
......@@ -20,6 +21,8 @@ describe('unicloud-import-object', () => {
todoContent,
returnTodoTitle,
returnTodoContent,
genericDemoReturnTodoTitle,
genericDemoReturnTodoContent,
failErrCode,
failNumberErrCode,
successErrCode,
......@@ -27,6 +30,8 @@ describe('unicloud-import-object', () => {
expect(returnTodoTitle).toBe(todoTitle)
expect(returnTodoContent).toBe(todoContent)
expect(genericDemoReturnTodoTitle).toBe(todoTitle)
expect(genericDemoReturnTodoContent).toBe(todoContent)
expect(failErrCode).toBe('TEST_ERROR_CODE')
expect(failNumberErrCode).toBe(-1)
expect(successErrCode).toBe(0)
......
......@@ -8,6 +8,9 @@
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="addTodo">添加Todo</button>
</view>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="addTodoWithGeneric">添加Todo传入泛型</button>
</view>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="randomFail">随机触发失败重试</button>
</view>
......@@ -36,6 +39,8 @@
todoContent: '熟悉uts语法',
returnTodoTitle: '',
returnTodoContent: '',
genericDemoReturnTodoTitle: '',
genericDemoReturnTodoContent: '',
failErrCode: '',
failNumberErrCode: 0,
successErrCode: -1,
......@@ -60,11 +65,33 @@
})
const title = this.todoTitle
const content = this.todoContent
await todo.add(title, content).then((res : UTSJSONObject) => {
await todo.add<UTSJSONObject>(title, content).then((res : UTSJSONObject) => {
this.returnTodoTitle = res['title'] as string
this.returnTodoContent = res['content'] as string
this.notify(res['showMessage'] as string, '提示')
}).catch((err : any | null) => {
console.log(err)
const error = err as UniCloudError
console.error(error)
})
},
async addTodoWithGeneric() : Promise<void> {
type AddTodoResult = {
title: string,
content: string,
showMessage: string
}
const todo = uniCloud.importObject('todo', {
customUI: this.isUniTest
})
const title = this.todoTitle
const content = this.todoContent
await todo.add<AddTodoResult>(title, content).then((res : AddTodoResult) => {
this.genericDemoReturnTodoTitle = res.title
this.genericDemoReturnTodoContent = res.content
this.notify(res.showMessage, '提示')
}).catch((err : any | null) => {
console.log(err)
const error = err as UniCloudError
console.error(error)
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册