-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { DataSource, Repository } from 'typeorm'; | ||
import { CreateServicesSeed } from './create-services.seed'; | ||
import { Service } from '../../services/entities/service.entity'; | ||
|
||
describe('CreateServicesSeed', () => { | ||
let seed: CreateServicesSeed; | ||
let mockDataSource: Partial<DataSource>; | ||
let mockServiceRepository: Partial<Repository<Service>>; | ||
|
||
beforeEach(() => { | ||
mockServiceRepository = { | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
}; | ||
|
||
mockDataSource = { | ||
getRepository: jest.fn().mockReturnValue(mockServiceRepository), | ||
}; | ||
|
||
seed = new CreateServicesSeed(); | ||
}); | ||
|
||
it('should create services if they do not exist', async () => { | ||
// Mock findOne to return null (service doesn't exist) | ||
(mockServiceRepository.findOne as jest.Mock).mockResolvedValue(null); | ||
|
||
await seed.run(mockDataSource as DataSource); | ||
|
||
// Should try to find each service | ||
expect(mockServiceRepository.findOne).toHaveBeenCalledTimes(4); | ||
|
||
// Verify save was called for each service | ||
expect(mockServiceRepository.save).toHaveBeenCalledTimes(4); | ||
|
||
// Verify the correct services were saved | ||
expect(mockServiceRepository.save).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
name: 'Standard Klipp', | ||
description: expect.any(String), | ||
duration: 20, | ||
price: 299, | ||
}) | ||
); | ||
|
||
expect(mockServiceRepository.save).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
name: 'Styling Klipp', | ||
description: expect.any(String), | ||
duration: 30, | ||
price: 399, | ||
}) | ||
); | ||
|
||
expect(mockServiceRepository.save).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
name: 'Skjegg Trim', | ||
description: expect.any(String), | ||
duration: 15, | ||
price: 199, | ||
}) | ||
); | ||
|
||
expect(mockServiceRepository.save).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
name: 'Full Service', | ||
description: expect.any(String), | ||
duration: 45, | ||
price: 549, | ||
}) | ||
); | ||
}); | ||
|
||
it('should not create services that already exist', async () => { | ||
// Mock findOne to return an existing service | ||
(mockServiceRepository.findOne as jest.Mock).mockResolvedValue({ | ||
id: 1, | ||
name: 'Standard Klipp', | ||
description: 'Existing service', | ||
duration: 20, | ||
price: 299, | ||
}); | ||
|
||
await seed.run(mockDataSource as DataSource); | ||
|
||
// Should try to find each service | ||
expect(mockServiceRepository.findOne).toHaveBeenCalledTimes(4); | ||
|
||
// Should not save any services since they all exist | ||
expect(mockServiceRepository.save).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle database errors gracefully', async () => { | ||
// Mock findOne to throw an error | ||
const dbError = new Error('Database connection error'); | ||
(mockServiceRepository.findOne as jest.Mock).mockRejectedValue(dbError); | ||
|
||
await expect(seed.run(mockDataSource as DataSource)).rejects.toThrow(dbError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Employee } from './employee.entity'; | ||
import { User } from '../../users/entities/user.entity'; | ||
import { Service } from '../../services/entities/service.entity'; | ||
|
||
describe('Employee Entity', () => { | ||
let employee: Employee; | ||
|
||
beforeEach(() => { | ||
employee = new Employee(); | ||
}); | ||
|
||
it('should create an employee instance', () => { | ||
expect(employee).toBeDefined(); | ||
expect(employee).toBeInstanceOf(Employee); | ||
}); | ||
|
||
it('should have correct properties', () => { | ||
// Create mock data | ||
const mockUser = new User(); | ||
const mockService = new Service(); | ||
const mockAvailability = { | ||
monday: [{ start: '09:00', end: '17:00' }], | ||
tuesday: [{ start: '09:00', end: '17:00' }], | ||
}; | ||
const mockSpecializations = ['Haircut', 'Styling']; | ||
const mockDate = new Date(); | ||
|
||
// Set properties | ||
employee.id = 'test-id'; | ||
employee.user = mockUser; | ||
employee.services = [mockService]; | ||
employee.availability = mockAvailability; | ||
employee.specializations = mockSpecializations; | ||
employee.isActive = true; | ||
employee.createdAt = mockDate; | ||
employee.updatedAt = mockDate; | ||
|
||
// Verify properties | ||
expect(employee.id).toBe('test-id'); | ||
expect(employee.user).toBe(mockUser); | ||
expect(employee.services).toEqual([mockService]); | ||
expect(employee.availability).toEqual(mockAvailability); | ||
expect(employee.specializations).toEqual(mockSpecializations); | ||
expect(employee.isActive).toBe(true); | ||
expect(employee.createdAt).toBe(mockDate); | ||
expect(employee.updatedAt).toBe(mockDate); | ||
}); | ||
|
||
it('should handle empty availability', () => { | ||
employee.availability = {}; | ||
expect(employee.availability).toEqual({}); | ||
}); | ||
|
||
it('should handle empty specializations', () => { | ||
expect(employee.specializations).toEqual([]); | ||
}); | ||
|
||
it('should handle empty services', () => { | ||
expect(employee.services).toBeUndefined(); | ||
employee.services = []; | ||
expect(employee.services).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Service } from './service.entity'; | ||
import { Employee } from '../../employees/entities/employee.entity'; | ||
|
||
describe('Service Entity', () => { | ||
let service: Service; | ||
|
||
beforeEach(() => { | ||
service = new Service(); | ||
}); | ||
|
||
it('should create a service instance', () => { | ||
expect(service).toBeDefined(); | ||
expect(service).toBeInstanceOf(Service); | ||
}); | ||
|
||
it('should have correct properties', () => { | ||
// Create mock data | ||
const mockEmployee = new Employee(); | ||
const mockDate = new Date(); | ||
|
||
// Set properties | ||
service.id = 'test-id'; | ||
service.name = 'Haircut'; | ||
service.description = 'Basic haircut service'; | ||
service.duration = 30; | ||
service.price = 299.99; | ||
service.isActive = true; | ||
service.employees = [mockEmployee]; | ||
service.createdAt = mockDate; | ||
service.updatedAt = mockDate; | ||
|
||
// Verify properties | ||
expect(service.id).toBe('test-id'); | ||
expect(service.name).toBe('Haircut'); | ||
expect(service.description).toBe('Basic haircut service'); | ||
expect(service.duration).toBe(30); | ||
expect(service.price).toBe(299.99); | ||
expect(service.isActive).toBe(true); | ||
expect(service.employees).toEqual([mockEmployee]); | ||
expect(service.createdAt).toBe(mockDate); | ||
expect(service.updatedAt).toBe(mockDate); | ||
}); | ||
|
||
it('should handle empty employees array', () => { | ||
expect(service.employees).toBeUndefined(); | ||
service.employees = []; | ||
expect(service.employees).toEqual([]); | ||
}); | ||
|
||
it('should handle decimal price values', () => { | ||
service.price = 299.99; | ||
expect(service.price).toBe(299.99); | ||
}); | ||
|
||
it('should handle duration in minutes', () => { | ||
service.duration = 45; | ||
expect(service.duration).toBe(45); | ||
}); | ||
}); |