From 273c25eccb0d06b6e1866eb3833457881ab2af56 Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Tue, 4 Aug 2026 18:42:03 +0200 Subject: [PATCH] test: assert recipient-filter query clauses in NotificationsService.create --- .../notifications.service.spec.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/myteamwallet_backend/src/notifications/notifications.service.spec.ts b/myteamwallet_backend/src/notifications/notifications.service.spec.ts index 4d0bf1f..08e3ef0 100644 --- a/myteamwallet_backend/src/notifications/notifications.service.spec.ts +++ b/myteamwallet_backend/src/notifications/notifications.service.spec.ts @@ -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 },