test: assert recipient-filter query clauses in NotificationsService.create

This commit is contained in:
Bastian Wagner
2026-08-04 18:42:03 +02:00
parent 97b0a5c19a
commit 273c25eccb

View File

@@ -32,9 +32,8 @@ describe('NotificationsService', () => {
describe('create', () => {
it('does nothing when the team has no other active members with a login', async () => {
playerRepository.createQueryBuilder.mockReturnValue(
chain({ getRawMany: jest.fn().mockResolvedValue([]) }),
);
const playerQuery = chain({ getRawMany: jest.fn().mockResolvedValue([]) });
playerRepository.createQueryBuilder.mockReturnValue(playerQuery);
await service.create({
teamId: 10,
@@ -43,14 +42,18 @@ describe('NotificationsService', () => {
payload: { playerId: 1, playerName: 'Ada Lovelace' },
});
expect(playerQuery.where).toHaveBeenCalledWith('player.teamId = :teamId', { teamId: 10 });
expect(playerQuery.andWhere).toHaveBeenCalledWith('player.active = :active', { active: true });
expect(playerQuery.andWhere).toHaveBeenCalledWith('player.userId IS NOT NULL');
expect(playerQuery.andWhere).toHaveBeenCalledWith('player.userId != :actorUserId', { actorUserId: 5 });
expect(notificationRepository.save).not.toHaveBeenCalled();
expect(recipientRepository.insert).not.toHaveBeenCalled();
});
it('creates one notification and fans it out to every recipient', async () => {
playerRepository.createQueryBuilder.mockReturnValue(
chain({ getRawMany: jest.fn().mockResolvedValue([{ userId: 7 }, { userId: 8 }]) }),
);
const playerQuery = chain({ getRawMany: jest.fn().mockResolvedValue([{ userId: 7 }, { userId: 8 }]) });
playerRepository.createQueryBuilder.mockReturnValue(playerQuery);
notificationRepository.save.mockResolvedValue({ id: 99 });
await service.create({
@@ -60,6 +63,11 @@ describe('NotificationsService', () => {
payload: { playerId: 1, playerName: 'Ada Lovelace' },
});
expect(playerQuery.where).toHaveBeenCalledWith('player.teamId = :teamId', { teamId: 10 });
expect(playerQuery.andWhere).toHaveBeenCalledWith('player.active = :active', { active: true });
expect(playerQuery.andWhere).toHaveBeenCalledWith('player.userId IS NOT NULL');
expect(playerQuery.andWhere).toHaveBeenCalledWith('player.userId != :actorUserId', { actorUserId: 5 });
expect(notificationRepository.save).toHaveBeenCalledWith(
expect.objectContaining({
team: { id: 10 },