1
0

web-patch.spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // @ts-check
  2. const { test, expect } = require("@playwright/test");
  3. const hotPatchTimeout = {
  4. timeout: 1000 * 60 * 2, // 2 minute
  5. };
  6. test("button click", async ({ page }) => {
  7. const fs = require("fs");
  8. const mainPath = "web-hot-patch-temp/src/main.rs";
  9. var mainContent = fs.readFileSync(mainPath, "utf8");
  10. const stylePath = "web-hot-patch-temp/assets/style.css";
  11. var styleContent = fs.readFileSync(stylePath, "utf8");
  12. // Reset any changes made to the main.rs and style.css files.
  13. mainContent = mainContent.replace(/num \+= 2;/g, "num += 1;");
  14. mainContent = mainContent.replace("Click button! Count:", "Click me! Count:");
  15. mainContent = mainContent.replace("asset!('/assets/alternative-style.css')", "asset!('/assets/style.css')");
  16. fs.writeFileSync(mainPath, mainContent);
  17. styleContent = styleContent.replace(
  18. "background-color: blue;",
  19. "background-color: red;"
  20. );
  21. fs.writeFileSync(stylePath, styleContent);
  22. await page.goto("http://localhost:9980");
  23. // ** First test make sure the initial fat build is working **
  24. // Expect the page to contain the counter text.
  25. const main = page.locator("#main");
  26. await expect(main).toContainText("Click me! Count: 0");
  27. // Click the increment button.
  28. let button = page.locator("button#increment-button");
  29. await button.click();
  30. // Expect the page to contain the updated counter text.
  31. await expect(main).toContainText("Click me! Count: 1");
  32. // Make sure the css is applied correctly.
  33. await expect(main).toHaveCSS("background-color", "rgb(255, 0, 0)");
  34. // Make sure the image is loaded.
  35. const headerImage = page.locator("#toasts");
  36. // expect the attribute src to start with /assets/toasts-
  37. await expect(headerImage).toHaveAttribute("src", /\/assets\/toasts-/);
  38. // ** Then make sure the hot patch is working **
  39. // Then change the file to increment by 2.
  40. const updatedContent = mainContent.replace(/num \+= 1;/g, "num += 2;");
  41. // Change the click me text to reflect the new increment.
  42. const updatedContentWithText = updatedContent.replace(
  43. "Click me! Count:",
  44. "Click button! Count:"
  45. );
  46. fs.writeFileSync(mainPath, updatedContentWithText);
  47. // Wait for the page to update and show the new text.
  48. await expect(main).toContainText("Click button! Count: 1", hotPatchTimeout);
  49. // Now click the button again.
  50. await button.click();
  51. // Expect the count to update by 2.
  52. await expect(main).toContainText("Click button! Count: 3");
  53. // Next change just the css file to change the background color to blue.
  54. const updatedStyleContent = styleContent.replace(
  55. "background-color: red;",
  56. "background-color: blue;"
  57. );
  58. fs.writeFileSync(stylePath, updatedStyleContent);
  59. // Wait for the page to update the background color.
  60. await expect(main).toHaveCSS(
  61. "background-color",
  62. "rgb(0, 0, 255)",
  63. hotPatchTimeout
  64. );
  65. // Make sure the image is still loaded.
  66. // expect the attribute src to start with /assets/toasts-
  67. await expect(headerImage).toHaveAttribute("src", /\/assets\/toasts-/);
  68. // ** Then add a new asset to the page **
  69. // Switch from style to alternative-style
  70. const updatedContentWithAlternativeStyle = updatedContentWithText.replace(
  71. 'asset!("/assets/style.css")',
  72. 'asset!("/assets/alternative-style.css")'
  73. );
  74. fs.writeFileSync(mainPath, updatedContentWithAlternativeStyle);
  75. // Assert the page has the new alternative style applied.
  76. const body = page.locator("body");
  77. // Log the page content to debug if needed.
  78. console.log(await page.content());
  79. await expect(body).toHaveCSS(
  80. "background-color",
  81. "rgb(100, 100, 100)",
  82. hotPatchTimeout
  83. );
  84. });