import { NotFoundException } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; import { AssistantesMaternellesService } from './assistantes_maternelles.service'; import { AssistanteMaternelle } from 'src/entities/assistantes_maternelles.entity'; import { Users } from 'src/entities/users.entity'; import { AmChildren } from 'src/entities/am_children.entity'; import { Children } from 'src/entities/children.entity'; import { ParentsChildren } from 'src/entities/parents_children.entity'; describe('AssistantesMaternellesService — couples de garde (#171)', () => { let service: AssistantesMaternellesService; const amRepo = { findOne: jest.fn(), update: jest.fn() }; const usersRepo = {}; const amChildrenRepo = { find: jest.fn(), findOne: jest.fn() }; const childrenRepo = {}; const parentsChildrenRepo = { find: jest.fn() }; beforeEach(async () => { jest.clearAllMocks(); const module: TestingModule = await Test.createTestingModule({ providers: [ AssistantesMaternellesService, { provide: getRepositoryToken(AssistanteMaternelle), useValue: amRepo }, { provide: getRepositoryToken(Users), useValue: usersRepo }, { provide: getRepositoryToken(AmChildren), useValue: amChildrenRepo }, { provide: getRepositoryToken(Children), useValue: childrenRepo }, { provide: getRepositoryToken(ParentsChildren), useValue: parentsChildrenRepo, }, ], }).compile(); service = module.get(AssistantesMaternellesService); }); it('should be defined', () => { expect(service).toBeDefined(); }); describe('listerCouplesGarde', () => { it('retourne vide si aucun placement', async () => { amRepo.findOne.mockResolvedValue({ user_id: 'am-1', id_placement_garde_courant: null, }); amChildrenRepo.find.mockResolvedValue([]); const res = await service.listerCouplesGarde('am-1'); expect(res).toEqual({ couples: [], couple_courant_id: null }); expect(parentsChildrenRepo.find).not.toHaveBeenCalled(); }); it('mappe placements + parents et marque le courant', async () => { amRepo.findOne.mockResolvedValue({ user_id: 'am-1', id_placement_garde_courant: 'pl-2', }); amChildrenRepo.find.mockResolvedValue([ { id: 'pl-1', enfantId: 'e1', child: { id: 'e1', first_name: 'Léa', last_name: 'M', photo_url: null }, }, { id: 'pl-2', enfantId: 'e2', child: { id: 'e2', first_name: 'Emma', last_name: 'M', photo_url: null }, }, ]); parentsChildrenRepo.find.mockResolvedValue([ { parentId: 'p1', enfantId: 'e1', parent: { user: { prenom: 'Claire', nom: 'Martin', photo_url: null } }, }, { parentId: 'p2', enfantId: 'e1', parent: { user: { prenom: 'Thomas', nom: 'Martin', photo_url: null } }, }, { parentId: 'p1', enfantId: 'e2', parent: { user: { prenom: 'Claire', nom: 'Martin', photo_url: null } }, }, ]); const res = await service.listerCouplesGarde('am-1'); expect(res.couples).toHaveLength(2); expect(res.couple_courant_id).toBe('pl-2'); expect(res.couples[1].courant).toBe(true); expect(res.couples[0].parents).toHaveLength(2); expect(res.couples[0].parents[0].prenom).toBe('Claire'); expect(res.couples[0].enfant.prenom).toBe('Léa'); }); it('404 si AM inconnue', async () => { amRepo.findOne.mockResolvedValue(null); await expect(service.listerCouplesGarde('x')).rejects.toBeInstanceOf( NotFoundException, ); }); }); describe('definirCoupleGardeCourant', () => { it('persiste si le placement appartient à l’AM', async () => { amRepo.findOne.mockResolvedValue({ user_id: 'am-1', id_placement_garde_courant: null, }); amChildrenRepo.findOne.mockResolvedValue({ id: 'pl-1', amId: 'am-1', enfantId: 'e1', }); amRepo.update.mockResolvedValue({ affected: 1 }); amChildrenRepo.find.mockResolvedValue([ { id: 'pl-1', enfantId: 'e1', child: { id: 'e1', first_name: 'Léa', last_name: null, photo_url: null }, }, ]); parentsChildrenRepo.find.mockResolvedValue([ { parentId: 'p1', enfantId: 'e1', parent: { user: { prenom: 'Claire', nom: 'M', photo_url: null } }, }, ]); const res = await service.definirCoupleGardeCourant('am-1', 'pl-1'); expect(amRepo.update).toHaveBeenCalledWith( { user_id: 'am-1' }, { id_placement_garde_courant: 'pl-1' }, ); expect(res.couple_courant_id).toBe('pl-1'); expect(res.couples[0].courant).toBe(true); }); it('404 si placement d’une autre AM', async () => { amRepo.findOne.mockResolvedValue({ user_id: 'am-1' }); amChildrenRepo.findOne.mockResolvedValue(null); await expect( service.definirCoupleGardeCourant('am-1', 'pl-x'), ).rejects.toBeInstanceOf(NotFoundException); }); }); });