join-form.required-info.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * 契约级单测(TDD-RED)——能力键 family.invite.joinRequiredInfoFrontend
  3. *
  4. * 溯源:
  5. * requirement: docs/superpowers/specs/2026-11-07-family-join-required-info.md
  6. * §3 AC-R1/AC-R3(前端可见且必填、校验规则)、§4.2(前端表单)、§5 WI-1(前端表单补充出生日期与性别必填项)
  7. * design: cfc-frontend/pages/invite/join-form.vue + cfc-frontend/utils/api.js `updateUserInfo`
  8. *
  9. * 说明(既有 Jest 体系约束):
  10. * cfc-frontend 的 jest.config.js 仅 transform `.js`(babel-jest),moduleFileExtensions 不含 `vue`,
  11. * 且未安装 vue-jest/jsdom 组件环境 —— 既有约定为「抽取被测逻辑断言」而非挂载 .vue
  12. * (见 __tests__/components/FamilyRelationGraph.test.js,导入 shallowMount 但实际只测抽取函数)。
  13. * 本文件沿用该约定,但比「测试内复刻逻辑」更强:直接加载真实 SFC 的 <script>/<template> 源码——
  14. * - <script>:把 import 语句替换为注入桩后求值,得到真实组件的 data()/methods,做行为级断言;
  15. * - <template>:读取模板文本,做结构契约断言(必填标记、picker 绑定、change 处理器)。
  16. * 若实现方在后续 GREEN 阶段对 join-form.vue 补足契约,本文件断言即翻绿;当前实现缺失 → 预期 RED。
  17. * 本文件不修改 pages/、utils/ 任何业务代码。
  18. */
  19. import fs from 'fs'
  20. import path from 'path'
  21. import { updateUserInfo as apiUpdateUserInfo, clearApiDedup } from '@/utils/api'
  22. const JOIN_FORM_PATH = path.join(__dirname, '../../pages/invite/join-form.vue')
  23. // ---------------------------------------------------------------------------
  24. // 工具:加载真实 join-form.vue 的 <script> 逻辑(行为级)与 <template> 结构(结构级)
  25. // ---------------------------------------------------------------------------
  26. /** 把 ES import 语句替换为从 globalThis.__joinFormStubs 取桩的声明(不解析模块,避免污染业务实现) */
  27. function stubImports(script, stubMap) {
  28. return script.replace(/import\s+([^;]+?)\s+from\s+['"]([^'"]+)['"];?/g, function (m, clause, id) {
  29. var names = []
  30. var c = clause.trim()
  31. var braceIdx = c.indexOf('{')
  32. if (braceIdx >= 0) {
  33. var def = c.slice(0, braceIdx).replace(/,\s*$/, '').trim()
  34. if (def) names.push(def)
  35. var named = c.slice(braceIdx + 1, c.lastIndexOf('}'))
  36. named.split(',').forEach(function (part) {
  37. var p = part.trim()
  38. if (!p) return
  39. var parts = p.split(/\s+as\s+/)
  40. var local = parts.length > 1 ? parts[1].trim() : parts[0].trim()
  41. if (local) names.push(local)
  42. })
  43. } else if (c.indexOf('*') === 0) {
  44. var nsMatch = c.match(/\*\s+as\s+(\w+)/)
  45. if (nsMatch) names.push(nsMatch[1])
  46. } else {
  47. names.push(c)
  48. }
  49. return names.map(function (n) {
  50. return 'var ' + n + ' = (globalThis.__joinFormStubs[' + JSON.stringify(id) + '] || {})[' + JSON.stringify(n) + '];'
  51. }).join('\n')
  52. })
  53. }
  54. /** 加载真实 SFC:返回 { component, template }(component = 真实 <script> 的 export default 对象) */
  55. function loadJoinForm() {
  56. var src = fs.readFileSync(JOIN_FORM_PATH, 'utf8')
  57. var tmplMatch = src.match(/<template>([\s\S]*?)<\/template>/)
  58. var scriptMatch = src.match(/<script>([\s\S]*?)<\/script>/)
  59. if (!scriptMatch) throw new Error('无法在 join-form.vue 中定位 <script> 块')
  60. var script = stubImports(scriptMatch[1], globalThis.__joinFormStubs || {})
  61. script = script.replace(/export default\s*\{/, 'module.exports = {')
  62. var mod = { exports: {} }
  63. // eslint-disable-next-line no-new-func
  64. var fn = new Function('module', 'exports', script)
  65. fn(mod, mod.exports)
  66. return { component: mod.exports, template: tmplMatch ? tmplMatch[1] : '' }
  67. }
  68. /** 依据真实组件的 data()/methods 构造可驱动上下文(模拟 Vue 把 data 与 methods 合并到实例) */
  69. function instantiate(component) {
  70. var vm = {}
  71. var data = typeof component.data === 'function' ? component.data.call(vm) : (component.data || {})
  72. Object.keys(data).forEach(function (k) { vm[k] = data[k] })
  73. var methods = component.methods || {}
  74. Object.keys(methods).forEach(function (k) { vm[k] = methods[k] })
  75. return vm
  76. }
  77. // ---------------------------------------------------------------------------
  78. // 测试夹具
  79. // ---------------------------------------------------------------------------
  80. var stubs
  81. beforeEach(function () {
  82. global.uni._storage = {}
  83. global.uni.showToast.mockReset()
  84. global.uni.reLaunch.mockReset()
  85. global.uni.request.mockReset()
  86. stubs = {
  87. updateUserInfo: jest.fn(function () { return Promise.resolve({ code: 0, message: 'ok', data: null }) }),
  88. requestJoinByCode: jest.fn(function () { return Promise.resolve({ code: 0, message: 'ok', data: null }) }),
  89. pickImage: jest.fn(),
  90. fontScaleMixin: { data: function () { return { fontScale: 1 } }, onShow: function () {} }
  91. }
  92. globalThis.__joinFormStubs = {
  93. '@/utils/api.js': { updateUserInfo: stubs.updateUserInfo, requestJoinByCode: stubs.requestJoinByCode },
  94. '@/utils/image-picker.js': { pickImage: stubs.pickImage },
  95. '@/mixins/font-scale.js': { fontScaleMixin: stubs.fontScaleMixin }
  96. }
  97. jest.useFakeTimers()
  98. })
  99. afterEach(function () {
  100. jest.useRealTimers()
  101. delete globalThis.__joinFormStubs
  102. })
  103. // ---------------------------------------------------------------------------
  104. // AC1.1 表单结构:真实姓名 + 出生日期 + 性别三项,出生日期/性别带 * 必填标记
  105. // ---------------------------------------------------------------------------
  106. describe('AC1.1 表单结构:出生日期/性别必填项可见', function () {
  107. var template
  108. beforeAll(function () {
  109. // beforeAll 先于 beforeEach 执行:仅做模板结构断言,无需桩函数,置空即可(loadJoinForm 对桩做可选访问)
  110. globalThis.__joinFormStubs = {}
  111. template = loadJoinForm().template
  112. })
  113. it('真实姓名字段保留(无回归)', function () {
  114. expect(template).toMatch(/真实姓名/)
  115. expect(template).toMatch(/form\.realName/)
  116. })
  117. it('出生日期为必填(label 带 * 标记)', function () {
  118. expect(template).toMatch(/出生日期[\s\S]{0,120}<text class="required">\*<\/text>/)
  119. })
  120. it('性别为必填(label 带 * 标记)', function () {
  121. expect(template).toMatch(/性别[\s\S]{0,120}<text class="required">\*<\/text>/)
  122. })
  123. it('出生日期使用 uni date picker 绑定 form.birthday', function () {
  124. expect(template).toMatch(/<picker[^>]*mode="date"[^>]*form\.birthday/)
  125. expect(template).toMatch(/onBirthdayChange/)
  126. })
  127. it('性别使用 picker(男/女 两档)绑定 form.gender,change 映射 male/female', function () {
  128. expect(template).toMatch(/onGenderChange/)
  129. expect(template).toMatch(/男/)
  130. expect(template).toMatch(/女/)
  131. })
  132. })
  133. // ---------------------------------------------------------------------------
  134. // AC1.4 进入页面时预填:userInfo 已有 realName/birthday/gender 则带出
  135. // ---------------------------------------------------------------------------
  136. describe('AC1.4 prefill 预填已存资料', function () {
  137. var component
  138. beforeEach(function () {
  139. component = loadJoinForm().component
  140. })
  141. it('预填真实姓名(既有行为,无回归)', function () {
  142. global.uni.setStorageSync('userInfo', { realName: '张三' })
  143. var vm = instantiate(component)
  144. vm.prefill()
  145. expect(vm.form.realName).toBe('张三')
  146. })
  147. it('预填出生日期与性别(userInfo.birthday/gender 带出)', function () {
  148. global.uni.setStorageSync('userInfo', { realName: '张三', birthday: '1990-05-20', gender: 'male' })
  149. var vm = instantiate(component)
  150. vm.prefill()
  151. expect(vm.form.birthday).toBe('1990-05-20')
  152. expect(vm.form.gender).toBe('male')
  153. })
  154. it('userInfo 缺失三项时不写入(保持空串)', function () {
  155. global.uni.setStorageSync('userInfo', {})
  156. var vm = instantiate(component)
  157. vm.prefill()
  158. expect(vm.form.birthday).toBe('')
  159. expect(vm.form.gender).toBe('')
  160. })
  161. })
  162. // ---------------------------------------------------------------------------
  163. // AC1.2 提交拦截:出生日期/性别/姓名任一缺失或格式非法 → toast + 不调用 requestJoinByCode
  164. // ---------------------------------------------------------------------------
  165. describe('AC1.2 提交拦截(正常/边界)', function () {
  166. var vm
  167. beforeEach(function () {
  168. var component = loadJoinForm().component
  169. vm = instantiate(component)
  170. vm.familyInviteCode = 'CODE-123'
  171. vm.form.realName = '张三'
  172. vm.form.birthday = '1990-05-20'
  173. vm.form.gender = 'male'
  174. })
  175. it('真实姓名为空 → 拦截,不调用 requestJoinByCode(既有行为)', async function () {
  176. vm.form.realName = ' '
  177. await vm.submit()
  178. expect(global.uni.showToast).toHaveBeenCalled()
  179. expect(stubs.requestJoinByCode).not.toHaveBeenCalled()
  180. })
  181. it('出生日期未选 → 拦截并 toast 提示,不调用 requestJoinByCode', async function () {
  182. vm.form.birthday = ''
  183. await vm.submit()
  184. expect(stubs.requestJoinByCode).not.toHaveBeenCalled()
  185. expect(global.uni.showToast).toHaveBeenCalled()
  186. })
  187. it('性别未选 → 拦截并 toast 提示,不调用 requestJoinByCode', async function () {
  188. vm.form.gender = ''
  189. await vm.submit()
  190. expect(stubs.requestJoinByCode).not.toHaveBeenCalled()
  191. expect(global.uni.showToast).toHaveBeenCalled()
  192. })
  193. it('出生日期格式非法(非 yyyy-MM-dd)→ 拦截,不调用 requestJoinByCode', async function () {
  194. vm.form.birthday = '1990/05/20'
  195. await vm.submit()
  196. expect(stubs.requestJoinByCode).not.toHaveBeenCalled()
  197. expect(global.uni.showToast).toHaveBeenCalled()
  198. })
  199. it('缺少家庭邀请码 → 拦截(既有行为)', async function () {
  200. vm.familyInviteCode = ''
  201. await vm.submit()
  202. expect(stubs.requestJoinByCode).not.toHaveBeenCalled()
  203. expect(global.uni.showToast).toHaveBeenCalled()
  204. })
  205. })
  206. // ---------------------------------------------------------------------------
  207. // AC1.3 提交载荷:birthday 以 yyyy-MM-dd 字符串、gender 为 male/female 写入 updateUserInfo
  208. // ---------------------------------------------------------------------------
  209. describe('AC1.3 提交载荷与 picker 映射', function () {
  210. var vm
  211. beforeEach(function () {
  212. var component = loadJoinForm().component
  213. vm = instantiate(component)
  214. vm.familyInviteCode = 'CODE-123'
  215. vm.form.realName = '张三'
  216. vm.form.birthday = '1990-05-20'
  217. vm.form.gender = 'male'
  218. vm.form.nickname = ''
  219. vm.form.avatar = ''
  220. })
  221. it('出生日期 change 处理器以 yyyy-MM-dd 写入 form.birthday', function () {
  222. expect(typeof vm.onBirthdayChange).toBe('function')
  223. vm.onBirthdayChange({ detail: { value: '2010-01-15' } })
  224. expect(vm.form.birthday).toBe('2010-01-15')
  225. })
  226. it('性别 picker 索引 0 → male、索引 1 → female', function () {
  227. expect(typeof vm.onGenderChange).toBe('function')
  228. vm.onGenderChange({ detail: { value: 0 } })
  229. expect(vm.form.gender).toBe('male')
  230. vm.onGenderChange({ detail: { value: 1 } })
  231. expect(vm.form.gender).toBe('female')
  232. })
  233. it('合法提交:updateUserInfo 收到 realName(trim)/birthday(yyyy-MM-dd)/gender(male|female)', async function () {
  234. await vm.submit()
  235. expect(stubs.updateUserInfo).toHaveBeenCalledWith(expect.objectContaining({
  236. realName: '张三',
  237. birthday: '1990-05-20',
  238. gender: 'male'
  239. }))
  240. })
  241. it('合法提交:随后调用 requestJoinByCode(inviteCode) 正常进入待审批', async function () {
  242. await vm.submit()
  243. expect(stubs.requestJoinByCode).toHaveBeenCalledWith('CODE-123')
  244. })
  245. })
  246. // ---------------------------------------------------------------------------
  247. // 设计锚点:utils/api.js updateUserInfo 需支持 realName/gender/birthday 透传
  248. // (规格 §2:接口封装无改动,锚定「已支持透传」契约)
  249. // ---------------------------------------------------------------------------
  250. describe('设计锚点:api.js updateUserInfo 透传 realName/gender/birthday', function () {
  251. it('POST /api/user/update 请求体含 realName/gender/birthday 三字段', async function () {
  252. var captured = null
  253. global.uni.request.mockImplementation(function (opts) {
  254. captured = opts
  255. process.nextTick(function () { opts.success({ data: { code: 200, message: 'ok', data: null } }) })
  256. })
  257. clearApiDedup()
  258. await apiUpdateUserInfo({ realName: '张三', gender: 'male', birthday: '1990-05-20' })
  259. expect(captured.url).toContain('/api/user/update')
  260. expect(captured.method).toBe('POST')
  261. expect(captured.data).toEqual(expect.objectContaining({
  262. realName: '张三',
  263. gender: 'male',
  264. birthday: '1990-05-20'
  265. }))
  266. })
  267. })
  268. // ---------------------------------------------------------------------------
  269. // 权限流:本能力为「被邀请人加入申请表单」,不区分角色(邀请人/被邀请人均不触发角色门禁),
  270. // 属《编排派发》中「权限(适用时)」的不适用项,显式记录。
  271. // ---------------------------------------------------------------------------
  272. describe.skip('权限流:加入表单无角色门禁,不适用', function () {
  273. it('占位:该能力面向被邀请人,无 role 分支(详见汇报)', function () {})
  274. })