Skip to content

Commit 9e07c73

Browse files
Enhanced form with email + Playwright tests
1 parent 0fe1351 commit 9e07c73

File tree

3 files changed

+79
-15
lines changed

3 files changed

+79
-15
lines changed

src/app/api/submit/route.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { NextResponse } from 'next/server';
22

33
export async function POST(req: Request) {
4-
const { name } = await req.json();
5-
return NextResponse.json({ message: `Hello, ${name}!` });
4+
try {
5+
const { name, email } = await req.json();
6+
7+
if (!name || !email) {
8+
return NextResponse.json({ message: 'Name and Email are required' }, { status: 400 });
9+
}
10+
11+
return NextResponse.json({ message: `Thank you, ${name}!` });
12+
} catch {
13+
return NextResponse.json({ message: 'Server error' }, { status: 500 });
14+
}
615
}

src/app/form/page.tsx

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,39 @@ import { useState } from 'react';
44

55
export default function Form() {
66
const [name, setName] = useState('');
7+
const [email, setEmail] = useState('');
78
const [message, setMessage] = useState('');
9+
const [loading, setLoading] = useState(false);
10+
const [error, setError] = useState('');
811

912
const handleSubmit = async (e: React.FormEvent) => {
1013
e.preventDefault();
11-
const res = await fetch('/api/submit', {
12-
method: 'POST',
13-
headers: { 'Content-Type': 'application/json' },
14-
body: JSON.stringify({ name }),
15-
});
16-
const data = await res.json();
17-
setMessage(data.message);
14+
setLoading(true);
15+
setMessage('');
16+
setError('');
17+
18+
try {
19+
const res = await fetch('/api/submit', {
20+
method: 'POST',
21+
headers: { 'Content-Type': 'application/json' },
22+
body: JSON.stringify({ name, email }),
23+
});
24+
25+
if (!res.ok) throw new Error('Failed to submit');
26+
const data = await res.json();
27+
setMessage(data.message);
28+
} catch {
29+
setError('Submission failed. Please try again.');
30+
} finally {
31+
setLoading(false);
32+
}
33+
};
34+
35+
const handleReset = () => {
36+
setName('');
37+
setEmail('');
38+
setMessage('');
39+
setError('');
1840
};
1941

2042
return (
@@ -27,9 +49,19 @@ export default function Form() {
2749
value={name}
2850
onChange={(e) => setName(e.target.value)}
2951
/>
30-
<button type="submit">Submit</button>
52+
<input
53+
type="email"
54+
placeholder="Enter your email"
55+
value={email}
56+
onChange={(e) => setEmail(e.target.value)}
57+
/>
58+
<button type="submit" disabled={loading}>
59+
{loading ? 'Submitting...' : 'Submit'}
60+
</button>
61+
<button type="button" onClick={handleReset}>Reset</button>
3162
</form>
32-
{message && <p>{message}</p>}
63+
{message && <p style={{ color: 'green' }}>{message}</p>}
64+
{error && <p style={{ color: 'red' }}>{error}</p>}
3365
</div>
3466
);
3567
}

tests/form.spec.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
import { test, expect } from '@playwright/test';
22

3-
test('Form submission test', async ({ page }) => {
3+
test('Form submission works correctly', async ({ page }) => {
44
await page.goto('/form');
5-
await page.fill('input', 'Nandini');
6-
await page.click('button');
7-
await expect(page.locator('p')).toHaveText('Hello, Nandini!');
5+
6+
// Ensure form elements are present
7+
await expect(page.locator('input[placeholder="Enter your name"]')).toBeVisible();
8+
await expect(page.locator('input[placeholder="Enter your email"]')).toBeVisible();
9+
await expect(page.locator('button[type="submit"]')).toHaveText('Submit');
10+
11+
// Fill out the form
12+
await page.fill('input[placeholder="Enter your name"]', 'John Doe');
13+
await page.fill('input[placeholder="Enter your email"]', '[email protected]');
14+
15+
// Submit the form
16+
await page.click('button[type="submit"]');
17+
18+
// Wait for success message
19+
await expect(page.locator('p')).toHaveText('Thank you, John Doe!');
20+
});
21+
22+
test('Shows validation error if fields are missing', async ({ page }) => {
23+
await page.goto('/form');
24+
25+
// Try to submit without filling anything
26+
await page.click('button[type="submit"]');
27+
28+
// Expect an error message
29+
await expect(page.locator('p')).toHaveText('Name and Email are required');
830
});
31+

0 commit comments

Comments
 (0)