Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit ea42d4f

Browse files
committed
test: Enhance unit tests for OrganizationController and OrganizationService by adding JWT guard mock and improving database mock setup
1 parent 6ffdc7e commit ea42d4f

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/organization/__tests__/organization.controller.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AddOwnerDto } from '../dto/add-owner.dto';
1111
import { AddHelpTypeDto } from '../dto/add-help-type.dto';
1212
import { CreateOrganizationsBulkDto } from '../dto/create-organizations-bulk.dto';
1313
import { Response } from 'express';
14+
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
1415

1516
describe('OrganizationController', () => {
1617
let controller: OrganizationController;
@@ -133,11 +134,31 @@ describe('OrganizationController', () => {
133134
useValue: mockS3Service,
134135
},
135136
],
136-
}).compile();
137+
})
138+
.overrideGuard(JwtAuthGuard)
139+
.useValue({
140+
canActivate: vi.fn(() => true),
141+
})
142+
.compile();
137143

138144
controller = module.get<OrganizationController>(OrganizationController);
139145
service = module.get<OrganizationService>(OrganizationService);
140146
s3Service = module.get<S3Service>(S3Service);
147+
148+
// Убеждаемся, что контроллер получил зависимости
149+
expect(controller).toBeDefined();
150+
expect(service).toBe(mockService);
151+
expect(s3Service).toBe(mockS3Service);
152+
153+
// Проверяем, что зависимости внедрены в контроллер через рефлексию
154+
// В NestJS приватные поля доступны через рефлексию
155+
const controllerService = (controller as any).organizationService;
156+
const controllerS3Service = (controller as any).s3Service;
157+
158+
if (!controllerService || !controllerS3Service) {
159+
// Если зависимости не внедрены, создаем контроллер вручную
160+
controller = new OrganizationController(mockService, mockS3Service);
161+
}
141162
});
142163

143164
describe('create', () => {

src/organization/__tests__/organization.service.spec.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,19 @@ describe('OrganizationService', () => {
974974
},
975975
];
976976

977-
setupDbMock([[mockUser], [mockOrganizationType], [mockHelpType], [mockCity], [mockCity]]);
977+
// Порядок вызовов:
978+
// 1. user
979+
// 2. findCityByName('Москва') - точное совпадение (найдено)
980+
// 3. organizationTypes
981+
// 4. helpTypes
982+
// 5. citiesData для координат
983+
setupDbMock([
984+
[mockUser], // 1. проверка пользователя
985+
[mockCity], // 2. findCityByName - точное совпадение (для cityId = 0)
986+
[mockOrganizationType], // 3. проверка типов организаций
987+
[mockHelpType], // 4. проверка видов помощи
988+
[mockCity], // 5. получение данных городов для координат
989+
]);
978990
mockRepository.createMany.mockResolvedValue([mockOrganization]);
979991
mockRepository.addOwnersToOrganizations.mockResolvedValue(undefined);
980992
mockRepository.addHelpTypes.mockResolvedValue(undefined);
@@ -995,16 +1007,14 @@ describe('OrganizationService', () => {
9951007
},
9961008
];
9971009

998-
// Порядок: user, cities (для cityId - пусто, т.к. cityId = 0),
999-
// findCityByName - точное совпадение (пусто), частичное совпадение (пусто),
1000-
// organizationTypes, helpTypes, cities (для координат - не доходит)
1010+
// Порядок: user, findCityByName - точное совпадение (не найдено),
1011+
// findCityByName - частичное совпадение (не найдено), затем ошибка
1012+
// При cityId = 0 проверка городов по ID не выполняется (cityIds.size = 0)
10011013
setupDbMock([
10021014
[mockUser], // 1. проверка пользователя
1003-
[], // 2. cities для cityId (пусто, т.к. cityId = 0)
1004-
[], // 3. findCityByName - точное совпадение (не найдено)
1005-
[], // 4. findCityByName - частичное совпадение (не найдено)
1006-
[mockOrganizationType], // 5. проверка типов организаций
1007-
[mockHelpType], // 6. проверка видов помощи
1015+
[], // 2. findCityByName - точное совпадение (не найдено)
1016+
[], // 3. findCityByName - частичное совпадение (не найдено)
1017+
// Ошибка выбрасывается здесь, дальше не доходит
10081018
]);
10091019

10101020
await expect(service.createMany(dtoWithUnknownCity, 1)).rejects.toThrow(

0 commit comments

Comments
 (0)