提交 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 @@ ...@@ -18,7 +18,9 @@
export default { export default {
data() { data() {
return { return {
title: '请求云函数' title: '请求云函数',
callFunctionResult: {},
callFunctionError: {}
} }
}, },
onLoad() { onLoad() {
...@@ -38,6 +40,7 @@ ...@@ -38,6 +40,7 @@
} }
}).then<void>(res => { }).then<void>(res => {
const result = res.result const result = res.result
this.callFunctionResult = result
console.log(JSON.stringify(result)) console.log(JSON.stringify(result))
uni.hideLoading() uni.hideLoading()
uni.showModal({ uni.showModal({
...@@ -47,6 +50,10 @@ ...@@ -47,6 +50,10 @@
}) })
}).catch<void>((err: any | null) => { }).catch<void>((err: any | null) => {
const error = err as UniCloudError const error = err as UniCloudError
this.callFunctionError = {
errCode: error.errCode,
errMsg: error.errMsg
}
uni.hideLoading() uni.hideLoading()
uni.showModal({ uni.showModal({
title: '错误', 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 @@ ...@@ -25,7 +25,15 @@
export default { export default {
data() { data() {
return { 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() { onLoad() {
...@@ -48,6 +56,7 @@ ...@@ -48,6 +56,7 @@
.then<void>(res => { .then<void>(res => {
uni.hideLoading() uni.hideLoading()
console.log(res) console.log(res)
this.addId = res.id
uni.showModal({ uni.showModal({
content: `新增成功,id: ${res.id}`, content: `新增成功,id: ${res.id}`,
showCancel: false showCancel: false
...@@ -79,6 +88,8 @@ ...@@ -79,6 +88,8 @@
.then<void>((res) => { .then<void>((res) => {
uni.hideLoading() uni.hideLoading()
console.log(res) console.log(res)
this.batchAddIds = res.ids
this.batchAddinserted = res.inserted
uni.showModal({ uni.showModal({
content: `新增成功,id列表: ${res.ids.join(',')}`, content: `新增成功,id列表: ${res.ids.join(',')}`,
showCancel: false showCancel: false
...@@ -94,7 +105,7 @@ ...@@ -94,7 +105,7 @@
}) })
}) })
}, },
dbUpdate() { dbGet() {
uni.showLoading({ uni.showLoading({
title: '加载中...' title: '加载中...'
}) })
...@@ -103,14 +114,17 @@ ...@@ -103,14 +114,17 @@
.where( .where(
'tag == "default-tag"' 'tag == "default-tag"'
) )
.update({ .field('num, tag')
num: 4 .orderBy('num desc')
}) .skip(1)
.limit(2)
.get()
.then<void>(res => { .then<void>(res => {
uni.hideLoading() uni.hideLoading()
console.log(res) console.log(res)
this.getData = res.data
uni.showModal({ uni.showModal({
content: `更新成功,更新了${res.updated}条数据`, content: `获取成功,取到了${res.data.length}条数据`,
showCancel: false showCancel: false
}) })
}) })
...@@ -124,15 +138,15 @@ ...@@ -124,15 +138,15 @@
}) })
}) })
}, },
dbGet() { dbGetWithCommand() {
uni.showLoading({ uni.showLoading({
title: '加载中...' title: '加载中...'
}) })
const db = uniCloud.databaseForJQL() const db = uniCloud.databaseForJQL()
db.collection('type') db.collection('type')
.where( .where({
'tag == "default-tag"' num: db.command.gt(1)
) })
.field('num, tag') .field('num, tag')
.orderBy('num desc') .orderBy('num desc')
.skip(1) .skip(1)
...@@ -141,6 +155,7 @@ ...@@ -141,6 +155,7 @@
.then<void>(res => { .then<void>(res => {
uni.hideLoading() uni.hideLoading()
console.log(res) console.log(res)
this.getWithCommandData = res.data
uni.showModal({ uni.showModal({
content: `获取成功,取到了${res.data.length}条数据`, content: `获取成功,取到了${res.data.length}条数据`,
showCancel: false showCancel: false
...@@ -156,25 +171,24 @@ ...@@ -156,25 +171,24 @@
}) })
}) })
}, },
dbGetWithCommand() { dbUpdate() {
uni.showLoading({ uni.showLoading({
title: '加载中...' title: '加载中...'
}) })
const db = uniCloud.databaseForJQL() const db = uniCloud.databaseForJQL()
db.collection('type') db.collection('type')
.where({ .where(
num: db.command.gt(2) 'tag == "default-tag"'
)
.update({
num: 4
}) })
.field('num, tag')
.orderBy('num desc')
.skip(1)
.limit(2)
.get()
.then<void>(res => { .then<void>(res => {
uni.hideLoading() uni.hideLoading()
console.log(res) console.log(res)
this.updateUpdated = res.updated
uni.showModal({ uni.showModal({
content: `获取成功,取到了${res.data.length}条数据`, content: `更新成功,更新了${res.updated}条数据`,
showCancel: false showCancel: false
}) })
}) })
...@@ -201,6 +215,7 @@ ...@@ -201,6 +215,7 @@
.then<void>(res => { .then<void>(res => {
uni.hideLoading() uni.hideLoading()
console.log(res) console.log(res)
this.removeDeleted = res.deleted
uni.showModal({ uni.showModal({
content: `删除成功,删掉了${res.deleted}条数据`, content: `删除成功,删掉了${res.deleted}条数据`,
showCancel: false showCancel: false
...@@ -289,6 +304,7 @@ ...@@ -289,6 +304,7 @@
.then<void>(res => { .then<void>(res => {
uni.hideLoading() uni.hideLoading()
console.log(res) console.log(res)
this.lookupData = res.data
uni.showModal({ uni.showModal({
content: `联表查询成功,取到了${res.data.length}条数据`, content: `联表查询成功,取到了${res.data.length}条数据`,
showCancel: false 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
...@@ -28,15 +28,23 @@ ...@@ -28,15 +28,23 @@
export default { export default {
data() { data() {
return { return {
title: '请求云对象' title: '请求云对象',
todoTitle: '学习编程',
todoContent: '熟悉uts语法',
returnTodoTitle: '',
returnTodoContent: '',
failErrCode: '',
successErrCode: -1
} }
}, },
methods: { methods: {
addTodo() { addTodo() {
const todo = uniCloud.importObject('todo') const todo = uniCloud.importObject('todo')
const title = '学习编程' const title = this.todoTitle
const content = '熟悉uts语法' const content = this.todoContent
todo.add(title, content).then<void>((res: UTSJSONObject) : void => { todo.add(title, content).then<void>((res : UTSJSONObject) : void => {
this.returnTodoTitle = res['title'] as string
this.returnTodoContent = res['content'] as string
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: res['showMessage'] as string, content: res['showMessage'] as string,
...@@ -53,7 +61,7 @@ ...@@ -53,7 +61,7 @@
retry: true retry: true
} }
}) })
todoObj.randomFail().then<void>((res: UTSJSONObject) : void => { todoObj.randomFail().then<void>((res : UTSJSONObject) : void => {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: res['showMessage'] as string, content: res['showMessage'] as string,
...@@ -66,7 +74,7 @@ ...@@ -66,7 +74,7 @@
}, },
fail() { fail() {
const todo = uniCloud.importObject('todo') const todo = uniCloud.importObject('todo')
todo.fail().then<void>((res: UTSJSONObject) : void => { todo.fail().then<void>((res : UTSJSONObject) : void => {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: 'todo.fail应调用失败,此处错误的触发了成功回调', content: 'todo.fail应调用失败,此处错误的触发了成功回调',
...@@ -75,12 +83,14 @@ ...@@ -75,12 +83,14 @@
console.log('todo.fail: ', res); console.log('todo.fail: ', res);
}).catch<void>((err : any | null) : void => { }).catch<void>((err : any | null) : void => {
const error = err as UniCloudError const error = err as UniCloudError
this.failErrCode = error.errCode as string
console.error(error) console.error(error)
}) })
}, },
success() { success() {
const todo = uniCloud.importObject('todo') 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({ uni.showModal({
title: '提示', title: '提示',
content: res['showMessage'] as string, content: res['showMessage'] as string,
......
...@@ -4,6 +4,8 @@ module.exports = { ...@@ -4,6 +4,8 @@ module.exports = {
async add(title, content) { async add(title, content) {
console.log(title, content) console.log(title, content)
return { return {
title,
content,
showMessage: `Todo added, title: ${title}, content: ${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.
先完成此消息的编辑!
想要评论请 注册