3d76ded24a
ci/woodpecker/push/woodpecker Pipeline failed
新增功能: - test-data-factory.ts: 统一的测试数据工厂 - 支持创建用户、产品、新闻、联系表单数据 - 支持批量创建测试数据 - 支持覆盖默认属性 - 提供便捷函数 - test-data-cleaner.ts: 测试数据清理工具 - 自动清理mock函数 - 清理localStorage/sessionStorage - 清理定时器和事件监听器 - 提供withCleanup装饰器 测试覆盖: - test-data-factory.test.ts: 22个测试用例 - test-data-cleaner.test.ts: 9个测试用例 优化效果: - 减少测试代码重复 - 提高测试可维护性 - 标准化测试数据管理
222 lines
6.0 KiB
TypeScript
222 lines
6.0 KiB
TypeScript
import { describe, it, expect, beforeEach } from '@jest/globals';
|
|
import {
|
|
TestDataFactory,
|
|
createTestUser,
|
|
createTestProduct,
|
|
createTestNews,
|
|
createTestContactFormData,
|
|
} from './test-data-factory';
|
|
|
|
describe('TestDataFactory', () => {
|
|
beforeEach(() => {
|
|
TestDataFactory.reset();
|
|
});
|
|
|
|
describe('createUser', () => {
|
|
it('应该创建用户对象', () => {
|
|
const user = TestDataFactory.createUser();
|
|
|
|
expect(user).toHaveProperty('id');
|
|
expect(user).toHaveProperty('name');
|
|
expect(user).toHaveProperty('email');
|
|
expect(user).toHaveProperty('role');
|
|
expect(user).toHaveProperty('createdAt');
|
|
expect(user.role).toBe('user');
|
|
});
|
|
|
|
it('应该支持覆盖属性', () => {
|
|
const user = TestDataFactory.createUser({
|
|
name: '自定义用户',
|
|
role: 'admin',
|
|
});
|
|
|
|
expect(user.name).toBe('自定义用户');
|
|
expect(user.role).toBe('admin');
|
|
});
|
|
|
|
it('应该生成唯一ID', () => {
|
|
const user1 = TestDataFactory.createUser();
|
|
const user2 = TestDataFactory.createUser();
|
|
|
|
expect(user1.id).not.toBe(user2.id);
|
|
});
|
|
});
|
|
|
|
describe('createAdmin', () => {
|
|
it('应该创建管理员用户', () => {
|
|
const admin = TestDataFactory.createAdmin();
|
|
|
|
expect(admin.role).toBe('admin');
|
|
});
|
|
|
|
it('应该支持覆盖属性', () => {
|
|
const admin = TestDataFactory.createAdmin({
|
|
name: '超级管理员',
|
|
});
|
|
|
|
expect(admin.name).toBe('超级管理员');
|
|
expect(admin.role).toBe('admin');
|
|
});
|
|
});
|
|
|
|
describe('createProduct', () => {
|
|
it('应该创建产品对象', () => {
|
|
const product = TestDataFactory.createProduct();
|
|
|
|
expect(product).toHaveProperty('id');
|
|
expect(product).toHaveProperty('name');
|
|
expect(product).toHaveProperty('price');
|
|
expect(typeof product.price).toBe('number');
|
|
});
|
|
|
|
it('应该支持覆盖属性', () => {
|
|
const product = TestDataFactory.createProduct({
|
|
name: '自定义产品',
|
|
price: 999,
|
|
});
|
|
|
|
expect(product.name).toBe('自定义产品');
|
|
expect(product.price).toBe(999);
|
|
});
|
|
});
|
|
|
|
describe('createNews', () => {
|
|
it('应该创建新闻对象', () => {
|
|
const news = TestDataFactory.createNews();
|
|
|
|
expect(news).toHaveProperty('id');
|
|
expect(news).toHaveProperty('title');
|
|
expect(news).toHaveProperty('content');
|
|
expect(news).toHaveProperty('author');
|
|
expect(news).toHaveProperty('publishedAt');
|
|
});
|
|
|
|
it('应该支持覆盖属性', () => {
|
|
const news = TestDataFactory.createNews({
|
|
title: '自定义新闻标题',
|
|
author: '自定义作者',
|
|
});
|
|
|
|
expect(news.title).toBe('自定义新闻标题');
|
|
expect(news.author).toBe('自定义作者');
|
|
});
|
|
});
|
|
|
|
describe('createContactFormData', () => {
|
|
it('应该创建联系表单数据', () => {
|
|
const formData = TestDataFactory.createContactFormData();
|
|
|
|
expect(formData).toHaveProperty('name');
|
|
expect(formData).toHaveProperty('email');
|
|
expect(formData).toHaveProperty('message');
|
|
});
|
|
|
|
it('应该支持覆盖属性', () => {
|
|
const formData = TestDataFactory.createContactFormData({
|
|
name: '自定义姓名',
|
|
email: 'custom@test.com',
|
|
});
|
|
|
|
expect(formData.name).toBe('自定义姓名');
|
|
expect(formData.email).toBe('custom@test.com');
|
|
});
|
|
});
|
|
|
|
describe('createMany', () => {
|
|
it('应该创建多个对象', () => {
|
|
const users = TestDataFactory.createMany(
|
|
() => TestDataFactory.createUser(),
|
|
5
|
|
);
|
|
|
|
expect(users).toHaveLength(5);
|
|
expect(users[0].id).not.toBe(users[1].id);
|
|
});
|
|
|
|
it('默认应该创建3个对象', () => {
|
|
const products = TestDataFactory.createMany(
|
|
() => TestDataFactory.createProduct()
|
|
);
|
|
|
|
expect(products).toHaveLength(3);
|
|
});
|
|
});
|
|
|
|
describe('createUsers', () => {
|
|
it('应该创建多个用户', () => {
|
|
const users = TestDataFactory.createUsers(5);
|
|
|
|
expect(users).toHaveLength(5);
|
|
users.forEach(user => {
|
|
expect(user).toHaveProperty('id');
|
|
expect(user).toHaveProperty('name');
|
|
});
|
|
});
|
|
|
|
it('应该支持覆盖属性', () => {
|
|
const users = TestDataFactory.createUsers(3, { role: 'admin' });
|
|
|
|
users.forEach(user => {
|
|
expect(user.role).toBe('admin');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('createProducts', () => {
|
|
it('应该创建多个产品', () => {
|
|
const products = TestDataFactory.createProducts(4);
|
|
|
|
expect(products).toHaveLength(4);
|
|
products.forEach(product => {
|
|
expect(product).toHaveProperty('id');
|
|
expect(product).toHaveProperty('name');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('createNewsList', () => {
|
|
it('应该创建多个新闻', () => {
|
|
const newsList = TestDataFactory.createNewsList(3);
|
|
|
|
expect(newsList).toHaveLength(3);
|
|
newsList.forEach(news => {
|
|
expect(news).toHaveProperty('id');
|
|
expect(news).toHaveProperty('title');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('reset', () => {
|
|
it('应该重置ID计数器', () => {
|
|
TestDataFactory.createUser();
|
|
TestDataFactory.createUser();
|
|
TestDataFactory.reset();
|
|
|
|
const user = TestDataFactory.createUser();
|
|
expect(user.id).toContain('test-id-1-');
|
|
});
|
|
});
|
|
|
|
describe('便捷函数', () => {
|
|
it('createTestUser应该创建用户', () => {
|
|
const user = createTestUser({ name: '测试用户' });
|
|
expect(user.name).toBe('测试用户');
|
|
});
|
|
|
|
it('createTestProduct应该创建产品', () => {
|
|
const product = createTestProduct({ name: '测试产品' });
|
|
expect(product.name).toBe('测试产品');
|
|
});
|
|
|
|
it('createTestNews应该创建新闻', () => {
|
|
const news = createTestNews({ title: '测试新闻' });
|
|
expect(news.title).toBe('测试新闻');
|
|
});
|
|
|
|
it('createTestContactFormData应该创建联系表单数据', () => {
|
|
const formData = createTestContactFormData({ name: '测试联系人' });
|
|
expect(formData.name).toBe('测试联系人');
|
|
});
|
|
});
|
|
});
|