Skip to content

Commit 3974f43

Browse files
committed
services test
1 parent 8665c53 commit 3974f43

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { getRepositoryToken } from '@nestjs/typeorm';
3+
import { Repository } from 'typeorm';
4+
import { BranchesService } from './branches.service';
5+
import { Branch } from './entities/branch.entity';
6+
import { CreateBranchDto } from './dto/create-branch.dto';
7+
import { UpdateBranchDto } from './dto/update-branch.dto';
8+
9+
describe('BranchesService', () => {
10+
let service: BranchesService;
11+
let repository: Repository<Branch>;
12+
13+
const mockRepository = {
14+
create: jest.fn(),
15+
save: jest.fn(),
16+
find: jest.fn(),
17+
findOne: jest.fn(),
18+
remove: jest.fn(),
19+
};
20+
21+
beforeEach(async () => {
22+
const module: TestingModule = await Test.createTestingModule({
23+
providers: [
24+
BranchesService,
25+
{
26+
provide: getRepositoryToken(Branch),
27+
useValue: mockRepository,
28+
},
29+
],
30+
}).compile();
31+
32+
service = module.get<BranchesService>(BranchesService);
33+
repository = module.get<Repository<Branch>>(getRepositoryToken(Branch));
34+
jest.clearAllMocks();
35+
});
36+
37+
it('should be defined', () => {
38+
expect(service).toBeDefined();
39+
expect(repository).toBeDefined();
40+
});
41+
42+
describe('create', () => {
43+
it('should create a new branch', async () => {
44+
const dto: CreateBranchDto = {
45+
name: 'Main Branch',
46+
address: '123 Street',
47+
companyId: 1,
48+
};
49+
50+
const expected: Branch = {
51+
id: 1,
52+
...dto,
53+
createdAt: new Date(),
54+
updatedAt: new Date(),
55+
} as Branch;
56+
57+
mockRepository.create.mockReturnValue(expected);
58+
mockRepository.save.mockResolvedValue(expected);
59+
60+
const result = await service.create(dto);
61+
expect(mockRepository.create).toHaveBeenCalledWith(dto);
62+
expect(mockRepository.save).toHaveBeenCalledWith(expected);
63+
expect(result).toEqual(expected);
64+
});
65+
});
66+
67+
describe('findAll', () => {
68+
it('should return an array of branches', async () => {
69+
const expected: Branch[] = [
70+
{
71+
id: 1,
72+
name: 'Main Branch',
73+
address: '123 Street',
74+
companyId: 1,
75+
createdAt: new Date(),
76+
updatedAt: new Date(),
77+
} as Branch,
78+
];
79+
mockRepository.find.mockResolvedValue(expected);
80+
const result = await service.findAll();
81+
expect(mockRepository.find).toHaveBeenCalled();
82+
expect(result).toEqual(expected);
83+
});
84+
});
85+
86+
describe('findOne', () => {
87+
it('should return a branch when found', async () => {
88+
const expected: Branch = {
89+
id: 1,
90+
name: 'Main Branch',
91+
address: '123 Street',
92+
companyId: 1,
93+
createdAt: new Date(),
94+
updatedAt: new Date(),
95+
} as Branch;
96+
mockRepository.findOne.mockResolvedValue(expected);
97+
const result = await service.findOne(1);
98+
expect(mockRepository.findOne).toHaveBeenCalledWith({ where: { id: 1 } });
99+
expect(result).toEqual(expected);
100+
});
101+
102+
it('should throw when branch not found', async () => {
103+
mockRepository.findOne.mockResolvedValue(undefined);
104+
await expect(service.findOne(999)).rejects.toThrow('Branch 999 not found');
105+
});
106+
});
107+
108+
describe('update', () => {
109+
it('should merge and save updates', async () => {
110+
const existing: Branch = {
111+
id: 1,
112+
name: 'Main Branch',
113+
address: '123 Street',
114+
companyId: 1,
115+
createdAt: new Date(),
116+
updatedAt: new Date(),
117+
} as Branch;
118+
const updates: UpdateBranchDto = { address: '456 Ave' };
119+
const saved = { ...existing, ...updates } as Branch;
120+
mockRepository.findOne.mockResolvedValue(existing);
121+
mockRepository.save.mockResolvedValue(saved);
122+
const result = await service.update(1, updates);
123+
expect(mockRepository.save).toHaveBeenCalledWith(saved);
124+
expect(result).toEqual(saved);
125+
});
126+
});
127+
128+
describe('remove', () => {
129+
it('should remove the branch', async () => {
130+
const existing: Branch = {
131+
id: 1,
132+
name: 'Main Branch',
133+
address: '123 Street',
134+
companyId: 1,
135+
createdAt: new Date(),
136+
updatedAt: new Date(),
137+
} as Branch;
138+
mockRepository.findOne.mockResolvedValue(existing);
139+
mockRepository.remove.mockResolvedValue(existing);
140+
await service.remove(1);
141+
expect(mockRepository.remove).toHaveBeenCalledWith(existing);
142+
});
143+
});
144+
});
145+
146+

0 commit comments

Comments
 (0)