forked from hngprojects/hng_boilerplate_expressjs
-
Notifications
You must be signed in to change notification settings - Fork 0
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 hngprojects#516 from Khingz/feat/subscription
feat: unsubscribe from newsletter
- Loading branch information
Showing
6 changed files
with
230 additions
and
23 deletions.
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 |
---|---|---|
|
@@ -12,4 +12,7 @@ export class NewsLetterSubscriber { | |
|
||
@Column() | ||
email: string; | ||
|
||
@Column() | ||
isSubscribe: boolean; | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import { Repository } from "typeorm"; | |
import AppDataSource from "../data-source"; | ||
import { NewsLetterSubscriber } from "../models/newsLetterSubscription"; | ||
import { NewsLetterSubscriptionService } from "../services/newsLetterSubscription.service"; | ||
import { BadRequest, ResourceNotFound } from "../middleware"; | ||
|
||
jest.mock("../data-source", () => ({ | ||
__esModule: true, | ||
|
@@ -22,9 +23,7 @@ jest.mock("../utils"); | |
|
||
describe("NewsLetterSubscriptionService", () => { | ||
let newsLetterSubscriptionService: NewsLetterSubscriptionService; | ||
let newsLetterRepositoryMock: jest.Mocked< | ||
Repository<NewsLetterSubscriptionService> | ||
>; | ||
let newsLetterRepositoryMock: jest.Mocked<Repository<NewsLetterSubscriber>>; | ||
|
||
beforeEach(() => { | ||
newsLetterRepositoryMock = { | ||
|
@@ -45,12 +44,8 @@ describe("NewsLetterSubscriptionService", () => { | |
|
||
describe("SubscribeToNewsLetter", () => { | ||
it("should subscribe a new user", async () => { | ||
const user = new NewsLetterSubscriber(); | ||
user.email = "[email protected]"; | ||
|
||
const payload = { | ||
email: "[email protected]", | ||
}; | ||
const newSubscriber = new NewsLetterSubscriber(); | ||
newSubscriber.email = "[email protected]"; | ||
|
||
(newsLetterRepositoryMock.findOne as jest.Mock).mockResolvedValue(null); | ||
(newsLetterRepositoryMock.save as jest.Mock).mockImplementation( | ||
|
@@ -59,39 +54,63 @@ describe("NewsLetterSubscriptionService", () => { | |
return Promise.resolve(user); | ||
}, | ||
); | ||
|
||
const result = | ||
await newsLetterSubscriptionService.subscribeUser("[email protected]"); | ||
|
||
expect(result.isSubscribe).toBe(false); | ||
expect(result.isNewlySubscribe).toBe(true); | ||
expect(result.subscriber).toEqual({ | ||
id: "456", | ||
email: "[email protected]", | ||
isSubscribe: true, | ||
}); | ||
expect(newsLetterRepositoryMock.save).toHaveBeenCalled(); | ||
expect(newsLetterRepositoryMock.save).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
email: "[email protected]", | ||
isSubscribe: true, | ||
}), | ||
); | ||
}); | ||
|
||
it("should handle already subscribed user", async () => { | ||
const user = new NewsLetterSubscriber(); | ||
user.id = "123"; | ||
user.email = "[email protected]"; | ||
user.isSubscribe = true; | ||
(newsLetterRepositoryMock.findOne as jest.Mock).mockResolvedValue(user); | ||
(newsLetterRepositoryMock.save as jest.Mock).mockImplementation( | ||
(user) => { | ||
user.id = "456"; | ||
return Promise.resolve(user); | ||
}, | ||
); | ||
|
||
const result = | ||
await newsLetterSubscriptionService.subscribeUser("[email protected]"); | ||
|
||
expect(result.isSubscribe).toBe(true); | ||
expect(result.isNewlySubscribe).toBe(false); | ||
expect(result.subscriber).toEqual({ | ||
id: "123", | ||
email: "[email protected]", | ||
isSubscribe: true, | ||
}); | ||
expect(newsLetterRepositoryMock.save).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should throw a Conflict error if already subscribed but inactive", async () => { | ||
const inactiveSubscriber = new NewsLetterSubscriber(); | ||
inactiveSubscriber.email = "[email protected]"; | ||
inactiveSubscriber.isSubscribe = false; | ||
|
||
(newsLetterRepositoryMock.findOne as jest.Mock).mockResolvedValue( | ||
inactiveSubscriber, | ||
); | ||
|
||
await expect( | ||
newsLetterSubscriptionService.subscribeUser("[email protected]"), | ||
).rejects.toThrow(BadRequest); | ||
}); | ||
|
||
it("should throw an error if something goes wrong", async () => { | ||
(newsLetterRepositoryMock.findOne as jest.Mock).mockRejectedValue( | ||
new Error("An error occurred while processing your request"), | ||
|
@@ -168,4 +187,53 @@ describe("NewsLetterSubscriptionService", () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe("UnsubscribeFromNewsLetter", () => { | ||
it("should successfully unsubscribe a logged-in user from the newsletter", async () => { | ||
const user = new NewsLetterSubscriber(); | ||
user.email = "[email protected]"; | ||
user.id = "5678"; | ||
user.isSubscribe = true; | ||
|
||
(newsLetterRepositoryMock.findOne as jest.Mock).mockResolvedValue(user); | ||
|
||
(newsLetterRepositoryMock.save as jest.Mock).mockImplementation( | ||
(user) => { | ||
user.isSubscribe = false; | ||
return Promise.resolve(user); | ||
}, | ||
); | ||
|
||
const result = | ||
await newsLetterSubscriptionService.unSubcribeUser("[email protected]"); | ||
|
||
expect(result).toEqual({ | ||
id: "5678", | ||
email: "[email protected]", | ||
isSubscribe: false, | ||
}); | ||
|
||
expect(newsLetterRepositoryMock.save).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
id: "5678", | ||
email: "[email protected]", | ||
isSubscribe: false, | ||
}), | ||
); | ||
}); | ||
|
||
it("should throw an error if user is not subscribed", async () => { | ||
const inactiveSubscriber = new NewsLetterSubscriber(); | ||
inactiveSubscriber.email = "[email protected]"; | ||
inactiveSubscriber.isSubscribe = false; | ||
|
||
(newsLetterRepositoryMock.findOne as jest.Mock).mockResolvedValue( | ||
inactiveSubscriber, | ||
); | ||
|
||
await expect( | ||
newsLetterSubscriptionService.subscribeUser("[email protected]"), | ||
).rejects.toThrow(BadRequest); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.