-
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.
Merge pull request #1292 from hngprojects/feat/unsubscribe-newsletter
feat: unsubscribe newsletter
- Loading branch information
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/modules/newsletter-subscription/dto/unsubscribe-newsletter.dto.ts
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsEmail } from 'class-validator'; | ||
|
||
export class UnsubscribeNewsletterDto { | ||
@ApiProperty({ description: 'Email of the user to unsubscribe' }) | ||
@IsEmail() | ||
email: string; | ||
} |
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
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 |
---|---|---|
|
@@ -146,4 +146,47 @@ describe('NewsletterService', () => { | |
await expect(service.restore(id)).rejects.toThrow(NotFoundException); | ||
}); | ||
}); | ||
|
||
describe('unsubscribe', () => { | ||
it('should successfully unsubscribe a user', async () => { | ||
const email = '[email protected]'; | ||
const mockSubscription = { id: '1', email, isUnsubscribed: false } as NewsletterSubscription; | ||
|
||
// Mock `findOne` to return a subscribed user | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(mockSubscription); | ||
// Mock `save` to return updated object | ||
jest.spyOn(repository, 'save').mockImplementation( | ||
async sub => | ||
({ | ||
...sub, | ||
isUnsubscribed: true, | ||
email: sub.email, | ||
deletedAt: sub.deletedAt || new Date(), | ||
id: sub.id, | ||
created_at: sub.created_at || new Date(), | ||
updated_at: new Date(), | ||
}) as NewsletterSubscription | ||
); | ||
|
||
const result = await service.unsubscribe(email); | ||
expect(result).toEqual({ message: `Email ${email} has been unsubscribed successfully` }); | ||
|
||
// Ensure `isUnsubscribed` was updated | ||
expect(repository.save).toHaveBeenCalledWith(expect.objectContaining({ isUnsubscribed: true })); | ||
}); | ||
|
||
it('should throw NotFoundException if email is not found', async () => { | ||
const email = '[email protected]'; | ||
|
||
// Mock `findOne` to return null | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(null); | ||
|
||
// Expecting an error | ||
await expect(service.unsubscribe(email)).rejects.toThrow( | ||
new NotFoundException(`Email ${email} not found in the subscription list`) | ||
); | ||
|
||
expect(repository.findOne).toHaveBeenCalledWith({ where: { email } }); | ||
}); | ||
}); | ||
}); |