Explorar o código

fix: validate signup balance by family member

Developer hai 1 semana
pai
achega
5212bbad6b
Modificáronse 2 ficheiros con 61 adicións e 5 borrados
  1. 6 5
      pagesHome/activityDetail.vue
  2. 55 0
      tests/activity-signup-member-balance.test.js

+ 6 - 5
pagesHome/activityDetail.vue

@@ -325,13 +325,14 @@
 		const needLevel = activityInfo.value?.userLevel ?? 0;
 		if(level < needLevel) return proxy.$showModal(`很抱歉,当前活动限制报名等级为Lv${needLevel},您当前等级为Lv${level},不满足报名条件。`)
 		
-		let { loveValue, couponIds } = await getUserLoveTicket();
 		let url = `/pagesHome/activityApply?activity=${encodeURIComponent(JSON.stringify(activityInfo.value))}`;
 		
-		if((activityInfo.value?.activityLimit==1&&loveValue<activityInfo?.value.valueLimit)
-		||(activityInfo.value?.activityLimit==2&&couponIds?.length<activityInfo?.value.valueLimit)){
-			fail.value = true;
-			return 
+		if(activityInfo.value?.activityLimit==2){
+			const { couponIds } = await getUserLoveTicket();
+			if(couponIds?.length<activityInfo.value.valueLimit){
+				fail.value = true;
+				return
+			}
 		}
 		uni.navigateTo({ url })
 	}

+ 55 - 0
tests/activity-signup-member-balance.test.js

@@ -0,0 +1,55 @@
+import test from 'node:test'
+import assert from 'node:assert/strict'
+import fs from 'node:fs'
+import path from 'node:path'
+import vm from 'node:vm'
+
+const detailFile = path.resolve('pagesHome/activityDetail.vue')
+
+function loadHandleApply ({ activityLimit, valueLimit, parentLoveValue, couponIds = [] }) {
+  const source = fs.readFileSync(detailFile, 'utf8')
+  const start = source.indexOf('\tconst handleApply = async () => {')
+  const end = source.indexOf('\n\t//取消报名', start)
+  assert.notEqual(start, -1, '应找到立即报名方法')
+  assert.notEqual(end, -1, '应找到立即报名方法结束位置')
+
+  const navigations = []
+  const fail = { value: false }
+  const context = {
+    activityInfo: {
+      value: {
+        id: 'activity-1',
+        activityLimit,
+        valueLimit,
+        userLevel: 0
+      }
+    },
+    fail,
+    getUserLoveTicket: () => Promise.resolve({ loveValue: parentLoveValue, couponIds }),
+    proxy: { $showModal: () => {} },
+    uni: {
+      getStorageSync: () => JSON.stringify({ id: 'parent-1', userLevel: 0 }),
+      navigateTo: options => navigations.push(options)
+    },
+    encodeURIComponent,
+    JSON
+  }
+  vm.createContext(context)
+  vm.runInContext(`${source.slice(start, end)}\nthis.handleApply = handleApply`, context)
+
+  return { handleApply: context.handleApply, fail, navigations }
+}
+
+test('家长爱心值为零时仍可进入报名页并由后端校验所选孩子余额', async () => {
+  const { handleApply, fail, navigations } = loadHandleApply({
+    activityLimit: 1,
+    valueLimit: 10,
+    parentLoveValue: 0
+  })
+
+  await handleApply()
+
+  assert.equal(fail.value, false)
+  assert.equal(navigations.length, 1)
+  assert.match(navigations[0].url, /^\/pagesHome\/activityApply\?activity=/)
+})