feat: refine profile and home UI with dynamic fortune rules
This commit is contained in:
1 parent
869376979c
commit
4608dbd5cf
59 files changed
+3424
-1347
No files matched your search
@@ -0,0 +1,51 @@
|
||||
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')
|
||||
|
||||
function loadApi(env, search) {
|
||||
let requests = 0
|
||||
let uploads = 0
|
||||
const taro = { default: {
|
||||
getStorageSync: () => '',
|
||||
request: async () => { requests++; return { data: { code: 0, data: { id: 9 } } } },
|
||||
uploadFile: async () => { uploads++; return { data: JSON.stringify({ code: 0, data: { id: 9 } }) } }
|
||||
} }
|
||||
const cache = {}
|
||||
function load(name) {
|
||||
if (cache[name]) return cache[name]
|
||||
const source = fs.readFileSync(path.resolve(__dirname, `../src/services/${name}.ts`), 'utf8')
|
||||
const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2017 } }).outputText
|
||||
const exports = {}
|
||||
cache[name] = exports
|
||||
vm.runInNewContext(code, {
|
||||
exports, process: { env: { TARO_ENV: env } }, window: { location: { search } }, URLSearchParams,
|
||||
require: (id) => id === '@tarojs/taro' ? taro : load('frontendPreview')
|
||||
})
|
||||
return exports
|
||||
}
|
||||
return { api: load('request').api, counts: () => ({ requests, uploads }) }
|
||||
}
|
||||
|
||||
test('显式H5预览使用示例资料,阻止后端读取、保存和上传', async () => {
|
||||
const { api, counts } = loadApi('h5', '?preview=1')
|
||||
const profile = await api.profile()
|
||||
assert.equal(profile.id, 0)
|
||||
assert.equal(profile.birthday, '1998-06-18')
|
||||
await assert.rejects(api.updateProfile({ nickname: 'Changed' }), /纯前端预览/)
|
||||
await assert.rejects(api.uploadAvatar('local.png'), /纯前端预览/)
|
||||
await assert.rejects(api.agreement('privacy'), /纯前端预览/)
|
||||
assert.deepEqual(counts(), { requests: 0, uploads: 0 })
|
||||
profile.nickname = 'Mutated'
|
||||
assert.equal((await api.profile()).nickname, 'You')
|
||||
})
|
||||
|
||||
test('普通H5和微信端不使用预览资料', async () => {
|
||||
for (const [env, search] of [['h5', ''], ['h5', '?preview=0'], ['weapp', '?preview=1']]) {
|
||||
const { api, counts } = loadApi(env, search)
|
||||
assert.equal((await api.profile()).id, 9)
|
||||
assert.deepEqual(counts(), { requests: 1, uploads: 0 })
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user