import { Test, TestingModule } from '@nestjs/testing'; import { ParentsController } from './parents.controller'; import { ParentsService } from './parents.service'; import { UserService } from '../user/user.service'; import { AuthService } from '../auth/auth.service'; import { AuthGuard } from 'src/common/guards/auth.guard'; import { RolesGuard } from 'src/common/guards/roles.guard'; import { StatutUtilisateurType } from 'src/entities/users.entity'; describe('ParentsController', () => { let controller: ParentsController; const authServiceMock = { createParentDossierStaff: jest.fn(), addCoParentStaff: jest.fn(), }; const parentsServiceMock = { listerCouplesGarde: jest.fn(), definirCoupleGardeCourant: jest.fn(), }; const userServiceMock = {}; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [ParentsController], providers: [ { provide: ParentsService, useValue: parentsServiceMock }, { provide: UserService, useValue: userServiceMock }, { provide: AuthService, useValue: authServiceMock }, ], }) .overrideGuard(AuthGuard) .useValue({ canActivate: () => true }) .overrideGuard(RolesGuard) .useValue({ canActivate: () => true }) .compile(); controller = module.get(ParentsController); jest.clearAllMocks(); }); it('should be defined', () => { expect(controller).toBeDefined(); }); it('listerCouplesGarde délègue au service (#168)', async () => { parentsServiceMock.listerCouplesGarde.mockResolvedValue({ couples: [], couple_courant_id: null, }); const res = await controller.listerCouplesGarde('parent-uuid'); expect(parentsServiceMock.listerCouplesGarde).toHaveBeenCalledWith('parent-uuid'); expect(res.couples).toEqual([]); }); it('definirCoupleGardeCourant délègue au service (#168)', async () => { parentsServiceMock.definirCoupleGardeCourant.mockResolvedValue({ couples: [{ id: 'pl-1', courant: true }], couple_courant_id: 'pl-1', }); const res = await controller.definirCoupleGardeCourant('parent-uuid', { couple_id: 'pl-1', }); expect(parentsServiceMock.definirCoupleGardeCourant).toHaveBeenCalledWith( 'parent-uuid', 'pl-1', ); expect(res.couple_courant_id).toBe('pl-1'); }); it('createDossier delegates to authService.createParentDossierStaff with CGU accepted', async () => { authServiceMock.createParentDossierStaff.mockResolvedValue({ message: 'Dossier famille créé et validé. Un e-mail de création de mot de passe a été envoyé.', parent_user_id: 'p1', co_parent_user_id: 'p2', enfant_ids: ['e1'], statut: StatutUtilisateurType.ACTIF, numero_dossier: '2026-000043', }); const body = { email: 'parent.staff@test.fr', prenom: 'Claire', nom: 'MARTIN', telephone: '0689567890', enfants: [ { prenom: 'Emma', nom: 'MARTIN', date_naissance: '2023-02-15', genre: 'F', }, ], }; const res = await controller.createDossier(body as any); expect(authServiceMock.createParentDossierStaff).toHaveBeenCalledWith( expect.objectContaining({ email: body.email, acceptation_cgu: true, acceptation_privacy: true, }), ); expect(res.numero_dossier).toBe('2026-000043'); expect(res.parent_user_id).toBe('p1'); expect(res.co_parent_user_id).toBe('p2'); expect(res.enfant_ids).toEqual(['e1']); expect(res.statut).toBe(StatutUtilisateurType.ACTIF); }); it('addCoParent delegates to authService.addCoParentStaff', async () => { authServiceMock.addCoParentStaff.mockResolvedValue({ message: 'ok', numero_dossier: '2026-000043', parent_user_id: 'p1', co_parent_user_id: 'p2', statut: StatutUtilisateurType.ACTIF, }); const body = { email: 'coparent@test.fr', prenom: 'Thomas', nom: 'MARTIN', telephone: '0678456789', meme_adresse: true, }; const res = await controller.addCoParent('p1', body as any); expect(authServiceMock.addCoParentStaff).toHaveBeenCalledWith('p1', body); expect(res.co_parent_user_id).toBe('p2'); expect(res.statut).toBe(StatutUtilisateurType.ACTIF); }); });