login-page-layout.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { join } from "node:path";
  2. import { mkdir, writeFile } from "node:fs/promises";
  3. import {
  4. TID_LOGIN_API_KEY_CANCEL_BUTTON,
  5. TID_LOGIN_API_KEY_SKIP_BUTTON,
  6. TID_LOGIN_MENU_ITEM,
  7. TID_LOGIN_TRIGGER,
  8. TID_LOGIN_USE_API_KEY_BUTTON,
  9. TID_OAUTH_LOGIN_BUTTON,
  10. } from "@zcode/shared/test-ids";
  11. import { clearAppData, clickTestIdByDom, waitForTestIdByDom } from "#e2e/helpers/desktop-app";
  12. import { clearRendererViewport, setRendererViewport } from "#e2e/helpers/renderer-viewport";
  13. const viewports = [
  14. [1280, 800],
  15. [390, 680],
  16. [640, 400],
  17. ] as const;
  18. describe("Login page layout", () => {
  19. after(async () => {
  20. await clearRendererViewport();
  21. await clearAppData();
  22. });
  23. for (const theme of ["zai-light", "zai-dark"] as const) {
  24. for (const locale of ["en-US", "zh-CN"] as const) {
  25. it(`${theme} ${locale}: card and footer remain reachable at desktop, narrow and short sizes`, async () => {
  26. await browser.waitUntil(
  27. async () => {
  28. if (await $('[data-testid="onboarding-page"]').isExisting())
  29. await browser.keys("Escape");
  30. return browser.execute(() =>
  31. Boolean(
  32. document.querySelector(
  33. '[data-testid="login-trigger"], [data-testid="oauth-login-button"]',
  34. ),
  35. ),
  36. );
  37. },
  38. { timeout: 60000 },
  39. );
  40. if (!(await $(`[data-testid="${TID_OAUTH_LOGIN_BUTTON}"]`).isExisting())) {
  41. await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).click();
  42. await browser.waitUntil(() =>
  43. browser.execute(
  44. (menu, login) =>
  45. Boolean(
  46. document.querySelector(`[data-testid="${menu}"], [data-testid="${login}"]`),
  47. ),
  48. TID_LOGIN_MENU_ITEM,
  49. TID_OAUTH_LOGIN_BUTTON,
  50. ),
  51. );
  52. if (await $(`[data-testid="${TID_LOGIN_MENU_ITEM}"]`).isExisting())
  53. await clickTestIdByDom(TID_LOGIN_MENU_ITEM);
  54. }
  55. await waitForTestIdByDom(TID_OAUTH_LOGIN_BUTTON);
  56. await browser.execute(
  57. (nextTheme, nextLocale) => {
  58. const element = document.querySelector('[data-testid="oauth-login-button"]')!;
  59. const key = Object.keys(element).find((name) => name.startsWith("__reactFiber"))!;
  60. let fiber = (element as unknown as Record<string, any>)[key];
  61. let changedTheme = false;
  62. let changedLocale = false;
  63. while (fiber) {
  64. const value = fiber.memoizedProps?.value;
  65. if (!changedTheme && value?.getState?.().setTheme) {
  66. value.getState().setTheme(nextTheme);
  67. changedTheme = true;
  68. }
  69. if (!changedLocale && value?.setLocalePreference) {
  70. value.setLocalePreference(nextLocale);
  71. changedLocale = true;
  72. }
  73. fiber = fiber.return;
  74. }
  75. if (!changedTheme || !changedLocale)
  76. throw new Error("presentation context unavailable");
  77. },
  78. theme,
  79. locale,
  80. );
  81. await browser.waitUntil(() =>
  82. browser.execute((value) => document.documentElement.lang === value, locale),
  83. );
  84. const artifacts = join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "login-layout");
  85. await mkdir(artifacts, { recursive: true });
  86. for (const [width, height] of viewports) {
  87. await setRendererViewport(width, height);
  88. for (const mode of ["login", "api-key"] as const) {
  89. if (mode === "api-key") await clickTestIdByDom(TID_LOGIN_USE_API_KEY_BUTTON);
  90. await waitForTestIdByDom(
  91. mode === "api-key" ? TID_LOGIN_API_KEY_SKIP_BUTTON : TID_OAUTH_LOGIN_BUTTON,
  92. );
  93. await browser.execute(() =>
  94. document
  95. .querySelector("main")
  96. ?.querySelector("section")
  97. ?.scrollIntoView({ block: "start" }),
  98. );
  99. const metrics = await browser.execute(() => {
  100. const card = document.querySelector("main section")!;
  101. const footer = document.querySelector("main footer")!;
  102. const heading = card.querySelector("header")!;
  103. const actions = heading.nextElementSibling!;
  104. const bounds = card.getBoundingClientRect();
  105. const foot = footer.getBoundingClientRect();
  106. return {
  107. border: getComputedStyle(card).borderTopWidth,
  108. shadow: getComputedStyle(card).boxShadow,
  109. gap: actions.getBoundingClientRect().top - heading.getBoundingClientRect().bottom,
  110. separation: foot.top - bounds.bottom,
  111. cardWithinWidth: bounds.left >= 0 && bounds.right <= innerWidth,
  112. controlsWithinWidth: [...card.querySelectorAll("button,input")].every((node) => {
  113. const rect = node.getBoundingClientRect();
  114. return rect.left >= bounds.left && rect.right <= bounds.right;
  115. }),
  116. };
  117. });
  118. const name = `${theme}-${locale}-${width}x${height}-${mode}`;
  119. await browser.saveScreenshot(join(artifacts, `${name}.png`));
  120. await writeFile(join(artifacts, `${name}.json`), JSON.stringify(metrics, null, 2));
  121. expect(metrics).toMatchObject({
  122. border: "0px",
  123. gap: 28,
  124. cardWithinWidth: true,
  125. controlsWithinWidth: true,
  126. });
  127. expect(metrics.shadow).not.toBe("none");
  128. expect(metrics.separation).toBeGreaterThanOrEqual(24);
  129. await browser.execute(() =>
  130. document.querySelector("main footer")?.scrollIntoView({ block: "end" }),
  131. );
  132. expect(
  133. await browser.execute(() => {
  134. const rect = document.querySelector("main footer")!.getBoundingClientRect();
  135. return rect.top >= 0 && rect.bottom <= innerHeight;
  136. }),
  137. ).toBe(true);
  138. if (mode === "api-key") await clickTestIdByDom(TID_LOGIN_API_KEY_CANCEL_BUTTON);
  139. }
  140. }
  141. });
  142. }
  143. }
  144. });