@bigbinary/neeto-playwright-commons 4.11.15-beta1 → 4.11.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -846,24 +846,18 @@ declare class CustomCommands {
846
846
  }: ToggleElementProps) => Promise<void>;
847
847
  /**
848
848
  *
849
- * Method that returns a page-side promise handle which settles once the DOM selection is collapsed, or rejects once timeout elapses. Create it before the key press that moves the caret so a selectionchange that fires straight away is not missed, await it with handle.evaluate(({ promise }) => promise), then dispose the handle.
849
+ * Method that waits until the focused editor's own selection is collapsed, by polling the ProseMirror state of the editor that owns document.activeElement. Use it before a key press that would replace a selection, such as Enter. Fails if no editor is focused or the selection is still a range when the timeout elapses.
850
850
  *
851
- * timeout (optional): Milliseconds to wait for the selection to collapse before rejecting. Default is 2000.
851
+ * timeout (optional): Milliseconds to wait for the selection to collapse. Default is 2000.
852
852
  *
853
853
  * @example
854
854
  *
855
- * const selectionCollapsed = await neetoPlaywrightUtilities.waitForSelectionToCollapse();
856
855
  * await page.keyboard.press("End");
857
- * try {
858
- * await selectionCollapsed.evaluate(({ promise }) => promise);
859
- * } finally {
860
- * await selectionCollapsed.dispose();
861
- * }
856
+ * await neetoPlaywrightUtilities.waitForSelectionToCollapse();
857
+ * await page.keyboard.press("Enter");
862
858
  * @endexample
863
859
  */
864
- waitForSelectionToCollapse: (timeout?: number) => Promise<_playwright_test.JSHandle<{
865
- promise: Promise<void>;
866
- }>>;
860
+ waitForSelectionToCollapse: (timeout?: number) => Promise<void>;
867
861
  /**
868
862
  *
869
863
  * Method to move the cursor to bottom of a custom text editor. Before calling this method, please make sure the input field is focused. It waits for the editor selection to collapse (via waitForSelectionToCollapse) before pressing Enter, so a word selected before the call is not replaced.
package/index.js CHANGED
@@ -3480,47 +3480,33 @@ class CustomCommands {
3480
3480
  isChecked !== shouldBeChecked && (await locator.click());
3481
3481
  await expect(locator).toBeChecked({ checked: shouldBeChecked });
3482
3482
  }).toPass({ timeout: 15_000 });
3483
- // Returns a page-side promise that settles once the DOM selection is
3484
- // collapsed, or rejects after `timeout` ms. Create it before the key press
3485
- // that moves the caret so a selectionchange that fires straight away is not
3486
- // missed. Both outcomes detach the listener, so nothing outlives the wait.
3487
- waitForSelectionToCollapse = (timeout = 2_000) => this.page.evaluateHandle(timeout => ({
3488
- promise: new Promise((resolve, reject) => {
3489
- const isCollapsed = () => window.getSelection()?.isCollapsed !== false;
3490
- if (isCollapsed())
3491
- return resolve();
3492
- let timer;
3493
- const onSelectionChange = () => {
3494
- if (!isCollapsed())
3495
- return;
3496
- clearTimeout(timer);
3497
- document.removeEventListener("selectionchange", onSelectionChange);
3498
- resolve();
3499
- };
3500
- timer = setTimeout(() => {
3501
- document.removeEventListener("selectionchange", onSelectionChange);
3502
- reject(new Error("Editor selection did not collapse"));
3503
- }, timeout);
3504
- document.addEventListener("selectionchange", onSelectionChange);
3505
- }),
3506
- }), timeout);
3483
+ // Polls the editor's own state, which is what the next key press acts on.
3484
+ // The DOM selection is not proof: ProseMirror re-applies its stored
3485
+ // selection shortly after a programmatic focus, undoing an early collapse.
3486
+ waitForSelectionToCollapse = (timeout = 2_000) => expect
3487
+ .poll(() => this.page.evaluate(contentField => {
3488
+ const editor = document.activeElement?.closest(`[data-testid="${contentField}"]`)?.editor;
3489
+ if (!editor)
3490
+ return "no focused editor";
3491
+ const { selection } = editor.state;
3492
+ return selection.empty
3493
+ ? "collapsed"
3494
+ : `${selection.from}-${selection.to}`;
3495
+ }, NEETO_EDITOR_SELECTORS.contentField), {
3496
+ timeout,
3497
+ intervals: [50, 100, 250],
3498
+ message: "Editor selection did not collapse",
3499
+ })
3500
+ .toBe("collapsed");
3507
3501
  moveCursorAtBottom = async () => {
3508
3502
  const isMacOS = os.platform() === "darwin";
3509
3503
  const rightArrowKey = isMacOS ? "Meta+ArrowRight" : "End";
3510
3504
  const downArrowKey = isMacOS ? "Meta+ArrowDown" : "PageDown";
3511
3505
  await expect(async () => {
3512
- // ProseMirror adopts a keyboard-moved caret only on selectionchange, which
3513
- // can land after the next key press; an early Enter replaces the selection.
3514
- const selectionCollapsed = await this.waitForSelectionToCollapse();
3515
3506
  await this.page.keyboard.press(rightArrowKey);
3516
3507
  await this.page.keyboard.press(rightArrowKey);
3517
3508
  await this.page.keyboard.press(downArrowKey);
3518
- try {
3519
- await selectionCollapsed.evaluate(({ promise }) => promise);
3520
- }
3521
- finally {
3522
- await selectionCollapsed.dispose();
3523
- }
3509
+ await this.waitForSelectionToCollapse();
3524
3510
  }).toPass({ timeout: 15_000 });
3525
3511
  await this.page.keyboard.press("Enter");
3526
3512
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/neeto-playwright-commons",
3
- "version": "4.11.15-beta1",
3
+ "version": "4.11.15",
4
4
  "description": "A package encapsulating common playwright code across neeto projects.",
5
5
  "repository": "git@github.com:bigbinary/neeto-playwright-commons.git",
6
6
  "license": "apache-2.0",
@@ -106,4 +106,4 @@
106
106
  "puppeteer-extra-plugin-stealth": "2.11.2"
107
107
  },
108
108
  "packageManager": "yarn@4.18.1"
109
- }
109
+ }
File without changes