- 在 .env.production 中新增 VITE_AUTH_COOKIE_NAME 配置项用于设置认证cookie名称 - 将 AcceptanceException.vue 中的表单标签宽度从固定90px改为自动适配 - 在 ActionDialog.vue 中将多个表单项标记为必填项并调整标签宽度为自动适配 - 重构 ActionHistory.vue 中的按钮布局结构并添加表格列样式类名 - 更新 api-contract-audit.cjs 添加生产任务和BOM相关接口的契约测试 - 修改 auth.js 使用动态认证cookie键名替换硬编码的Admin-Token - 调整系统角色授权用户页面的表单布局和表格样式类名 - 优化 ReceiptDialog Batch 组件的对话框尺寸、表单验证和分页功能
193 lines
7.2 KiB
JavaScript
193 lines
7.2 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
|
|
const source = readFileSync(new URL('../../src/components/ExcelImportDialog/index.vue', import.meta.url), 'utf8')
|
|
const script = source.match(/<script>([\s\S]*?)<\/script>/)[1]
|
|
.replace(/^import .*$/gm, '')
|
|
.replace('export default', 'return')
|
|
.replaceAll('import.meta.env.VITE_APP_BASE_API', "'/api'")
|
|
|
|
function instance(request = async () => ({ data: [] })) {
|
|
const component = new Function('getToken', 'request', script)(() => 'test-token', request)
|
|
const messages = []
|
|
const state = {
|
|
...component.data(),
|
|
previewAction: '/preview',
|
|
$refs: { uploadRef: { submit() { state.submitted = (state.submitted || 0) + 1 } } },
|
|
$nextTick: callback => Promise.resolve().then(callback),
|
|
$modal: {
|
|
msgError: value => messages.push(['error', value]),
|
|
msgWarning: value => messages.push(['warning', value])
|
|
},
|
|
$alert: () => {},
|
|
$emit: name => { state.emitted = name },
|
|
messages
|
|
}
|
|
for (const [name, method] of Object.entries(component.methods)) state[name] = method.bind(state)
|
|
return { state, component }
|
|
}
|
|
|
|
function selectedFile(name = '采购.xlsx', uid = 7) {
|
|
const raw = new Blob(['excel-fixture'], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
|
|
raw.name = name
|
|
return { name, raw, uid, status: 'ready' }
|
|
}
|
|
|
|
test('preview uses the controlled file raw, not the unexposed uploadFiles property', async () => {
|
|
let submitted
|
|
const { state } = instance(async options => {
|
|
submitted = options
|
|
return { data: [{ line: 2, summary: '项目 / 物品', error: '' }] }
|
|
})
|
|
state.open()
|
|
state.fileList = [selectedFile()]
|
|
await state.handleSubmit()
|
|
assert.equal(submitted.url, '/preview')
|
|
assert.equal(submitted.data.get('file').size, state.fileList[0].raw.size)
|
|
assert.equal(state.previewReady, true)
|
|
assert.equal(state.isUploading, false)
|
|
assert.equal(state.submitted, undefined)
|
|
assert.match(source, /v-model:file-list="fileList"/)
|
|
assert.doesNotMatch(source, /\.uploadFiles\b/)
|
|
})
|
|
|
|
test('empty, missing raw and unsupported files are rejected before either request', async () => {
|
|
let requests = 0
|
|
const { state } = instance(async () => { requests++; return { data: [] } })
|
|
for (const files of [[], [{ name: 'saved.xlsx' }], [selectedFile('bad.pdf')]]) {
|
|
state.fileList = files
|
|
await state.handleSubmit()
|
|
}
|
|
assert.equal(requests, 0)
|
|
assert.equal(state.messages.length, 3)
|
|
})
|
|
|
|
test('preview locks changes, closing and repeat submission until the response completes', async () => {
|
|
let finish
|
|
let requests = 0
|
|
const { state } = instance(() => { requests++; return new Promise(resolve => { finish = resolve }) })
|
|
state.open()
|
|
state.fileList = [selectedFile()]
|
|
const pending = state.handleSubmit()
|
|
assert.equal(state.isUploading, true)
|
|
await state.handleSubmit()
|
|
assert.equal(requests, 1)
|
|
assert.equal(state.handleBeforeRemove(), false)
|
|
let closed = false
|
|
state.handleBeforeClose(() => { closed = true })
|
|
state.handleCancel()
|
|
state.open()
|
|
assert.equal(closed, false)
|
|
assert.equal(state.visible, true)
|
|
assert.equal(state.fileList.length, 1)
|
|
finish({ data: [{ line: 2, error: '' }] })
|
|
await pending
|
|
assert.equal(state.isUploading, false)
|
|
assert.equal(state.handleBeforeRemove(), true)
|
|
})
|
|
|
|
test('preview errors and transport failures retain the selected file for another attempt', async () => {
|
|
let requests = 0
|
|
const { state } = instance(async () => {
|
|
requests++
|
|
if (requests === 1) throw new Error('network failure')
|
|
return { data: [{ line: 2, error: '物品类别不是启用的小类' }] }
|
|
})
|
|
state.open()
|
|
state.fileList = [selectedFile()]
|
|
await state.handleSubmit()
|
|
assert.equal(state.isUploading, false)
|
|
assert.equal(state.fileList.length, 1)
|
|
assert.equal(state.previewReady, false)
|
|
await state.handleSubmit()
|
|
assert.equal(requests, 2)
|
|
assert.equal(state.previewReady, false)
|
|
assert.equal(state.previewRows[0].error, '物品类别不是启用的小类')
|
|
assert.equal(state.submitted, undefined)
|
|
})
|
|
|
|
test('actual upload locks immediately, uses the public submit API and prevents duplicate clicks', async () => {
|
|
const { state } = instance()
|
|
state.open()
|
|
state.fileList = [selectedFile()]
|
|
state.previewReady = true
|
|
const upload = state.handleSubmit()
|
|
assert.equal(state.isUploading, true)
|
|
await state.handleSubmit()
|
|
await upload
|
|
assert.equal(state.submitted, 1)
|
|
assert.equal(state.isUploading, true)
|
|
})
|
|
|
|
test('transport-failed and business-rejected uploads restore a retryable file and invalidate preview', async () => {
|
|
for (const failedTransport of [true, false]) {
|
|
const { state } = instance()
|
|
const file = selectedFile()
|
|
state.open()
|
|
state.fileList = [file]
|
|
state.previewReady = true
|
|
state.isUploading = true
|
|
if (failedTransport) {
|
|
// Current Element Plus removes the failed file before firing onError.
|
|
state.fileList = []
|
|
state.handleError(new Error('{"msg":"连接中断"}'), file)
|
|
} else {
|
|
file.status = 'success'
|
|
state.handleSuccess({ code: 500, msg: '请检查问题行' }, file)
|
|
}
|
|
await state.$nextTick()
|
|
assert.equal(state.isUploading, false)
|
|
assert.equal(state.previewReady, false)
|
|
assert.equal(state.fileList[0].raw, file.raw)
|
|
assert.equal(state.fileList[0].status, 'ready')
|
|
assert.equal(state.visible, true)
|
|
}
|
|
})
|
|
|
|
test('successful upload clears files and preview and emits one success', () => {
|
|
const { state } = instance()
|
|
state.open()
|
|
state.fileList = [selectedFile()]
|
|
state.previewRows = [{ line: 2 }]
|
|
state.previewReady = true
|
|
state.isUploading = true
|
|
state.handleSuccess({ code: 200, msg: '导入成功' }, state.fileList[0])
|
|
assert.equal(state.visible, false)
|
|
assert.equal(state.isUploading, false)
|
|
assert.deepEqual(state.fileList, [])
|
|
assert.deepEqual(state.previewRows, [])
|
|
assert.equal(state.previewReady, false)
|
|
assert.equal(state.emitted, 'success')
|
|
})
|
|
|
|
test('changing options, removing files and reopening invalidate previous preview', () => {
|
|
const { state, component } = instance()
|
|
const setPreview = () => { state.previewRows = [{ line: 2 }]; state.previewReady = true }
|
|
for (const invalidate of [() => state.resetPreview(), () => component.watch.updateSupport.call(state), () => state.open()]) {
|
|
setPreview()
|
|
invalidate()
|
|
assert.equal(state.previewReady, false)
|
|
assert.deepEqual(state.previewRows, [])
|
|
}
|
|
state.fileList = [selectedFile()]
|
|
state.visible = false
|
|
state.handleClose()
|
|
assert.deepEqual(state.fileList, [])
|
|
state.open()
|
|
state.fileList = [selectedFile('new.xlsx', 8)]
|
|
state.handleClose()
|
|
assert.equal(state.fileList[0].uid, 8)
|
|
})
|
|
|
|
test('retains instructions and shows complete check errors without tooltip truncation', () => {
|
|
assert.match(source, /<slot name="instructions"\s*\/>/)
|
|
const errorColumn = source.match(/<el-table-column label="检查结果"([^>]*)>/)[1]
|
|
assert.doesNotMatch(errorColumn, /show-overflow-tooltip/)
|
|
assert.match(source, /white-space:\s*normal/)
|
|
assert.match(source, /overflow-wrap:\s*anywhere/)
|
|
assert.match(source, /<el-checkbox[^>]*:disabled="isUploading"/)
|
|
assert.match(source, /:before-remove="handleBeforeRemove"/)
|
|
assert.match(source, /:before-close="handleBeforeClose"/)
|
|
})
|