120 lines
3.5 KiB
TypeScript
120 lines
3.5 KiB
TypeScript
import { tap, TapTools } from '../../ts_tapbundle/index.js';
|
|
import { expect } from '@push.rocks/smartexpect';
|
|
|
|
// Define fixture factories
|
|
interface User {
|
|
id: number;
|
|
name: string;
|
|
email: string;
|
|
role: string;
|
|
}
|
|
|
|
interface Post {
|
|
id: number;
|
|
title: string;
|
|
content: string;
|
|
authorId: number;
|
|
tags: string[];
|
|
}
|
|
|
|
// Define user fixture factory
|
|
TapTools.defineFixture<User>('user', (data) => {
|
|
const id = data?.id || Math.floor(Math.random() * 10000);
|
|
return {
|
|
id,
|
|
name: data?.name || `Test User ${id}`,
|
|
email: data?.email || `user${id}@test.com`,
|
|
role: data?.role || 'user'
|
|
};
|
|
});
|
|
|
|
// Define post fixture factory
|
|
TapTools.defineFixture<Post>('post', async (data) => {
|
|
const id = data?.id || Math.floor(Math.random() * 10000);
|
|
return {
|
|
id,
|
|
title: data?.title || `Post ${id}`,
|
|
content: data?.content || `Content for post ${id}`,
|
|
authorId: data?.authorId || 1,
|
|
tags: data?.tags || ['test', 'sample']
|
|
};
|
|
});
|
|
|
|
tap.describe('Fixture System', () => {
|
|
tap.afterEach(async () => {
|
|
// Clean up fixtures after each test
|
|
await TapTools.cleanupFixtures();
|
|
});
|
|
|
|
tap.tags('unit', 'fixtures')
|
|
.test('should create a simple fixture', async (toolsArg) => {
|
|
const user = await toolsArg.fixture<User>('user');
|
|
|
|
expect(user).toHaveProperty('id');
|
|
expect(user).toHaveProperty('name');
|
|
expect(user).toHaveProperty('email');
|
|
expect(user.role).toEqual('user');
|
|
});
|
|
|
|
tap.tags('unit', 'fixtures')
|
|
.test('should create fixture with custom data', async (toolsArg) => {
|
|
const admin = await toolsArg.fixture<User>('user', {
|
|
name: 'Admin User',
|
|
role: 'admin'
|
|
});
|
|
|
|
expect(admin.name).toEqual('Admin User');
|
|
expect(admin.role).toEqual('admin');
|
|
expect(admin.email).toContain('@test.com');
|
|
});
|
|
|
|
tap.tags('unit', 'fixtures')
|
|
.test('should create multiple fixtures with factory', async (toolsArg) => {
|
|
const userFactory = toolsArg.factory<User>('user');
|
|
const users = await userFactory.createMany(3);
|
|
|
|
// Try different approach
|
|
expect(users.length).toEqual(3);
|
|
expect(users[0].id).not.toEqual(users[1].id);
|
|
expect(users[0].email).not.toEqual(users[1].email);
|
|
});
|
|
|
|
tap.tags('unit', 'fixtures')
|
|
.test('should create fixtures with custom data per instance', async (toolsArg) => {
|
|
const postFactory = toolsArg.factory<Post>('post');
|
|
const posts = await postFactory.createMany(3, (index) => ({
|
|
title: `Post ${index + 1}`,
|
|
tags: [`tag${index + 1}`]
|
|
}));
|
|
|
|
expect(posts[0].title).toEqual('Post 1');
|
|
expect(posts[1].title).toEqual('Post 2');
|
|
expect(posts[2].title).toEqual('Post 3');
|
|
|
|
expect(posts[0].tags).toContain('tag1');
|
|
expect(posts[1].tags).toContain('tag2');
|
|
});
|
|
|
|
tap.tags('unit', 'fixtures')
|
|
.test('should handle related fixtures', async (toolsArg) => {
|
|
const user = await toolsArg.fixture<User>('user', { name: 'Author' });
|
|
const post = await toolsArg.fixture<Post>('post', {
|
|
title: 'My Article',
|
|
authorId: user.id
|
|
});
|
|
|
|
expect(post.authorId).toEqual(user.id);
|
|
});
|
|
|
|
tap.tags('unit', 'fixtures', 'error')
|
|
.test('should throw error for undefined fixture', async (toolsArg) => {
|
|
try {
|
|
await toolsArg.fixture('nonexistent');
|
|
expect(true).toBeFalse(); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.message).toContain('Fixture \'nonexistent\' not found');
|
|
}
|
|
});
|
|
});
|
|
|
|
tap.start(); |