153 lines
5.2 KiB
TypeScript
153 lines
5.2 KiB
TypeScript
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||
import { Test, TestingModule } from '@nestjs/testing';
|
||
import { getRepositoryToken } from '@nestjs/typeorm';
|
||
import { ParentsService } from './parents.service';
|
||
import { Parents } from 'src/entities/parents.entity';
|
||
import { Users } from 'src/entities/users.entity';
|
||
import { DossierFamille } from 'src/entities/dossier_famille.entity';
|
||
import { ParentsChildren } from 'src/entities/parents_children.entity';
|
||
import { AmChildren } from 'src/entities/am_children.entity';
|
||
|
||
describe('ParentsService — couples de garde (#168)', () => {
|
||
let service: ParentsService;
|
||
|
||
const parentsRepository = {
|
||
findOne: jest.fn(),
|
||
update: jest.fn(),
|
||
};
|
||
const parentsChildrenRepository = {
|
||
find: jest.fn(),
|
||
findOne: jest.fn(),
|
||
};
|
||
const amChildrenRepository = {
|
||
find: jest.fn(),
|
||
findOne: jest.fn(),
|
||
};
|
||
|
||
beforeEach(async () => {
|
||
jest.clearAllMocks();
|
||
const module: TestingModule = await Test.createTestingModule({
|
||
providers: [
|
||
ParentsService,
|
||
{ provide: getRepositoryToken(Parents), useValue: parentsRepository },
|
||
{ provide: getRepositoryToken(Users), useValue: {} },
|
||
{ provide: getRepositoryToken(DossierFamille), useValue: {} },
|
||
{
|
||
provide: getRepositoryToken(ParentsChildren),
|
||
useValue: parentsChildrenRepository,
|
||
},
|
||
{ provide: getRepositoryToken(AmChildren), useValue: amChildrenRepository },
|
||
],
|
||
}).compile();
|
||
|
||
service = module.get<ParentsService>(ParentsService);
|
||
});
|
||
|
||
it('should be defined', () => {
|
||
expect(service).toBeDefined();
|
||
});
|
||
|
||
describe('listerCouplesGarde', () => {
|
||
it('retourne une liste vide si le parent n’a pas d’enfant', async () => {
|
||
parentsRepository.findOne.mockResolvedValue({
|
||
user_id: 'p1',
|
||
id_placement_garde_courant: null,
|
||
});
|
||
parentsChildrenRepository.find.mockResolvedValue([]);
|
||
|
||
const res = await service.listerCouplesGarde('p1');
|
||
expect(res).toEqual({ couples: [], couple_courant_id: null });
|
||
expect(amChildrenRepository.find).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('mappe les placements actifs en couples et marque le courant', async () => {
|
||
parentsRepository.findOne.mockResolvedValue({
|
||
user_id: 'p1',
|
||
id_placement_garde_courant: 'pl-2',
|
||
});
|
||
parentsChildrenRepository.find.mockResolvedValue([
|
||
{ enfantId: 'e1' },
|
||
{ enfantId: 'e2' },
|
||
]);
|
||
amChildrenRepository.find.mockResolvedValue([
|
||
{
|
||
id: 'pl-1',
|
||
amId: 'am-1',
|
||
child: { id: 'e1', first_name: 'Léo', last_name: 'M', photo_url: null },
|
||
am: { user: { prenom: 'Marie', nom: 'N', photo_url: '/a.jpg' } },
|
||
},
|
||
{
|
||
id: 'pl-2',
|
||
amId: 'am-2',
|
||
child: { id: 'e2', first_name: 'Léa', last_name: 'M', photo_url: null },
|
||
am: { user: { prenom: 'Sophie', nom: 'P', photo_url: null } },
|
||
},
|
||
]);
|
||
|
||
const res = await service.listerCouplesGarde('p1');
|
||
expect(res.couples).toHaveLength(2);
|
||
expect(res.couple_courant_id).toBe('pl-2');
|
||
expect(res.couples[1].courant).toBe(true);
|
||
expect(res.couples[0].am.prenom).toBe('Marie');
|
||
expect(res.couples[0].enfant.prenom).toBe('Léo');
|
||
});
|
||
|
||
it('404 si parent inconnu', async () => {
|
||
parentsRepository.findOne.mockResolvedValue(null);
|
||
await expect(service.listerCouplesGarde('x')).rejects.toBeInstanceOf(
|
||
NotFoundException,
|
||
);
|
||
});
|
||
});
|
||
|
||
describe('definirCoupleGardeCourant', () => {
|
||
it('persiste le couple si l’enfant est rattaché au parent', async () => {
|
||
parentsRepository.findOne.mockResolvedValue({
|
||
user_id: 'p1',
|
||
id_placement_garde_courant: null,
|
||
});
|
||
amChildrenRepository.findOne.mockResolvedValue({
|
||
id: 'pl-1',
|
||
enfantId: 'e1',
|
||
date_fin: null,
|
||
});
|
||
parentsChildrenRepository.findOne.mockResolvedValue({
|
||
parentId: 'p1',
|
||
enfantId: 'e1',
|
||
});
|
||
parentsRepository.update.mockResolvedValue({ affected: 1 });
|
||
// second call via listerCouplesGarde
|
||
parentsChildrenRepository.find.mockResolvedValue([{ enfantId: 'e1' }]);
|
||
amChildrenRepository.find.mockResolvedValue([
|
||
{
|
||
id: 'pl-1',
|
||
amId: 'am-1',
|
||
child: { id: 'e1', first_name: 'Léo', last_name: null, photo_url: null },
|
||
am: { user: { prenom: 'Marie', nom: null, photo_url: null } },
|
||
},
|
||
]);
|
||
|
||
const res = await service.definirCoupleGardeCourant('p1', 'pl-1');
|
||
expect(parentsRepository.update).toHaveBeenCalledWith(
|
||
{ user_id: 'p1' },
|
||
{ id_placement_garde_courant: 'pl-1' },
|
||
);
|
||
expect(res.couple_courant_id).toBe('pl-1');
|
||
expect(res.couples[0].courant).toBe(true);
|
||
});
|
||
|
||
it('400 si le couple n’appartient pas au parent', async () => {
|
||
parentsRepository.findOne.mockResolvedValue({ user_id: 'p1' });
|
||
amChildrenRepository.findOne.mockResolvedValue({
|
||
id: 'pl-1',
|
||
enfantId: 'e99',
|
||
});
|
||
parentsChildrenRepository.findOne.mockResolvedValue(null);
|
||
|
||
await expect(
|
||
service.definirCoupleGardeCourant('p1', 'pl-1'),
|
||
).rejects.toBeInstanceOf(BadRequestException);
|
||
});
|
||
});
|
||
});
|