fullstack.spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // @ts-check
  2. const { test, expect } = require("@playwright/test");
  3. test("hydration", async ({ page }) => {
  4. await page.goto("http://localhost:3333");
  5. // Expect the page to contain the pending text.
  6. const main = page.locator("#main");
  7. await expect(main).toContainText("Server said: ...");
  8. // Expect the page to contain the counter text.
  9. await expect(main).toContainText("hello axum! 12345");
  10. // Expect the title to contain the counter text.
  11. await expect(page).toHaveTitle("hello axum! 12345");
  12. // Click the increment button.
  13. let button = page.locator("button.increment-button");
  14. await button.click();
  15. // Click the server button.
  16. let serverButton = page.locator("button.server-button");
  17. await serverButton.click();
  18. // Expect the page to contain the updated counter text.
  19. await expect(main).toContainText("hello axum! 12346");
  20. // Expect the title to contain the updated counter text.
  21. await expect(page).toHaveTitle("hello axum! 12346");
  22. // Expect the page to contain the updated counter text.
  23. await expect(main).toContainText("Server said: Hello from the server!");
  24. // Make sure the error that was thrown on the server is shown in the error boundary on the client
  25. const errors = page.locator("#errors");
  26. await expect(errors).toContainText("Hmm, something went wrong.");
  27. // Expect the onmounted event to be called exactly once.
  28. const mountedDiv = page.locator("div.onmounted-div");
  29. await expect(mountedDiv).toHaveText("onmounted was called 1 times");
  30. });
  31. test("document elements", async ({ page }) => {
  32. await page.goto("http://localhost:3333");
  33. // wait until the meta element is mounted
  34. const meta = page.locator("meta#meta-head[name='testing']");
  35. await meta.waitFor({ state: "attached" });
  36. await expect(meta).toHaveAttribute("data", "dioxus-meta-element");
  37. const link = page.locator("link#link-head[rel='stylesheet']");
  38. await link.waitFor({ state: "attached" });
  39. await expect(link).toHaveAttribute(
  40. "href",
  41. "https://fonts.googleapis.com/css?family=Roboto+Mono"
  42. );
  43. const stylesheet = page.locator("link#stylesheet-head[rel='stylesheet']");
  44. await stylesheet.waitFor({ state: "attached" });
  45. await expect(stylesheet).toHaveAttribute(
  46. "href",
  47. "https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"
  48. );
  49. const script = page.locator("script#script-head");
  50. await script.waitFor({ state: "attached" });
  51. await expect(script).toHaveAttribute("async", "true");
  52. const style = page.locator("style#style-head");
  53. await style.waitFor({ state: "attached" });
  54. const main = page.locator("#main");
  55. await expect(main).toHaveCSS("font-family", "Roboto");
  56. });
  57. test("assets cache correctly", async ({ page }) => {
  58. // Wait for the hashed image to be loaded
  59. const hashedImageFuture = page.waitForResponse((resp) => {
  60. console.log("Response URL:", resp.url());
  61. return resp.url().includes("/assets/image-") && resp.status() === 200;
  62. });
  63. const assetImageFuture = page.waitForResponse(
  64. (resp) => resp.url().includes("/assets/image.png") && resp.status() === 200
  65. );
  66. const nestedAssetImageFuture = page.waitForResponse(
  67. (resp) =>
  68. resp.url().includes("/assets/nested/image.png") && resp.status() === 200
  69. );
  70. // Navigate to the page that includes the image.
  71. await page.goto("http://localhost:3333");
  72. const hashedImageResponse = await hashedImageFuture;
  73. // Make sure the hashed image cache control header is set to immutable
  74. const cacheControl = hashedImageResponse.headers()["cache-control"];
  75. console.log("Cache-Control header:", cacheControl);
  76. expect(cacheControl).toContain("immutable");
  77. // Wait for the asset image to be loaded
  78. const assetImageResponse = await assetImageFuture;
  79. console.log("Asset Image Response:", assetImageResponse);
  80. // Make sure the asset image cache control header does not contain immutable
  81. const assetCacheControl = assetImageResponse.headers()["cache-control"];
  82. console.log("Cache-Control header:", assetCacheControl);
  83. // Expect there to be no cache control header
  84. expect(assetCacheControl).toBeFalsy();
  85. // Wait for the nested asset image to be loaded
  86. const nestedAssetImageResponse = await nestedAssetImageFuture;
  87. console.log(
  88. "Nested Asset Image Response:",
  89. nestedAssetImageResponse
  90. );
  91. // Make sure the nested asset image cache control header does not contain immutable
  92. const nestedAssetCacheControl =
  93. nestedAssetImageResponse.headers()["cache-control"];
  94. console.log("Cache-Control header:", nestedAssetCacheControl);
  95. // Expect there to be no cache control header
  96. expect(nestedAssetCacheControl).toBeFalsy();
  97. });
  98. test("websockets", async ({ page }) => {
  99. await page.goto("http://localhost:3333");
  100. // wait until the websocket div is mounted
  101. const wsDiv = page.locator("div#websocket-div");
  102. await expect(wsDiv).toHaveText("Received: HELLO WORLD");
  103. });