activity-delete-policy.test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import fs from 'node:fs'
  4. import path from 'node:path'
  5. import vm from 'node:vm'
  6. import { fileURLToPath } from 'node:url'
  7. import Vue from 'vue'
  8. import { compileToFunctions } from 'vue-template-compiler'
  9. const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
  10. const componentPath = path.join(root, 'src/views/modules/activity/list.vue')
  11. const source = fs.readFileSync(componentPath, 'utf8')
  12. function operationButtons (row) {
  13. const operationSlot = source.match(/<el-table-column label="操作"[\s\S]*?<template #default="scope">([\s\S]*?)<\/template>/)[1]
  14. const { render, staticRenderFns } = compileToFunctions(`<div>${operationSlot}</div>`)
  15. Vue.prototype.$hasPermission = () => true
  16. const vm = new Vue({
  17. data: () => ({ scope: { row } }),
  18. methods: {
  19. handleEdit () {}, handleDetail () {}, handleInfo () {}, handleCode () {},
  20. handleReviewCode () {}, handleUpDown () {}, handleCancel () {}, handleDelete () {}
  21. },
  22. render,
  23. staticRenderFns
  24. })
  25. const vnode = vm._render()
  26. return (vnode.children || []).map(child => child.children && child.children[0] && child.children[0].text).filter(Boolean)
  27. }
  28. function loadMethods () {
  29. const script = source.match(/<script>([\s\S]*?)<\/script>/)[1]
  30. .replace(/import moment from 'moment'/, "const moment = { format: value => value }")
  31. .replace('export default', 'component =')
  32. const sandbox = { component: null, Promise }
  33. vm.runInNewContext(script, sandbox)
  34. return sandbox.component.methods
  35. }
  36. test('报名中的活动下架且无人报名时显示删除按钮', () => {
  37. const buttons = operationButtons({ state: 0, activeState: 1, recruitmentNow: 0 })
  38. assert.ok(buttons.includes('删除'))
  39. })
  40. test('已有报名人数的上架活动仍允许下架', () => {
  41. const methods = loadMethods()
  42. let confirmCount = 0
  43. let errorCount = 0
  44. const context = {
  45. $message: { error: () => { errorCount++ } },
  46. $confirm: () => {
  47. confirmCount++
  48. return new Promise(() => {})
  49. }
  50. }
  51. methods.handleUpDown.call(context, { activityName: '测试活动', activeState: 1, recruitmentNow: 2 }, 0)
  52. assert.equal(confirmCount, 1)
  53. assert.equal(errorCount, 0)
  54. })