text changes and remove infra folder
This commit is contained in:
@@ -1,27 +1,29 @@
|
||||
// Mock dependencies
|
||||
jest.mock('../../../../../services/email/core/EmailClient', () => {
|
||||
jest.mock("../../../../../services/email/core/EmailClient", () => {
|
||||
return jest.fn().mockImplementation(() => ({
|
||||
initialize: jest.fn().mockResolvedValue(),
|
||||
sendEmail: jest.fn().mockResolvedValue({ success: true, messageId: 'msg-123' }),
|
||||
sendEmail: jest
|
||||
.fn()
|
||||
.mockResolvedValue({ success: true, messageId: "msg-123" }),
|
||||
}));
|
||||
});
|
||||
|
||||
jest.mock('../../../../../services/email/core/TemplateManager', () => {
|
||||
jest.mock("../../../../../services/email/core/TemplateManager", () => {
|
||||
return jest.fn().mockImplementation(() => ({
|
||||
initialize: jest.fn().mockResolvedValue(),
|
||||
renderTemplate: jest.fn().mockResolvedValue('<html>Test</html>'),
|
||||
renderTemplate: jest.fn().mockResolvedValue("<html>Test</html>"),
|
||||
}));
|
||||
});
|
||||
|
||||
jest.mock('../../../../../utils/logger', () => ({
|
||||
jest.mock("../../../../../utils/logger", () => ({
|
||||
info: jest.fn(),
|
||||
error: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
}));
|
||||
|
||||
const UserEngagementEmailService = require('../../../../../services/email/domain/UserEngagementEmailService');
|
||||
const UserEngagementEmailService = require("../../../../../services/email/domain/UserEngagementEmailService");
|
||||
|
||||
describe('UserEngagementEmailService', () => {
|
||||
describe("UserEngagementEmailService", () => {
|
||||
let service;
|
||||
const originalEnv = process.env;
|
||||
|
||||
@@ -29,8 +31,8 @@ describe('UserEngagementEmailService', () => {
|
||||
jest.clearAllMocks();
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
FRONTEND_URL: 'http://localhost:3000',
|
||||
SUPPORT_EMAIL: 'support@villageshare.com',
|
||||
FRONTEND_URL: "http://localhost:3000",
|
||||
CUSTOMER_SUPPORT_EMAIL: "support@email.com",
|
||||
};
|
||||
service = new UserEngagementEmailService();
|
||||
});
|
||||
@@ -39,8 +41,8 @@ describe('UserEngagementEmailService', () => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
describe('initialize', () => {
|
||||
it('should initialize only once', async () => {
|
||||
describe("initialize", () => {
|
||||
it("should initialize only once", async () => {
|
||||
await service.initialize();
|
||||
await service.initialize();
|
||||
|
||||
@@ -49,148 +51,176 @@ describe('UserEngagementEmailService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('sendFirstListingCelebrationEmail', () => {
|
||||
const owner = { firstName: 'John', email: 'john@example.com' };
|
||||
const item = { id: 123, name: 'Power Drill' };
|
||||
describe("sendFirstListingCelebrationEmail", () => {
|
||||
const owner = { firstName: "John", email: "john@example.com" };
|
||||
const item = { id: 123, name: "Power Drill" };
|
||||
|
||||
it('should send first listing celebration email with correct variables', async () => {
|
||||
const result = await service.sendFirstListingCelebrationEmail(owner, item);
|
||||
it("should send first listing celebration email with correct variables", async () => {
|
||||
const result = await service.sendFirstListingCelebrationEmail(
|
||||
owner,
|
||||
item,
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(service.templateManager.renderTemplate).toHaveBeenCalledWith(
|
||||
'firstListingCelebrationToOwner',
|
||||
"firstListingCelebrationToOwner",
|
||||
expect.objectContaining({
|
||||
ownerName: 'John',
|
||||
itemName: 'Power Drill',
|
||||
ownerName: "John",
|
||||
itemName: "Power Drill",
|
||||
itemId: 123,
|
||||
viewItemUrl: 'http://localhost:3000/items/123',
|
||||
})
|
||||
viewItemUrl: "http://localhost:3000/items/123",
|
||||
}),
|
||||
);
|
||||
expect(service.emailClient.sendEmail).toHaveBeenCalledWith(
|
||||
'john@example.com',
|
||||
'Congratulations! Your first item is live on Village Share',
|
||||
expect.any(String)
|
||||
"john@example.com",
|
||||
"Congratulations! Your first item is live on Village Share",
|
||||
expect.any(String),
|
||||
);
|
||||
});
|
||||
|
||||
it('should use default name when firstName is missing', async () => {
|
||||
const ownerNoName = { email: 'john@example.com' };
|
||||
it("should use default name when firstName is missing", async () => {
|
||||
const ownerNoName = { email: "john@example.com" };
|
||||
|
||||
await service.sendFirstListingCelebrationEmail(ownerNoName, item);
|
||||
|
||||
expect(service.templateManager.renderTemplate).toHaveBeenCalledWith(
|
||||
'firstListingCelebrationToOwner',
|
||||
expect.objectContaining({ ownerName: 'there' })
|
||||
"firstListingCelebrationToOwner",
|
||||
expect.objectContaining({ ownerName: "there" }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle errors gracefully', async () => {
|
||||
service.templateManager.renderTemplate.mockRejectedValueOnce(new Error('Template error'));
|
||||
it("should handle errors gracefully", async () => {
|
||||
service.templateManager.renderTemplate.mockRejectedValueOnce(
|
||||
new Error("Template error"),
|
||||
);
|
||||
|
||||
const result = await service.sendFirstListingCelebrationEmail(owner, item);
|
||||
const result = await service.sendFirstListingCelebrationEmail(
|
||||
owner,
|
||||
item,
|
||||
);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Template error');
|
||||
expect(result.error).toBe("Template error");
|
||||
});
|
||||
});
|
||||
|
||||
describe('sendItemDeletionNotificationToOwner', () => {
|
||||
const owner = { firstName: 'John', email: 'john@example.com' };
|
||||
const item = { id: 123, name: 'Power Drill' };
|
||||
const deletionReason = 'Violated community guidelines';
|
||||
describe("sendItemDeletionNotificationToOwner", () => {
|
||||
const owner = { firstName: "John", email: "john@example.com" };
|
||||
const item = { id: 123, name: "Power Drill" };
|
||||
const deletionReason = "Violated community guidelines";
|
||||
|
||||
it('should send item deletion notification with correct variables', async () => {
|
||||
it("should send item deletion notification with correct variables", async () => {
|
||||
const result = await service.sendItemDeletionNotificationToOwner(
|
||||
owner,
|
||||
item,
|
||||
deletionReason
|
||||
deletionReason,
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(service.templateManager.renderTemplate).toHaveBeenCalledWith(
|
||||
'itemDeletionToOwner',
|
||||
"itemDeletionToOwner",
|
||||
expect.objectContaining({
|
||||
ownerName: 'John',
|
||||
itemName: 'Power Drill',
|
||||
deletionReason: 'Violated community guidelines',
|
||||
supportEmail: 'support@villageshare.com',
|
||||
dashboardUrl: 'http://localhost:3000/owning',
|
||||
})
|
||||
ownerName: "John",
|
||||
itemName: "Power Drill",
|
||||
deletionReason: "Violated community guidelines",
|
||||
supportEmail: "support@villageshare.com",
|
||||
dashboardUrl: "http://localhost:3000/owning",
|
||||
}),
|
||||
);
|
||||
expect(service.emailClient.sendEmail).toHaveBeenCalledWith(
|
||||
'john@example.com',
|
||||
"john@example.com",
|
||||
'Important: Your listing "Power Drill" has been removed',
|
||||
expect.any(String)
|
||||
expect.any(String),
|
||||
);
|
||||
});
|
||||
|
||||
it('should use default name when firstName is missing', async () => {
|
||||
const ownerNoName = { email: 'john@example.com' };
|
||||
it("should use default name when firstName is missing", async () => {
|
||||
const ownerNoName = { email: "john@example.com" };
|
||||
|
||||
await service.sendItemDeletionNotificationToOwner(ownerNoName, item, deletionReason);
|
||||
await service.sendItemDeletionNotificationToOwner(
|
||||
ownerNoName,
|
||||
item,
|
||||
deletionReason,
|
||||
);
|
||||
|
||||
expect(service.templateManager.renderTemplate).toHaveBeenCalledWith(
|
||||
'itemDeletionToOwner',
|
||||
expect.objectContaining({ ownerName: 'there' })
|
||||
"itemDeletionToOwner",
|
||||
expect.objectContaining({ ownerName: "there" }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle errors gracefully', async () => {
|
||||
service.emailClient.sendEmail.mockRejectedValueOnce(new Error('Send error'));
|
||||
it("should handle errors gracefully", async () => {
|
||||
service.emailClient.sendEmail.mockRejectedValueOnce(
|
||||
new Error("Send error"),
|
||||
);
|
||||
|
||||
const result = await service.sendItemDeletionNotificationToOwner(
|
||||
owner,
|
||||
item,
|
||||
deletionReason
|
||||
deletionReason,
|
||||
);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Send error');
|
||||
expect(result.error).toBe("Send error");
|
||||
});
|
||||
});
|
||||
|
||||
describe('sendUserBannedNotification', () => {
|
||||
const bannedUser = { firstName: 'John', email: 'john@example.com' };
|
||||
const admin = { firstName: 'Admin', lastName: 'User' };
|
||||
const banReason = 'Multiple policy violations';
|
||||
describe("sendUserBannedNotification", () => {
|
||||
const bannedUser = { firstName: "John", email: "john@example.com" };
|
||||
const admin = { firstName: "Admin", lastName: "User" };
|
||||
const banReason = "Multiple policy violations";
|
||||
|
||||
it('should send user banned notification with correct variables', async () => {
|
||||
const result = await service.sendUserBannedNotification(bannedUser, admin, banReason);
|
||||
it("should send user banned notification with correct variables", async () => {
|
||||
const result = await service.sendUserBannedNotification(
|
||||
bannedUser,
|
||||
admin,
|
||||
banReason,
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(service.templateManager.renderTemplate).toHaveBeenCalledWith(
|
||||
'userBannedNotification',
|
||||
"userBannedNotification",
|
||||
expect.objectContaining({
|
||||
userName: 'John',
|
||||
banReason: 'Multiple policy violations',
|
||||
supportEmail: 'support@villageshare.com',
|
||||
})
|
||||
userName: "John",
|
||||
banReason: "Multiple policy violations",
|
||||
supportEmail: "support@villageshare.com",
|
||||
}),
|
||||
);
|
||||
expect(service.emailClient.sendEmail).toHaveBeenCalledWith(
|
||||
'john@example.com',
|
||||
'Important: Your Village Share Account Has Been Suspended',
|
||||
expect.any(String)
|
||||
"john@example.com",
|
||||
"Important: Your Village Share Account Has Been Suspended",
|
||||
expect.any(String),
|
||||
);
|
||||
});
|
||||
|
||||
it('should use default name when firstName is missing', async () => {
|
||||
const bannedUserNoName = { email: 'john@example.com' };
|
||||
it("should use default name when firstName is missing", async () => {
|
||||
const bannedUserNoName = { email: "john@example.com" };
|
||||
|
||||
await service.sendUserBannedNotification(bannedUserNoName, admin, banReason);
|
||||
await service.sendUserBannedNotification(
|
||||
bannedUserNoName,
|
||||
admin,
|
||||
banReason,
|
||||
);
|
||||
|
||||
expect(service.templateManager.renderTemplate).toHaveBeenCalledWith(
|
||||
'userBannedNotification',
|
||||
expect.objectContaining({ userName: 'there' })
|
||||
"userBannedNotification",
|
||||
expect.objectContaining({ userName: "there" }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle errors gracefully', async () => {
|
||||
service.templateManager.renderTemplate.mockRejectedValueOnce(new Error('Template error'));
|
||||
it("should handle errors gracefully", async () => {
|
||||
service.templateManager.renderTemplate.mockRejectedValueOnce(
|
||||
new Error("Template error"),
|
||||
);
|
||||
|
||||
const result = await service.sendUserBannedNotification(bannedUser, admin, banReason);
|
||||
const result = await service.sendUserBannedNotification(
|
||||
bannedUser,
|
||||
admin,
|
||||
banReason,
|
||||
);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Template error');
|
||||
expect(result.error).toBe("Template error");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user