| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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)
- })
|