提交 93c2359c 编写于 作者: 雪洛's avatar 雪洛

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

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