Repository: mozilla/pdf.js
File: test/integration/freetext_editor_spec.mjs (JavaScript)

Code before:
            rect.x + 0.5 * rect.width,
            rect.y + 0.5 * rect.height
          );
          await waitForSelectedEditor(page, getEditorSelector(1));

          const pos = n =>
            page.evaluate(sel => {
              const editor = document.querySelector(sel);
              return Array.prototype.indexOf.call(
                editor.parentNode.childNodes,
                editor
              );
            }, getEditorSelector(n));

          expect(await pos(0))
            .withContext(`In ${browserName}`)
            .toEqual(0);
          expect(await pos(1))
            .withContext(`In ${browserName}`)
            .toEqual(1);

          const { y: y0, height } = await getRect(page, getEditorSelector(0));
          const selectorEditor = getEditorSelector(1);
          let xy = await getXY(page, selectorEditor);
          while ((await getRect(page, selectorEditor)).y > y0 - height) {
            const handle = await waitForEditorMovedInDOM(page);
            await kbBigMoveUp(page);
            await awaitPromise(handle);
            await waitForPositionChange(page, selectorEditor, xy);
            xy = await getXY(page, selectorEditor);
          }

          // The editor must be moved in the DOM and potentially the focus
          // will be lost, hence there's a callback will get back the focus.
          await page.waitForSelector(`${getEditorSelector(1)}:focus`);

          expect(await pos(0))
            .withContext(`In ${browserName}`)
            .toEqual(1);
          expect(await pos(1))
            .withContext(`In ${browserName}`)
            .toEqual(0);
        })
      );
    });
  });

  describe("Move several FreeTexts", () => {
    let pages;

    beforeAll(async () => {

Review comment (refactor):
Nit: Could we perhaps use the new helper function here as well?
```suggestion
          while ((await getRect(page, selectorEditor)).y > y0 - height) {
            await moveEditor(page, selectorEditor, 1, () => kbBigMoveUp(page));
          }
```

Code after:
            rect.x + 0.5 * rect.width,
            rect.y + 0.5 * rect.height
          );
          await waitForSelectedEditor(page, getEditorSelector(1));

          const pos = n =>
            page.evaluate(sel => {
              const editor = document.querySelector(sel);
              return Array.prototype.indexOf.call(
                editor.parentNode.childNodes,
                editor
              );
            }, getEditorSelector(n));

          expect(await pos(0))
            .withContext(`In ${browserName}`)
            .toEqual(0);
          expect(await pos(1))
            .withContext(`In ${browserName}`)
            .toEqual(1);

          const { y: y0, height } = await getRect(page, getEditorSelector(0));
          const selectorEditor = getEditorSelector(1);
          while ((await getRect(page, selectorEditor)).y > y0 - height) {
            await moveEditor(page, selectorEditor, 1, () => kbBigMoveUp(page));
          }

          // The editor must be moved in the DOM and potentially the focus
          // will be lost, hence there's a callback will get back the focus.
          await page.waitForSelector(`${getEditorSelector(1)}:focus`);

          expect(await pos(0))
            .withContext(`In ${browserName}`)
            .toEqual(1);
          expect(await pos(1))
            .withContext(`In ${browserName}`)
            .toEqual(0);
        })
      );
    });
  });

  describe("Move several FreeTexts", () => {
    let pages;

    beforeAll(async () => {
      pages = await loadAndWait("empty.pdf", ".annotationEditorLayer");
    });

    afterAll(async () => {
      await closePages(pages);