/** * @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) => 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( ) } it('许可证不包含自定义模板时应显示前置授权提示并拦截新建入口', () => { renderSection(false) expect(screen.getByText('当前许可证未开放自定义模板功能,升级后可新建、复制、编辑和迁移模板')).toBeInTheDocument() expect(screen.getByTestId('pro-badge')).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() }) })