-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(newsletter): user newsletter resubscription
- Loading branch information
1 parent
1d974e0
commit 9d4594c
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.') | ||
); | ||
}); | ||
}); | ||
}); |