-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Summary Fixes #2537 ### Time to review: 15 mins ## Changes proposed Creates the E2E tests for the /subscribe page.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
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
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,69 @@ | ||
/* eslint-disable testing-library/prefer-screen-queries */ | ||
|
||
import { | ||
expect, | ||
NextFixture, | ||
test, | ||
} from "next/experimental/testmode/playwright"; | ||
|
||
function mockAPIEndpoints(next: NextFixture, responseText = "1") { | ||
next.onFetch((request: Request) => { | ||
if (request.url.endsWith("/subscribe") && request.method === "POST") { | ||
return new Response(responseText, { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "text/plain", | ||
}, | ||
}); | ||
} | ||
|
||
return "abort"; | ||
}); | ||
} | ||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/subscribe"); | ||
}); | ||
|
||
test.afterEach(async ({ context }) => { | ||
await context.close(); | ||
}); | ||
|
||
test("has title", async ({ page }) => { | ||
await expect(page).toHaveTitle(/Subscribe | Simpler.Grants.gov/); | ||
}); | ||
|
||
test("client side errors", async ({ page }) => { | ||
await page.getByRole("button", { name: /subscribe/i }).click(); | ||
|
||
// Verify client-side errors for required fields | ||
await expect(page.getByTestId("errorMessage")).toHaveCount(2); | ||
await expect(page.getByText("Please enter a name.")).toBeVisible(); | ||
await expect(page.getByText("Please enter an email address.")).toBeVisible(); | ||
}); | ||
|
||
test("successful signup", async ({ next, page }) => { | ||
mockAPIEndpoints(next); | ||
|
||
await page.getByLabel("First Name (required)").fill("Apple"); | ||
await page.getByLabel("Email (required)").fill("[email protected]"); | ||
|
||
await page.getByRole("button", { name: /subscribe/i }).click(); | ||
|
||
await expect( | ||
page.getByRole("heading", { name: /you['’]re subscribed/i }), | ||
).toBeVisible(); | ||
}); | ||
|
||
test("error during signup", async ({ next, page }) => { | ||
mockAPIEndpoints(next, "Error with subscribing"); | ||
|
||
await page.getByLabel("First Name (required)").fill("Apple"); | ||
await page.getByLabel("Email (required)").fill("[email protected]"); | ||
|
||
await page.getByRole("button", { name: /subscribe/i }).click(); | ||
|
||
await expect(page.getByTestId("errorMessage")).toHaveCount(1); | ||
await expect( | ||
page.getByText(/an error occurred when trying to save your subscription./i), | ||
).toBeVisible(); | ||
}); |