flnxi-Wu/app/admin/_components/template-manager/__tests__/CustomTemplateSection.test.tsx
2026-05-28 23:27:15 +08:00

115 lines
3.3 KiB
TypeScript

/**
* @jest-environment jsdom
*/
import React from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import CustomTemplateSection from '../CustomTemplateSection'
import type { DisplayTemplate } from '../types'
jest.mock('@/lib/ui-styles', () => ({
cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),
useThemeStyles: () => ({
cardStyles: {
flat: 'card-flat',
hover: 'card-hover',
},
buttonStyles: {
secondary: 'button-secondary',
success: 'button-success',
danger: 'button-danger',
},
buttonModifiers: {
compactXs: 'button-compact-xs',
pill: 'button-pill',
},
}),
useTheme: () => 'light',
}))
describe('CustomTemplateSection 授权提示', () => {
const callbacks = {
onRequestUpgrade: jest.fn(),
onCreate: jest.fn(),
onPreview: jest.fn(),
onActivate: jest.fn(),
onDeactivate: jest.fn(),
onEdit: jest.fn(),
onDuplicate: jest.fn(),
onDelete: jest.fn(),
}
const customTemplate: DisplayTemplate = {
id: 'template-1',
name: '客户自定义模板',
description: '用于测试授权限制',
status: 'active',
materialType: '3DMAX',
materialKind: 'IMAGE',
version: 1,
fieldGroups: null,
displayConfig: null,
createdAt: '2026-05-25T00:00:00.000Z',
updatedAt: '2026-05-25T00:00:00.000Z',
fields: [],
updatedLabel: '刚刚更新',
fieldCount: 0,
statusLabel: '已启用',
}
beforeEach(() => {
jest.clearAllMocks()
})
function renderSection(canManageCustomTemplates: boolean) {
return render(
<CustomTemplateSection
loading={false}
error={null}
templates={[customTemplate]}
filteredTemplates={[customTemplate]}
deletingId={null}
canManageCustomTemplates={canManageCustomTemplates}
{...callbacks}
/>
)
}
it('许可证不包含自定义模板时应显示前置授权提示并拦截新建入口', () => {
renderSection(false)
expect(screen.getByText('当前许可证未包含自定义模板功能,请联系管理员处理授权')).toBeInTheDocument()
expect(screen.getByText('授权受限')).toBeInTheDocument()
fireEvent.click(screen.getByText('授权后可新建'))
expect(callbacks.onRequestUpgrade).toHaveBeenCalledTimes(1)
expect(callbacks.onCreate).not.toHaveBeenCalled()
})
it('许可证允许自定义模板时应直接执行新建动作', () => {
renderSection(true)
expect(screen.getByText('这里列出你自己创建的模板,可继续编辑、复制或删除')).toBeInTheDocument()
fireEvent.click(screen.getByText('新建自定义模板'))
expect(callbacks.onCreate).toHaveBeenCalledTimes(1)
expect(callbacks.onRequestUpgrade).not.toHaveBeenCalled()
})
it('许可证不包含自定义模板时应拦截编辑、复制和启停动作', () => {
renderSection(false)
fireEvent.click(screen.getByText('取消启用'))
fireEvent.click(screen.getByText('编辑'))
fireEvent.click(screen.getByText('复制'))
expect(callbacks.onRequestUpgrade).toHaveBeenCalledTimes(3)
expect(callbacks.onDeactivate).not.toHaveBeenCalled()
expect(callbacks.onEdit).not.toHaveBeenCalled()
expect(callbacks.onDuplicate).not.toHaveBeenCalled()
})
})