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: [] }) 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, '请填写客户姓名') } run().catch(error => { console.error(error) process.exitCode = 1 })