Files

56 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

const test = require('node:test')
const assert = require('node:assert/strict')
const fs = require('node:fs')
const path = require('node:path')
const vm = require('node:vm')
const ts = require('typescript')
const root = path.resolve(__dirname, '..')
function harness(env = 'weapp', supported = true) {
const calls = [], notices = []
let outcome = () => Promise.resolve({}), resolve
const exports = {}
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(root, 'src/services/customerService.ts'), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2019 }
}).outputText, { exports, process: { env: { TARO_ENV: env } }, require: () => ({ default: {
canIUse: api => { assert.equal(api, 'openCustomerServiceChat'); return supported },
openCustomerServiceChat: options => { calls.push(JSON.parse(JSON.stringify(options))); return outcome() },
showToast: options => notices.push(options.title)
} }) })
return { api: exports, calls, notices,
fail: error => { outcome = () => Promise.reject(error) },
defer: () => { outcome = () => new Promise(ok => { resolve = ok }) },
complete: () => resolve({}) }
}
test('客服在点击的同一调用栈打开,企业ID与链接准确,不携带用户资料或秘密', async () => {
const h = harness()
const work = h.api.openWechatCustomerService()
assert.equal(h.calls.length, 1)
assert.deepEqual(h.calls[0], { corpId: 'wwf8bbe2cad2e9e84b', extInfo: { url: 'https://work.weixin.qq.com/kfid/kfc5a57c84cb0feb2fd' } })
await work
assert.equal(h.notices.length, 0)
})
test('H5与不支持的微信版本仅提示,不模拟客服接通', async () => {
const h5 = harness('h5'); await h5.api.openWechatCustomerService()
assert.equal(h5.calls.length, 0); assert.match(h5.notices[0], /微信小程序/)
const old = harness('weapp', false); await old.api.openWechatCustomerService()
assert.equal(old.calls.length, 0); assert.match(old.notices[0], /更新微信/)
})
test('连续点击不重复打开,完成或失败后可重试,取消无错误提示', async () => {
const h = harness(); h.defer()
const first = h.api.openWechatCustomerService()
await h.api.openWechatCustomerService(); assert.equal(h.calls.length, 1)
h.complete(); await first
h.fail({ errMsg: 'openCustomerServiceChat:fail not bound' })
await h.api.openWechatCustomerService(); assert.equal(h.calls.length, 2)
assert.match(h.notices[0], /客服打开失败/)
h.fail({ errMsg: 'openCustomerServiceChat:fail cancel' })
await h.api.openWechatCustomerService(); assert.equal(h.calls.length, 3)
assert.equal(h.notices.length, 1)
})
test('我的及绑定提示共用同一客服入口,不再显示待配置或跳模拟聊天页面', () => {
for (const file of ['src/pages/my/index.tsx', 'src/components/DeityBinding.tsx']) {
const source = fs.readFileSync(path.join(root, file), 'utf8')
assert.match(source, /onClick=\{openWechatCustomerService\}/)
assert.doesNotMatch(source, /企业微信入口待配置|openEnterpriseWechat|pages\/contact/)
}
})