Skip to content

Commit

Permalink
test(newsletter): user newsletter resubscription
Browse files Browse the repository at this point in the history
  • Loading branch information
darionnuel committed Mar 1, 2025
1 parent 1d974e0 commit 9d4594c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ResubscribeNewsletterDto {
email?: string;

@IsNotEmpty()
validateUserIdentifier() {
validateUserIdentifier?() {
if (!this.id && !this.email) {
throw new Error('Either userId or email must be provided.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,38 @@ describe('NewsletterService', () => {
expect(repository.findOne).toHaveBeenCalledWith({ where: { email } });
});
});

describe('resubscribe', () => {
it('should successfully resubscribe a user who was unsubscribed', async () => {
const dto = { email: '[email protected]' };
const mockSubscription = {
id: '1',
email: '[email protected]',
status: 'unsubscribed',
} as NewsletterSubscription;

jest.spyOn(repository, 'findOne').mockResolvedValue(mockSubscription);
jest.spyOn(repository, 'save').mockResolvedValue({ ...mockSubscription, status: 'active' });

const result = await service.resubscribe(dto);

expect(result).toEqual({ message: 'Successfully resubscribed to the newsletter.' });
expect(repository.findOne).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.arrayContaining([expect.objectContaining({ email: dto.email })]),
})
);
expect(repository.save).toHaveBeenCalledWith(expect.objectContaining({ status: 'active' }));
});

it('should throw NotFoundException if user is not found or was never subscribed', async () => {
const dto = { email: '[email protected]' };

jest.spyOn(repository, 'findOne').mockResolvedValue(null);

await expect(service.resubscribe(dto)).rejects.toThrow(
new NotFoundException('User not found or not unsubscribed.')
);
});
});
});

0 comments on commit 9d4594c

Please sign in to comment.