fullstack-routing.spec.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // @ts-check
  2. const { test, expect } = require("@playwright/test");
  3. // Wait for the build to finish
  4. async function waitForBuild(request) {
  5. for (let i = 0; i < 10; i++) {
  6. const build = await request.get("http://localhost:8888");
  7. let text = await build.text();
  8. if (!text.includes("Backend connection failed")) {
  9. return;
  10. }
  11. await new Promise((r) => setTimeout(r, 1000));
  12. }
  13. }
  14. // The home and id routes should return 200
  15. test("home route", async ({ request }) => {
  16. await waitForBuild(request);
  17. const response = await request.get("http://localhost:8888");
  18. expect(response.status()).toBe(200);
  19. const text = await response.text();
  20. expect(text).toContain("Home");
  21. });
  22. test("blog route", async ({ request }) => {
  23. await waitForBuild(request);
  24. const response = await request.get("http://localhost:8888/blog/123");
  25. expect(response.status()).toBe(200);
  26. const text = await response.text();
  27. expect(text).toContain("id: 123");
  28. });
  29. // The error route should return 500
  30. test("error route", async ({ request }) => {
  31. await waitForBuild(request);
  32. const response = await request.get("http://localhost:8888/error");
  33. expect(response.status()).toBe(500);
  34. });
  35. // An unknown route should return 404
  36. test("unknown route", async ({ request }) => {
  37. await waitForBuild(request);
  38. const response = await request.get(
  39. "http://localhost:8888/this-route-does-not-exist"
  40. );
  41. expect(response.status()).toBe(404);
  42. });
  43. // Clicking the link on the home page should navigate to the blog route
  44. test("click blog link", async ({ page }) => {
  45. await page.goto("http://localhost:8888");
  46. // Click the link to the blog route
  47. await page.locator('a').click();
  48. // Wait for navigation to complete
  49. await page.waitForURL("http://localhost:8888/blog/1/");
  50. // Check that the blog page is displayed
  51. const text = await page.textContent("body");
  52. expect(text).toContain("id: 1");
  53. });
  54. // Clicking the link on the blog page should navigate back to the home route
  55. test("click home link from blog", async ({ page }) => {
  56. await page.goto("http://localhost:8888/blog/1");
  57. // Click the link to the home route
  58. await page.locator('a').click();
  59. // Wait for navigation to complete
  60. await page.waitForURL("http://localhost:8888");
  61. // Check that the home page is displayed
  62. const text = await page.textContent("body");
  63. expect(text).toContain("Home");
  64. });