Skip to content

Commit 4109725

Browse files
committed
Update contact form and API unit and E2E tests
1 parent a214c07 commit 4109725

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

‎src/actions/contact/__tests__/domain.spec.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ import { describe, expect, it } from 'vitest'
33
import { contactFormInputSchema } from '../domain'
44

55
describe('contact domain validation', () => {
6+
it('accepts null optional fields from Astro Actions form parsing', () => {
7+
const result = contactFormInputSchema.safeParse({
8+
name: 'Jane Doe',
9+
email: 'jane@example.com',
10+
message: 'This is a valid message with enough detail.',
11+
budget: '5k-10k',
12+
consent: false,
13+
company: null,
14+
phone: null,
15+
timeline: null,
16+
website: null,
17+
service: null,
18+
DataSubjectId: null,
19+
})
20+
21+
expect(result.success).toBe(true)
22+
})
23+
624
it('accepts valid timeline values', () => {
725
const result = contactFormInputSchema.safeParse({
826
name: 'Jane Doe',

‎src/actions/contact/domain.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { isAllowedTimeline } from './responder'
66
const trimString = (value: unknown): unknown => (typeof value === 'string' ? value.trim() : value)
77

88
const emptyStringToUndefined = (value: unknown): unknown => {
9+
if (value === null) return undefined
910
if (typeof value !== 'string') return value
1011
const trimmed = value.trim()
1112
return trimmed.length === 0 ? undefined : trimmed

‎test/e2e/specs/03-forms/contact-form.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { TEST_CONTACT_DATA, TEST_EMAILS } from '@test/e2e/fixtures/test-data'
1818

1919
const CONTACT_PATH = '/contact'
2020

21-
const contactSubmitActionEndpoint = '/_actions/contact/submit'
21+
const contactSubmitActionEndpoint = '/_actions/contact.submit'
2222

2323
const waitForContactFormHydration = async (page: BasePage) => {
2424
await page.waitForFunction(() => {

‎test/e2e/specs/06-actions/contact-api.spec.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
import { expect, test } from '@test/e2e/helpers'
22

3-
const CONTACT_ENDPOINT = '/api/contact'
3+
const CONTACT_ACTION_ENDPOINT = '/_actions/contact.submit'
44

55
test.describe('Contact API integrations', () => {
66
test('@mocks accepts valid submissions', async ({ request }) => {
77
const uniqueEmail = `contact-${Date.now()}@example.com`
8-
const response = await request.post(CONTACT_ENDPOINT, {
9-
data: {
8+
const response = await request.post(CONTACT_ACTION_ENDPOINT, {
9+
multipart: {
1010
name: 'Integration Bot',
1111
email: uniqueEmail,
12-
message: 'Automated contact form verification message',
13-
consent: true,
12+
message: 'Automated contact form verification message.',
13+
'project_type': 'website',
14+
budget: '10k-25k',
15+
consent: 'true',
1416
},
1517
})
1618

1719
expect(response.status()).toBe(200)
18-
const body = await response.json()
19-
expect(body.success).toBe(true)
20+
const contentType = response.headers()['content-type']
21+
expect(contentType).toContain('application/json')
22+
23+
// Astro Actions serialize results using Devalue, which isn't always JSON.parse-friendly.
24+
// Validate using a lightweight string check instead of parsing a structured object.
25+
const bodyText = await response.text()
26+
expect(bodyText).toContain('Thank you for your message')
2027
})
2128

2229
test('@mocks rejects invalid submissions', async ({ request }) => {
2330
const invalidEmail = `invalid-contact-${Date.now()}@example.com`
24-
const response = await request.post(CONTACT_ENDPOINT, {
25-
data: {
31+
const response = await request.post(CONTACT_ACTION_ENDPOINT, {
32+
multipart: {
2633
name: 'x',
2734
email: invalidEmail,
2835
message: 'short',
36+
budget: '10k-25k',
2937
},
3038
})
3139

0 commit comments

Comments
 (0)