提交 9cc987b8 编写于 作者: 雪洛's avatar 雪洛

feat: uniCloud auto test

上级 a4208d70
const PAGE_PATH = '/pages/API/unicloud-call-function/unicloud-call-function'
describe('unicloud-call-function', () => {
let page
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor(500)
})
it('callFunction', async () => {
await page.callMethod('callFunction')
await page.waitFor(3000)
const {
callFunctionResult,
callFunctionError
} = await page.data()
console.error(callFunctionResult)
console.error(callFunctionError)
expect(callFunctionResult['showMessage']).toBe("Hello uniCloud function")
expect(callFunctionResult['event']['num']).toBe(1)
expect(callFunctionResult['event']['str']).toBe('ABC')
})
})
......@@ -18,7 +18,9 @@
export default {
data() {
return {
title: '请求云函数'
title: '请求云函数',
callFunctionResult: {},
callFunctionError: {}
}
},
onLoad() {
......@@ -38,6 +40,7 @@
}
}).then<void>(res => {
const result = res.result
this.callFunctionResult = result
console.log(JSON.stringify(result))
uni.hideLoading()
uni.showModal({
......@@ -47,6 +50,10 @@
})
}).catch<void>((err: any | null) => {
const error = err as UniCloudError
this.callFunctionError = {
errCode: error.errCode,
errMsg: error.errMsg
}
uni.hideLoading()
uni.showModal({
title: '错误',
......
const PAGE_PATH = '/pages/API/unicloud-database/unicloud-database'
describe('unicloud-database', () => {
let page
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor(500)
})
it('databaseBasic', async () => {
await page.callMethod('dbRemove')
await page.waitFor(3000)
await page.callMethod('dbAdd')
await page.waitFor(1000)
await page.callMethod('dbBatchAdd')
await page.waitFor(1000)
await page.callMethod('dbGet')
await page.waitFor(1000)
await page.callMethod('dbGetWithCommand')
await page.waitFor(1000)
await page.callMethod('dbUpdate')
await page.waitFor(1000)
await page.callMethod('dbRemove')
await page.waitFor(1000)
const {
addId,
batchAddIds,
batchAddinserted,
updateUpdated,
getData,
getWithCommandData,
removeDeleted,
} = await page.data()
expect(addId.length > 0).toBe(true)
expect(batchAddIds.length).toBe(2)
expect(batchAddinserted).toBe(2)
expect(getData.length).toBe(2)
expect(getWithCommandData.length).toBe(2)
expect(updateUpdated).toBe(3)
expect(removeDeleted).toBe(3)
})
it('databaseLookup', async () => {
await page.callMethod('dbLookupInit')
await page.waitFor(1000)
await page.callMethod('dbLookup')
await page.waitFor(1000)
const {
lookupData
} = await page.data()
expect(lookupData.length).toBe(2)
expect(lookupData[0]['foreign_id'].length).toBe(1)
expect(lookupData[1]['foreign_id'].length).toBe(1)
})
});
\ No newline at end of file
......@@ -25,7 +25,15 @@
export default {
data() {
return {
title: 'ClientDB'
title: 'ClientDB',
addId: '',
batchAddIds: [] as Array<string>,
batchAddinserted: 0,
updateUpdated: 0,
getData: [] as Array<UTSJSONObject>,
getWithCommandData: [] as Array<UTSJSONObject>,
removeDeleted: 0,
lookupData: [] as Array<UTSJSONObject>,
}
},
onLoad() {
......@@ -48,6 +56,7 @@
.then<void>(res => {
uni.hideLoading()
console.log(res)
this.addId = res.id
uni.showModal({
content: `新增成功,id: ${res.id}`,
showCancel: false
......@@ -79,6 +88,8 @@
.then<void>((res) => {
uni.hideLoading()
console.log(res)
this.batchAddIds = res.ids
this.batchAddinserted = res.inserted
uni.showModal({
content: `新增成功,id列表: ${res.ids.join(',')}`,
showCancel: false
......@@ -94,7 +105,7 @@
})
})
},
dbUpdate() {
dbGet() {
uni.showLoading({
title: '加载中...'
})
......@@ -103,14 +114,17 @@
.where(
'tag == "default-tag"'
)
.update({
num: 4
})
.field('num, tag')
.orderBy('num desc')
.skip(1)
.limit(2)
.get()
.then<void>(res => {
uni.hideLoading()
console.log(res)
this.getData = res.data
uni.showModal({
content: `更新成功,更新了${res.updated}条数据`,
content: `获取成功,取到了${res.data.length}条数据`,
showCancel: false
})
})
......@@ -124,15 +138,15 @@
})
})
},
dbGet() {
dbGetWithCommand() {
uni.showLoading({
title: '加载中...'
})
const db = uniCloud.databaseForJQL()
db.collection('type')
.where(
'tag == "default-tag"'
)
.where({
num: db.command.gt(1)
})
.field('num, tag')
.orderBy('num desc')
.skip(1)
......@@ -141,6 +155,7 @@
.then<void>(res => {
uni.hideLoading()
console.log(res)
this.getWithCommandData = res.data
uni.showModal({
content: `获取成功,取到了${res.data.length}条数据`,
showCancel: false
......@@ -156,25 +171,24 @@
})
})
},
dbGetWithCommand() {
dbUpdate() {
uni.showLoading({
title: '加载中...'
})
const db = uniCloud.databaseForJQL()
db.collection('type')
.where({
num: db.command.gt(2)
.where(
'tag == "default-tag"'
)
.update({
num: 4
})
.field('num, tag')
.orderBy('num desc')
.skip(1)
.limit(2)
.get()
.then<void>(res => {
uni.hideLoading()
console.log(res)
this.updateUpdated = res.updated
uni.showModal({
content: `获取成功,取到了${res.data.length}条数据`,
content: `更新成功,更新了${res.updated}条数据`,
showCancel: false
})
})
......@@ -201,6 +215,7 @@
.then<void>(res => {
uni.hideLoading()
console.log(res)
this.removeDeleted = res.deleted
uni.showModal({
content: `删除成功,删掉了${res.deleted}条数据`,
showCancel: false
......@@ -289,6 +304,7 @@
.then<void>(res => {
uni.hideLoading()
console.log(res)
this.lookupData = res.data
uni.showModal({
content: `联表查询成功,取到了${res.data.length}条数据`,
showCancel: false
......
const PAGE_PATH = '/pages/API/unicloud-import-object/unicloud-import-object'
describe('unicloud-import-object', () => {
let page
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor(500)
})
it('importObject', async () => {
await page.callMethod('addTodo')
await page.callMethod('fail')
await page.callMethod('success')
await page.waitFor(3000)
const {
todoTitle,
todoContent,
returnTodoTitle,
returnTodoContent,
failErrCode,
successErrCode,
} = await page.data()
expect(returnTodoTitle).toBe(todoTitle)
expect(returnTodoContent).toBe(todoContent)
expect(failErrCode).toBe('TEST_ERROR_CODE')
expect(successErrCode).toBe(0)
})
});
\ No newline at end of file
<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1">
<!-- #endif -->
<!-- #endif -->
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
......@@ -19,7 +19,7 @@
</view>
</view>
</view>
<!-- #ifdef APP -->
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
......@@ -28,15 +28,23 @@
export default {
data() {
return {
title: '请求云对象'
title: '请求云对象',
todoTitle: '学习编程',
todoContent: '熟悉uts语法',
returnTodoTitle: '',
returnTodoContent: '',
failErrCode: '',
successErrCode: -1
}
},
methods: {
addTodo() {
const todo = uniCloud.importObject('todo')
const title = '学习编程'
const content = '熟悉uts语法'
todo.add(title, content).then<void>((res: UTSJSONObject) : void => {
const title = this.todoTitle
const content = this.todoContent
todo.add(title, content).then<void>((res : UTSJSONObject) : void => {
this.returnTodoTitle = res['title'] as string
this.returnTodoContent = res['content'] as string
uni.showModal({
title: '提示',
content: res['showMessage'] as string,
......@@ -53,7 +61,7 @@
retry: true
}
})
todoObj.randomFail().then<void>((res: UTSJSONObject) : void => {
todoObj.randomFail().then<void>((res : UTSJSONObject) : void => {
uni.showModal({
title: '提示',
content: res['showMessage'] as string,
......@@ -66,7 +74,7 @@
},
fail() {
const todo = uniCloud.importObject('todo')
todo.fail().then<void>((res: UTSJSONObject) : void => {
todo.fail().then<void>((res : UTSJSONObject) : void => {
uni.showModal({
title: '提示',
content: 'todo.fail应调用失败,此处错误的触发了成功回调',
......@@ -75,12 +83,14 @@
console.log('todo.fail: ', res);
}).catch<void>((err : any | null) : void => {
const error = err as UniCloudError
this.failErrCode = error.errCode as string
console.error(error)
})
},
success() {
const todo = uniCloud.importObject('todo')
todo.success().then<void>((res: UTSJSONObject) : void => {
todo.success().then<void>((res : UTSJSONObject) : void => {
this.successErrCode = res['errCode'] as number
uni.showModal({
title: '提示',
content: res['showMessage'] as string,
......@@ -97,4 +107,4 @@
<style>
</style>
</style>
\ No newline at end of file
......@@ -4,6 +4,8 @@ module.exports = {
async add(title, content) {
console.log(title, content)
return {
title,
content,
showMessage: `Todo added, title: ${title}, content: ${content}`
}
},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册