UNPKG

hey-api-builders

Version:

A custom plugin for @hey-api/openapi-ts that generates mock data builders with a lightweight custom runtime, Zod integration, or static mock generation.

244 lines 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const handler_1 = require("./handler"); (0, vitest_1.describe)('handler integration tests', () => { (0, vitest_1.it)('should generate complete output with multiple schemas', () => { const userSchema = { type: 'object', properties: { id: { type: 'number' }, name: { type: 'string' }, email: { type: 'string', format: 'email' }, }, required: ['id', 'name', 'email'], }; const statusSchema = { type: 'string', enum: ['active', 'inactive', 'pending'], }; let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: {}, forEach: vitest_1.vi.fn((type, callback) => { if (type === 'schema') { callback({ name: 'User', schema: userSchema }); callback({ name: 'Status', schema: statusSchema }); } }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).toContain('import'); (0, vitest_1.expect)(output).toContain('type BuilderOptions'); (0, vitest_1.expect)(output).toContain('class UserBuilder'); (0, vitest_1.expect)(output).toContain('class StatusBuilder'); (0, vitest_1.expect)(output).toContain('schemas'); }); (0, vitest_1.it)('should generate output with Zod schemas', () => { const productSchema = { type: 'object', properties: { id: { type: 'number' }, title: { type: 'string' }, price: { type: 'number', minimum: 0 }, }, required: ['id', 'title', 'price'], }; let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: { generateZod: true }, forEach: vitest_1.vi.fn((type, callback) => { if (type === 'schema') { callback({ name: 'Product', schema: productSchema }); } }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).toContain('zodSchemas'); (0, vitest_1.expect)(output).toContain('z.object'); (0, vitest_1.expect)(output).toContain('class ProductBuilder'); }); (0, vitest_1.it)('should generate output with static mocks', () => { const itemSchema = { type: 'object', properties: { name: { type: 'string' }, quantity: { type: 'integer', minimum: 0 }, }, required: ['name', 'quantity'], }; let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: { mockStrategy: 'static' }, forEach: vitest_1.vi.fn((type, callback) => { if (type === 'schema') { callback({ name: 'Item', schema: itemSchema }); } }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).not.toContain('schemas'); (0, vitest_1.expect)(output).toContain('class ItemBuilder'); }); (0, vitest_1.it)('should generate output with Zod mocks', () => { const orderSchema = { type: 'object', properties: { orderId: { type: 'string' }, amount: { type: 'number' }, }, required: ['orderId', 'amount'], }; let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: { mockStrategy: 'zod' }, forEach: vitest_1.vi.fn((type, callback) => { if (type === 'schema') { callback({ name: 'Order', schema: orderSchema }); } }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).toContain('zodSchemas'); (0, vitest_1.expect)(output).toContain('generateMockFromZodSchema'); (0, vitest_1.expect)(output).toContain('class OrderBuilder'); }); (0, vitest_1.it)('should support backward compatibility with useStaticMocks', () => { const itemSchema = { type: 'object', properties: { name: { type: 'string' }, }, required: ['name'], }; let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: { useStaticMocks: true }, forEach: vitest_1.vi.fn((type, callback) => { if (type === 'schema') { callback({ name: 'Item', schema: itemSchema }); } }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).not.toContain('schemas'); (0, vitest_1.expect)(output).toContain('class ItemBuilder'); }); (0, vitest_1.it)('should support backward compatibility with useZodForMocks', () => { const orderSchema = { type: 'object', properties: { orderId: { type: 'string' }, }, required: ['orderId'], }; let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: { useZodForMocks: true }, forEach: vitest_1.vi.fn((type, callback) => { if (type === 'schema') { callback({ name: 'Order', schema: orderSchema }); } }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).toContain('zodSchemas'); (0, vitest_1.expect)(output).toContain('generateMockFromZodSchema'); }); (0, vitest_1.it)('should handle enum schemas correctly', () => { const roleSchema = { type: 'string', enum: ['admin', 'user', 'guest'], }; let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: {}, forEach: vitest_1.vi.fn((type, callback) => { if (type === 'schema') { callback({ name: 'Role', schema: roleSchema }); } }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).toContain('class RoleBuilder'); (0, vitest_1.expect)(output).toContain('admin'); (0, vitest_1.expect)(output).toContain('user'); (0, vitest_1.expect)(output).toContain('guest'); }); (0, vitest_1.it)('should handle empty schema collection', () => { let output = ''; const mockFile = { add: vitest_1.vi.fn((content) => { output += content; }), }; const mockPlugin = { name: 'hey-api-builders', output: 'builders', config: {}, forEach: vitest_1.vi.fn((_type, _callback) => { }), createFile: vitest_1.vi.fn(() => mockFile), }; (0, handler_1.handler)({ plugin: mockPlugin }); (0, vitest_1.expect)(output).toContain('import'); (0, vitest_1.expect)(output).toContain('type BuilderOptions'); (0, vitest_1.expect)(output).toContain('schemas = {'); }); }); //# sourceMappingURL=handler.test.js.map