const assert = require('assert') const employeesApi = require('../../api/employees') let navigatedBack = false let navigatedTo = '' let modalConfirm = false global.wx = { getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }), getDeviceInfo: () => ({ platform: 'devtools' }), showToast: () => {}, showModal: options => options.success({ confirm: modalConfirm }), navigateBack: () => { navigatedBack = true }, navigateTo: options => { navigatedTo = options.url } } global.setTimeout = callback => { callback(); return 1 } let definition global.Page = value => { definition = value } require('./index') let emitted const page = { ...definition, data: JSON.parse(JSON.stringify(definition.data)), getOpenerEventChannel: () => ({ emit: (name, value) => { emitted = { name, value } } }), 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() { page.onInput({ currentTarget: { dataset: { field: 'phone' } }, detail: { value: '13800138000' } }) assert.strictEqual(page.data.form.phone, '13800138000') assert.strictEqual(page.data.form.account, undefined) assert.strictEqual(page.data.form.password, undefined) assert.strictEqual(page.data.form.color, '#8FB89A') Object.assign(page.data.form, { nickname: '小周', phone: '13800138000', wechatId: '' }) let createPayload employeesApi.createUser = async payload => { createPayload = payload assert.strictEqual(payload.wechatId, '') assert.strictEqual(payload.color, '#8FB89A') return 88 } employeesApi.unassociatedApplications = async () => [] await page.submit() assert.deepStrictEqual(createPayload, { nickname: '小周', phone: '13800138000', contactPhone: '13800138000', wechatId: '', color: '#8FB89A', avatar: undefined, type: 3 }) assert.strictEqual(page.data.onboardingVisible, true) assert.strictEqual(emitted, undefined) let schedulePayload page.data.pickerMode = true employeesApi.saveScheduleRule = async payload => { schedulePayload = payload } page.skipFirstSchedule() await new Promise(resolve => setImmediate(resolve)) assert.deepStrictEqual(schedulePayload.workDays, [1, 2, 3, 4, 5, 6, 7]) assert.strictEqual(schedulePayload.startTime, '00:00') assert.strictEqual(schedulePayload.endTime, '23:59') assert.strictEqual(emitted.name, 'employeeCreated') assert.deepStrictEqual(emitted.value, { id: 88, name: '小周', title: '小周', avatar: '', autoSelect: true, scheduleSaved: true }) assert.strictEqual(navigatedBack, true) let associatedApplication employeesApi.unassociatedApplications = async () => [{ id: 42, applicantNickname: '微信昵称', applicantMaskedPhone: '138****8000', applicationNo: 'SALON-042' }] employeesApi.preAssociateApplication = async (applicationId, employeeId) => { associatedApplication = { applicationId, employeeId } } page.setData({ onboardingVisible: false, associationSheetVisible: false }) await page.offerPendingAssociation() assert.strictEqual(page.data.associationSheetVisible, true) assert.strictEqual(page.data.associationItems[0].title, '微信昵称') await page.selectAssociation({ detail: { item: { id: 42 } } }) assert.deepStrictEqual(associatedApplication, { applicationId: 42, employeeId: 88 }) assert.strictEqual(page.data.associationSheetVisible, false) assert.strictEqual(page.data.onboardingVisible, true) page.data.onboardingVisible = true page.data.createdEmployee = { id: 88, name: '小周' } page.data.appointmentContext = { date: '2026-10-12', time: '14:00', durationMinutes: '60' } page.setupFirstSchedule() assert.strictEqual(navigatedTo.includes('/pages/schedule-setting/index?id=88&onboarding=1&source=appointment'), true) let employeeCreateCount = 0 let approvalAttempts = 0 let approvalPayload page.data.form = { nickname: '申请档案员工', phone: '13800138001', wechatId: '', color: '#8FB89A', avatar: '', avatarUrl: '' } page.data.createdEmployee = null page.data.applicationId = '42' page.data.applicationApproved = false page.data.onboardingVisible = false employeesApi.createUser = async () => { employeeCreateCount += 1; return { userId: 90 } } employeesApi.approveApplication = async (applicationId, payload) => { approvalAttempts += 1 approvalPayload = { applicationId, ...payload } if (approvalAttempts === 1) throw new Error('暂时无法审核') } await page.submit() assert.strictEqual(employeeCreateCount, 1) assert.deepStrictEqual(approvalPayload, { applicationId: '42', targetArtistUserId: 90, useApplicantNickname: false, useApplicantAvatar: false }) assert.strictEqual(page.data.createdEmployee.id, 90) assert.strictEqual(page.data.onboardingVisible, true) await page.associateCreatedEmployee() assert.strictEqual(employeeCreateCount, 1) assert.strictEqual(approvalAttempts, 2) assert.strictEqual(page.data.applicationApproved, true) } run().catch(error => { console.error(error) process.exitCode = 1 })