From 31bafb80e01b5d467496e64bbd7cefc86ee1a1aa Mon Sep 17 00:00:00 2001 From: Jakub Wilk Date: Thu, 1 Aug 2024 15:54:14 +0200 Subject: [PATCH] refactor: update email interface and email sending test --- .../src/auth/__tests__/auth.service.spec.ts | 20 ++++--------------- .../api/src/common/emails/email.interface.ts | 16 ++++++--------- .../api/test/helpers/test-email.adapter.ts | 10 +--------- 3 files changed, 11 insertions(+), 35 deletions(-) diff --git a/examples/common_nestjs_remix/apps/api/src/auth/__tests__/auth.service.spec.ts b/examples/common_nestjs_remix/apps/api/src/auth/__tests__/auth.service.spec.ts index 1fa71ef..c3960b0 100644 --- a/examples/common_nestjs_remix/apps/api/src/auth/__tests__/auth.service.spec.ts +++ b/examples/common_nestjs_remix/apps/api/src/auth/__tests__/auth.service.spec.ts @@ -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 = "You are a bold one"; - - 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 () => { diff --git a/examples/common_nestjs_remix/apps/api/src/common/emails/email.interface.ts b/examples/common_nestjs_remix/apps/api/src/common/emails/email.interface.ts index cbd0949..90385b0 100644 --- a/examples/common_nestjs_remix/apps/api/src/common/emails/email.interface.ts +++ b/examples/common_nestjs_remix/apps/api/src/common/emails/email.interface.ts @@ -1,13 +1,9 @@ -interface BaseEmail { +export type Email = { to: string; from: string; subject: string; -} - -type RequireAtLeastOne = { - [K in keyof T]-?: Required> & - Partial>>; -}[keyof T]; - -export type Email = RequireAtLeastOne<{ text: string; html: string }> & - BaseEmail; +} & ( + | { html: string; text?: never } + | { text: string; html?: never } + | { text: string; html: string } +); diff --git a/examples/common_nestjs_remix/apps/api/test/helpers/test-email.adapter.ts b/examples/common_nestjs_remix/apps/api/test/helpers/test-email.adapter.ts index da03d92..61274af 100644 --- a/examples/common_nestjs_remix/apps/api/test/helpers/test-email.adapter.ts +++ b/examples/common_nestjs_remix/apps/api/test/helpers/test-email.adapter.ts @@ -6,17 +6,9 @@ import { last } from "lodash"; @Injectable() export class EmailTestingAdapter extends EmailAdapter { private sentEmails: Email[] = []; - private emailOverride: Partial | null = null; async sendMail(email: Email): Promise { - const finalEmail = this.emailOverride - ? { ...email, ...this.emailOverride } - : email; - this.sentEmails.push(finalEmail); - } - - setEmailOverride(override: Partial): void { - this.emailOverride = override; + this.sentEmails.push(email); } getAllEmails(): Email[] {