All files / src/test-utils factories.ts

69.93% Statements 300/429
100% Branches 6/6
26.31% Functions 5/19
69.93% Lines 300/429

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 4301x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 100x 100x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                           1x 1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 1x 1x 1x                         1x 1x 1x 1x 1x                               1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x                             1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x  
/**
 * Test Data Factories
 *
 * Creates realistic test data for use in unit and integration tests.
 * All functions return deterministic data unless overrides are provided.
 */
 
// Counter for generating unique IDs
let idCounter = 0;
const generateId = () => ++idCounter;
const resetIdCounter = () => {
  idCounter = 0;
};
 
// Type definitions for factory outputs
interface TestProduct {
  id: number;
  title: string;
  description: string;
  price: number;
  discountedPrice: number;
  stock: number;
  sku: string;
  categoryId: number;
  specifications: Record<string, string>;
  details: string;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestCategory {
  id: number;
  title: string;
  description: string;
  slug: string;
  image: string;
  parentId: number | null;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestUser {
  id: number;
  name: string;
  email: string;
  password: string;
  image: string | null;
  role: 'CUSTOMER' | 'ADMIN';
  emailVerified: Date;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestTestimonial {
  id: number;
  review: string;
  authorName: string;
  authorRole: string;
  authorImage: string | null;
  isActive: boolean;
  createdAt: Date;
}
 
interface TestReview {
  id: number;
  productId: number;
  userId: number;
  rating: number;
  comment: string;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestOrder {
  id: number;
  userId: number;
  status: 'PENDING' | 'PROCESSING' | 'SHIPPED' | 'DELIVERED' | 'CANCELLED';
  total: number;
  subtotal: number;
  tax: number;
  shippingCost: number;
  shippingAddressId: number;
  paymentMethod: 'STRIPE' | 'PAYPAL' | 'COD';
  paymentStatus: 'PENDING' | 'PAID' | 'FAILED' | 'REFUNDED';
  stripePaymentIntentId: string;
  notes: string | null;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestOrderItem {
  id: number;
  orderId: number;
  productId: number;
  quantity: number;
  price: number;
}
 
interface TestCart {
  id: number;
  userId: number;
  productId: number;
  quantity: number;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestWishlistItem {
  id: number;
  userId: number;
  productId: number;
  createdAt: Date;
}
 
interface TestAddress {
  id: number;
  userId: number;
  firstName: string;
  lastName: string;
  street: string;
  city: string;
  state: string;
  postalCode: string;
  country: string;
  phone: string;
  isDefault: boolean;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestPromotion {
  id: number;
  name: string;
  description: string;
  type: 'PERCENTAGE' | 'FIXED';
  value: number;
  code: string;
  minPurchase: number;
  maxDiscount: number;
  usageLimit: number;
  usedCount: number;
  startDate: Date;
  endDate: Date;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestLoyaltyAccount {
  id: number;
  userId: number;
  points: number;
  tier: 'BRONZE' | 'SILVER' | 'GOLD' | 'PLATINUM';
  lifetimePoints: number;
  createdAt: Date;
  updatedAt: Date;
}
 
interface TestSupportTicket {
  id: number;
  userId: number;
  subject: string;
  description: string;
  status: 'OPEN' | 'IN_PROGRESS' | 'RESOLVED' | 'CLOSED';
  priority: 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT';
  category: 'ORDER' | 'PRODUCT' | 'SHIPPING' | 'PAYMENT' | 'OTHER';
  assignedTo: number | null;
  createdAt: Date;
  updatedAt: Date;
}
 
/**
 * Create a test product
 */
export const createProduct = (overrides: Partial<TestProduct> = {}): TestProduct => ({
  id: generateId(),
  title: `Test Product ${idCounter}`,
  description: 'A high-quality test product for your events.',
  price: 99.99,
  discountedPrice: 79.99,
  stock: 100,
  sku: `SKU-${idCounter.toString().padStart(4, '0')}`,
  categoryId: 1,
  specifications: { weight: '1kg', dimensions: '10x10x10cm' },
  details: JSON.stringify(['Feature 1', 'Feature 2', 'Feature 3']),
  createdAt: new Date('2024-01-01'),
  updatedAt: new Date('2024-01-15'),
  ...overrides
});
 
/**
 * Create a test product with images
 */
export const createProductWithImages = (
  overrides: Partial<TestProduct> = {}
) => {
  const product = createProduct(overrides);
  return {
    ...product,
    images: [
      { id: generateId(), url: '/images/products/product-01.jpg', thumbnailUrl: '/images/products/thumb-01.jpg', order: 0 },
      { id: generateId(), url: '/images/products/product-02.jpg', thumbnailUrl: '/images/products/thumb-02.jpg', order: 1 },
    ]
  };
};
 
/**
 * Create a test category
 */
export const createCategory = (overrides: Partial<TestCategory> = {}): TestCategory => ({
  id: generateId(),
  title: `Category ${idCounter}`,
  description: 'A test category for products.',
  slug: `category-${idCounter}`,
  image: '/images/categories/category-01.jpg',
  parentId: null,
  createdAt: new Date('2024-01-01'),
  updatedAt: new Date('2024-01-01'),
  ...overrides
});
 
/**
 * Create a test user
 */
export const createUser = (overrides: Partial<TestUser> = {}): TestUser => ({
  id: generateId(),
  name: `Test User ${idCounter}`,
  email: `testuser${idCounter}@example.com`,
  password: '$2b$10$hashedpassword', // bcrypt hash placeholder
  image: null,
  role: 'CUSTOMER' as const,
  emailVerified: new Date('2024-01-01'),
  createdAt: new Date('2024-01-01'),
  updatedAt: new Date('2024-01-01'),
  ...overrides
});
 
/**
 * Create an admin user
 */
export const createAdminUser = (overrides: Partial<TestUser> = {}): TestUser =>
  createUser({ role: 'ADMIN' as const, ...overrides });
 
/**
 * Create a test testimonial
 */
export const createTestimonial = (overrides: Partial<TestTestimonial> = {}): TestTestimonial => ({
  id: generateId(),
  review: 'Elite Events provided exceptional service for our corporate event. Highly recommended!',
  authorName: `Customer ${idCounter}`,
  authorRole: 'Event Coordinator',
  authorImage: null,
  isActive: true,
  createdAt: new Date('2024-01-15'),
  ...overrides
});
 
/**
 * Create a test review
 */
export const createReview = (overrides: Partial<TestReview> = {}): TestReview => ({
  id: generateId(),
  productId: 1,
  userId: 1,
  rating: 5,
  comment: 'Excellent product quality and fast delivery!',
  createdAt: new Date('2024-01-10'),
  updatedAt: new Date('2024-01-10'),
  ...overrides
});
 
/**
 * Create a test review with user data
 */
export const createReviewWithUser = (overrides: Partial<TestReview> = {}) => {
  const review = createReview(overrides);
  return {
    ...review,
    user: {
      id: review.userId,
      name: `User ${review.userId}`,
      email: `user${review.userId}@example.com`,
      image: null
    },
    helpful: []
  };
};
 
/**
 * Create a test order
 */
export const createOrder = (overrides: Partial<TestOrder> = {}): TestOrder => ({
  id: generateId(),
  userId: 1,
  status: 'PROCESSING' as const,
  total: 159.98,
  subtotal: 149.98,
  tax: 10.00,
  shippingCost: 0,
  shippingAddressId: 1,
  paymentMethod: 'STRIPE' as const,
  paymentStatus: 'PAID' as const,
  stripePaymentIntentId: `pi_test_${idCounter}`,
  notes: null,
  createdAt: new Date('2024-01-20'),
  updatedAt: new Date('2024-01-20'),
  ...overrides
});
 
/**
 * Create a test order item
 */
export const createOrderItem = (overrides: Partial<TestOrderItem> = {}): TestOrderItem => ({
  id: generateId(),
  orderId: 1,
  productId: 1,
  quantity: 2,
  price: 79.99,
  ...overrides
});
 
/**
 * Create a test cart
 */
export const createCart = (overrides: Partial<TestCart> = {}): TestCart => ({
  id: generateId(),
  userId: 1,
  productId: 1,
  quantity: 1,
  createdAt: new Date('2024-01-15'),
  updatedAt: new Date('2024-01-15'),
  ...overrides
});
 
/**
 * Create a test wishlist item
 */
export const createWishlistItem = (overrides: Partial<TestWishlistItem> = {}): TestWishlistItem => ({
  id: generateId(),
  userId: 1,
  productId: 1,
  createdAt: new Date('2024-01-10'),
  ...overrides
});
 
/**
 * Create a test address
 */
export const createAddress = (overrides: Partial<TestAddress> = {}): TestAddress => ({
  id: generateId(),
  userId: 1,
  firstName: 'John',
  lastName: 'Doe',
  street: '123 Main St',
  city: 'New York',
  state: 'NY',
  postalCode: '10001',
  country: 'USA',
  phone: '+1 555-123-4567',
  isDefault: true,
  createdAt: new Date('2024-01-05'),
  updatedAt: new Date('2024-01-05'),
  ...overrides
});
 
/**
 * Create a test promotion
 */
export const createPromotion = (overrides: Partial<TestPromotion> = {}): TestPromotion => ({
  id: generateId(),
  name: `Test Promotion ${idCounter}`,
  description: 'Get 20% off your next purchase!',
  type: 'PERCENTAGE' as const,
  value: 20,
  code: `PROMO${idCounter}`,
  minPurchase: 50,
  maxDiscount: 100,
  usageLimit: 100,
  usedCount: 0,
  startDate: new Date('2024-01-01'),
  endDate: new Date('2024-12-31'),
  isActive: true,
  createdAt: new Date('2024-01-01'),
  updatedAt: new Date('2024-01-01'),
  ...overrides
});
 
/**
 * Create a test loyalty account
 */
export const createLoyaltyAccount = (overrides: Partial<TestLoyaltyAccount> = {}): TestLoyaltyAccount => ({
  id: generateId(),
  userId: 1,
  points: 500,
  tier: 'SILVER' as const,
  lifetimePoints: 1500,
  createdAt: new Date('2024-01-01'),
  updatedAt: new Date('2024-01-15'),
  ...overrides
});
 
/**
 * Create a test support ticket
 */
export const createSupportTicket = (overrides: Partial<TestSupportTicket> = {}): TestSupportTicket => ({
  id: generateId(),
  userId: 1,
  subject: 'Help with my order',
  description: 'I need assistance tracking my recent order.',
  status: 'OPEN' as const,
  priority: 'MEDIUM' as const,
  category: 'ORDER' as const,
  assignedTo: null,
  createdAt: new Date('2024-01-20'),
  updatedAt: new Date('2024-01-20'),
  ...overrides
});
 
/**
 * Reset all factory counters - call in beforeEach
 */
export { resetIdCounter };
 
/**
 * Create multiple items using a factory
 */
export const createMany = <T>(factory: (overrides?: Partial<T>) => T, count: number): T[] => {
  return Array.from({ length: count }, () => factory());
};