Skip to content

Commit

Permalink
refactor: update email interface and email sending test
Browse files Browse the repository at this point in the history
  • Loading branch information
typeWolffo committed Aug 1, 2024
1 parent fc3696c commit 31bafb8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,12 @@ describe("AuthService", () => {
it("should send a welcome email after successful registration", async () => {
const user = userFactory.build();
const password = "password123";
const subject = "Hello there!";
const text = "General Kenobi";
const html = "<strong>You are a bold one</strong>";

emailAdapter.setEmailOverride({
subject,
text,
html,
});

await authService.register(user.email, password);
const lastEmail = emailAdapter.getLastEmail();
const allEmails = emailAdapter.getAllEmails();

expect(lastEmail).toBeDefined();
expect(lastEmail?.to).toBe(user.email);
expect(lastEmail?.subject).toBe(subject);
expect(lastEmail?.text).toBe(text);
expect(lastEmail?.html).toBe(html);
expect(allEmails).toHaveLength(0);
await authService.register(user.email, password);
expect(allEmails).toHaveLength(1);
});

it("should throw ConflictException if user already exists", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
interface BaseEmail {
export type Email = {
to: string;
from: string;
subject: string;
}

type RequireAtLeastOne<T> = {
[K in keyof T]-?: Required<Pick<T, K>> &
Partial<Pick<T, Exclude<keyof T, K>>>;
}[keyof T];

export type Email = RequireAtLeastOne<{ text: string; html: string }> &
BaseEmail;
} & (
| { html: string; text?: never }
| { text: string; html?: never }
| { text: string; html: string }
);
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@ import { last } from "lodash";
@Injectable()
export class EmailTestingAdapter extends EmailAdapter {
private sentEmails: Email[] = [];
private emailOverride: Partial<Email> | null = null;

async sendMail(email: Email): Promise<void> {
const finalEmail = this.emailOverride
? { ...email, ...this.emailOverride }
: email;
this.sentEmails.push(finalEmail);
}

setEmailOverride(override: Partial<Email>): void {
this.emailOverride = override;
this.sentEmails.push(email);
}

getAllEmails(): Email[] {
Expand Down

0 comments on commit 31bafb8

Please sign in to comment.