Browse Source

fix: allow deleting eligible offline activities

Developer 1 week ago
parent
commit
9fd7f4d9da
2 changed files with 63 additions and 2 deletions
  1. 2 2
      src/views/modules/activity/list.vue
  2. 61 0
      tests/activity-delete-policy.test.js

+ 2 - 2
src/views/modules/activity/list.vue

@@ -80,7 +80,7 @@
                       <el-button link type="text" @click="handleUpDown(scope.row,0)" v-if="scope.row.state==1&&$hasPermission('core:activity:updown')&&scope.row.activeState>-2">下架</el-button>
                       <el-button link type="text" @click="handleUpDown(scope.row,1)" v-if="scope.row.state==0&&$hasPermission('core:activity:updown')&&scope.row.activeState>-2">上架</el-button>
                       <el-button link type="text" @click="handleCancel(scope.row)" v-if="$hasPermission('core:activity:cancel')&&scope.row.activeState!=-1&&(Date.parse(new Date())>Date.parse(scope.row.signupEndTime)&&Date.parse(new Date())<Date.parse(scope.row.activityStartTime))">取消</el-button>
-                      <el-button link type="text" @click="handleDelete(scope.row)" v-if="($hasPermission('core:activity:delete')&&scope.row.state==0&&scope.row.activeState==0)||($hasPermission('core:activity:delete')&&scope.row.activeState==-2)">删除</el-button>
+                      <el-button link type="text" @click="handleDelete(scope.row)" v-if="$hasPermission('core:activity:delete')&&(scope.row.activeState==-2||(scope.row.state==0&&(scope.row.recruitmentNow||0)==0))">删除</el-button>
                   </template>
               </el-table-column>
             </el-table>
@@ -248,7 +248,7 @@ export default {
       this.show = false
     },
     handleUpDown (row, state) {
-      if ([0, 1, 2].includes(row.activeState) && row.recruitmentNow > 0) return this.$message.error(`当前活动已存在报名人数,无法${state ? '上架' : '下架'}`)
+      if (state === 1 && [0, 1, 2].includes(row.activeState) && row.recruitmentNow > 0) return this.$message.error('当前活动已存在报名人数,无法上架')
       this.$confirm(`是否确认${state ? '上架' : '下架'}活动【${row.activityName}】?`, '提示', {
         confirmButtonText: '确定',
         cancelButtonText: '取消',

+ 61 - 0
tests/activity-delete-policy.test.js

@@ -0,0 +1,61 @@
+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'
+import { fileURLToPath } from 'node:url'
+import Vue from 'vue'
+import { compileToFunctions } from 'vue-template-compiler'
+
+const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
+const componentPath = path.join(root, 'src/views/modules/activity/list.vue')
+const source = fs.readFileSync(componentPath, 'utf8')
+
+function operationButtons (row) {
+  const operationSlot = source.match(/<el-table-column label="操作"[\s\S]*?<template #default="scope">([\s\S]*?)<\/template>/)[1]
+  const { render, staticRenderFns } = compileToFunctions(`<div>${operationSlot}</div>`)
+  Vue.prototype.$hasPermission = () => true
+  const vm = new Vue({
+    data: () => ({ scope: { row } }),
+    methods: {
+      handleEdit () {}, handleDetail () {}, handleInfo () {}, handleCode () {},
+      handleReviewCode () {}, handleUpDown () {}, handleCancel () {}, handleDelete () {}
+    },
+    render,
+    staticRenderFns
+  })
+  const vnode = vm._render()
+  return (vnode.children || []).map(child => child.children && child.children[0] && child.children[0].text).filter(Boolean)
+}
+
+function loadMethods () {
+  const script = source.match(/<script>([\s\S]*?)<\/script>/)[1]
+    .replace(/import moment from 'moment'/, "const moment = { format: value => value }")
+    .replace('export default', 'component =')
+  const sandbox = { component: null, Promise }
+  vm.runInNewContext(script, sandbox)
+  return sandbox.component.methods
+}
+
+test('报名中的活动下架且无人报名时显示删除按钮', () => {
+  const buttons = operationButtons({ state: 0, activeState: 1, recruitmentNow: 0 })
+  assert.ok(buttons.includes('删除'))
+})
+
+test('已有报名人数的上架活动仍允许下架', () => {
+  const methods = loadMethods()
+  let confirmCount = 0
+  let errorCount = 0
+  const context = {
+    $message: { error: () => { errorCount++ } },
+    $confirm: () => {
+      confirmCount++
+      return new Promise(() => {})
+    }
+  }
+
+  methods.handleUpDown.call(context, { activityName: '测试活动', activeState: 1, recruitmentNow: 2 }, 0)
+
+  assert.equal(confirmCount, 1)
+  assert.equal(errorCount, 0)
+})