68 lines
2.2 KiB
JavaScript
68 lines
2.2 KiB
JavaScript
const assert = require('assert')
|
|
const customersApi = require('../../api/customers')
|
|
|
|
let definition
|
|
global.Page = value => { definition = value }
|
|
global.wx = {
|
|
showToast: () => {},
|
|
navigateTo: () => {}
|
|
}
|
|
require('./index')
|
|
|
|
function createPage() {
|
|
return {
|
|
...definition,
|
|
data: JSON.parse(JSON.stringify(definition.data)),
|
|
setData(values) {
|
|
Object.entries(values).forEach(([key, value]) => {
|
|
const parts = key.split('.')
|
|
let target = this.data
|
|
for (let index = 0; index < parts.length - 1; index += 1) target = target[parts[index]]
|
|
target[parts[parts.length - 1]] = value
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
async function run() {
|
|
const page = createPage()
|
|
let submitted
|
|
let toast
|
|
global.wx.showToast = options => { toast = options.title }
|
|
customersApi.create = async value => { submitted = value }
|
|
customersApi.list = async () => ({ list: [] })
|
|
customersApi.matchExactIdentity = async () => []
|
|
|
|
page.setData({ 'createForm.nickname': '小周', 'createForm.wechatId': 'wx_zhou' })
|
|
await page.submitCustomer()
|
|
assert.deepStrictEqual(submitted, { nickname: '小周', wechatId: 'wx_zhou', phone: undefined })
|
|
|
|
submitted = undefined
|
|
page.setData({ 'createForm.nickname': '', 'createForm.phone': '' })
|
|
await page.submitCustomer()
|
|
assert.strictEqual(submitted, undefined)
|
|
assert.strictEqual(toast, '请填写客户姓名')
|
|
|
|
let lookedUp
|
|
customersApi.matchExactIdentity = async (...args) => {
|
|
lookedUp = args
|
|
return [{ id: '29', name: '小周', phone: '13800138000' }]
|
|
}
|
|
const duplicatePage = {
|
|
...definition,
|
|
data: JSON.parse(JSON.stringify(definition.data)),
|
|
setData(values) { Object.assign(this.data, values) }
|
|
}
|
|
duplicatePage.data.createForm = { nickname: '小周', wechatId: 'wx-zhou', phone: '13800138000' }
|
|
await definition.submitCustomer.call(duplicatePage)
|
|
assert.deepStrictEqual(lookedUp, ['小周', 'wx-zhou', '13800138000'])
|
|
assert.strictEqual(duplicatePage.data.duplicateModalVisible, true)
|
|
assert.deepStrictEqual(duplicatePage.data.duplicateCustomers, [{ id: '29', name: '小周', phone: '13800138000' }])
|
|
assert.strictEqual(submitted, undefined)
|
|
}
|
|
|
|
run().catch(error => {
|
|
console.error(error)
|
|
process.exitCode = 1
|
|
})
|