import { BadRequestException } from '@nestjs/common'; import { Repository } from 'typeorm'; import { OrganizationsService } from './organizations.service'; import { Organization } from '../entities/organization.entity'; describe('OrganizationsService — host organization rules', () => { let repo: jest.Mocked< Pick, 'findOne' | 'find' | 'count' | 'create' | 'save' | 'update'> >; let service: OrganizationsService; beforeEach(() => { repo = { findOne: jest.fn(), find: jest.fn(), count: jest.fn(), create: jest.fn(), save: jest.fn(), update: jest.fn(), } as any; service = new OrganizationsService(repo as Repository); }); it('does not allow the host organization to be archived', async () => { repo.findOne.mockResolvedValue({ id: 1, name: '本机构', isHost: true }); await expect(service.remove(1)).rejects.toBeInstanceOf(BadRequestException); expect(repo.update).not.toHaveBeenCalled(); }); it('findOptions returns only id, name, isHost for active organizations', async () => { const orgs = [ { id: 1, name: '本机构', isHost: true }, { id: 2, name: '分校', isHost: false }, ]; repo.find.mockResolvedValue(orgs as Organization[]); const result = await service.findOptions(); expect(repo.find).toHaveBeenCalledWith({ select: ['id', 'name', 'isHost'], where: { status: 'active' }, order: { isHost: 'DESC', name: 'ASC' }, }); expect(result).toEqual(orgs); }); });