-
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.
- Loading branch information
Showing
39 changed files
with
1,619 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Page } from "@playwright/test"; | ||
import { SharedPage } from "./shared-page"; | ||
|
||
export class ContactPage extends SharedPage { | ||
constructor(page: Page, baseURL: string) { | ||
super(page, baseURL); | ||
} | ||
|
||
async goto() { | ||
await super.goto("/contact"); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { test, expect } from "../fixtures"; | ||
|
||
test.beforeEach(async ({ contactPage }) => { | ||
await contactPage.goto(); | ||
}); | ||
|
||
test("has title", async ({ page }) => { | ||
await expect(page).toHaveTitle("Gil Hanan | Contact"); | ||
}); | ||
|
||
test("can submit form", async ({ page }) => { | ||
// should wait for capthca to load | ||
await new Promise((resolve) => setTimeout(resolve, 3000)); | ||
|
||
await page.getByLabel("Name").fill("Test User"); | ||
await page.getByLabel("Email").fill("[email protected]"); | ||
await page.getByLabel("Subject").fill("Test Subject"); | ||
await page.getByLabel("Text").fill("Test description"); | ||
|
||
// const responsePromise$ = page.waitForResponse( | ||
// (resp) => resp.url().includes("/api/contact") && resp.status() === 200, | ||
// ); | ||
|
||
await page | ||
.getByRole("button", { | ||
name: "Submit", | ||
}) | ||
.click(); | ||
|
||
// await responsePromise$; | ||
|
||
// await page.waitForURL("/contact/success"); | ||
// await expect(page.getByText(/Thank you for your message!/i)).toBeVisible(); | ||
}); |
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,7 @@ | ||
declare global { | ||
interface Window { | ||
reCaptchaCallback?: () => void; | ||
} | ||
} | ||
|
||
export {}; |
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,11 @@ import "@testing-library/jest-dom"; | |
import { createElement } from "react"; | ||
import type { ImageProps } from "next/image"; | ||
|
||
process.env.SMTP_USER = "mockUser"; | ||
process.env.SMTP_PASSWORD = "mockPassword"; | ||
process.env.SMTP_RECEIVER = "[email protected]"; | ||
process.env.RECAPTCHA_SECRET_KEY = "mockSecretKey"; | ||
|
||
function MockImage({ priority, ...props }: ImageProps) { | ||
return createElement("img", { | ||
...props, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { NextRequest } from "next/server"; | ||
import { sendMail } from "@shared/mailer"; | ||
import { isContact } from "@contact/utils"; | ||
import { Contact } from "@contact/models"; | ||
import { POST } from "@api/contact/route"; | ||
|
||
jest.mock("../../contact/utils"); | ||
jest.mock("../../shared/mailer"); | ||
|
||
const contact: Contact = { | ||
name: "John Doe", | ||
email: "[email protected]", | ||
subject: "Test Subject", | ||
text: "Test Message", | ||
}; | ||
|
||
const { name, email, subject, text } = contact; | ||
|
||
describe("POST", () => { | ||
let body: FormData; | ||
let request: NextRequest; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
body = new FormData(); | ||
Object.entries(contact).forEach(([key, value]) => body.append(key, value)); | ||
|
||
request = new NextRequest("https://example.com", { | ||
method: "POST", | ||
body, | ||
}); | ||
}); | ||
|
||
it("should send email and return 200 if form data is valid", async () => { | ||
(isContact as unknown as jest.Mock).mockReturnValue(true); | ||
|
||
const response = await POST(request); | ||
|
||
expect(response.status).toBe(200); | ||
expect(await response.json()).toEqual({ | ||
message: `Successfully sent email to ${email}`, | ||
}); | ||
expect(sendMail).toHaveBeenCalledWith({ | ||
subject, | ||
html: `${name} <${email}><br><br>${text}`, | ||
}); | ||
}); | ||
|
||
it("should return 400 if form data is invalid", async () => { | ||
(isContact as unknown as jest.Mock).mockReturnValue(false); | ||
|
||
const response = await POST(request); | ||
|
||
expect(response.status).toBe(400); | ||
expect(await response.json()).toEqual({ message: "Invalid form data" }); | ||
expect(sendMail).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should return 500 if sending email fails", async () => { | ||
(isContact as unknown as jest.Mock).mockReturnValue(true); | ||
(sendMail as jest.Mock).mockRejectedValue(new Error()); | ||
|
||
const response = await POST(request); | ||
|
||
expect(response.status).toBe(500); | ||
expect(await response.json()).toEqual({ message: "Failed to send email" }); | ||
expect(sendMail).toHaveBeenCalledWith({ | ||
subject, | ||
html: `${name} <${email}><br><br>${text}`, | ||
}); | ||
}); | ||
}); |
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,33 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { sendMail } from "@shared/mailer"; | ||
import { isContact } from "@contact/utils"; | ||
|
||
export async function POST(request: NextRequest): Promise< | ||
NextResponse<{ | ||
message: string; | ||
}> | ||
> { | ||
const form = Object.fromEntries(await request.formData()); | ||
|
||
if (!isContact(form)) { | ||
return NextResponse.json({ message: "Invalid form data" }, { status: 400 }); | ||
} | ||
|
||
const { name, email, subject, text } = form; | ||
|
||
try { | ||
await sendMail({ | ||
subject, | ||
html: `${name} <${email}><br><br>${text}`, | ||
}); | ||
|
||
return NextResponse.json({ | ||
message: `Successfully sent email to ${form.email}`, | ||
}); | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ message: "Failed to send email" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
Oops, something went wrong.