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

Code before:

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

    afterEach(async () => {
      await closePages(pages);
    });

    it("must write a string in a FreeText editor", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          await switchToFreeText(page);

          const rect = await getRect(page, ".annotationEditorLayer");
          const editorSelector = getEditorSelector(0);
          const data = "Hello PDF.js World !!";
          await page.mouse.click(rect.x + 100, rect.y + 100);
          await page.waitForSelector(editorSelector, { visible: true });
          await page.type(`${editorSelector} .internal`, data);
          await commit(page);

          await waitForSelectedEditor(page, editorSelector);
          await waitForStorageEntries(page, 1);

          const alert = await page.$eval("#viewer-alert", el => el.textContent);
          expect(alert).toEqual("Text added");

          let content = await page.$eval(editorSelector, el =>
            el.innerText.trimEnd()
          );
          expect(content).withContext(`In ${browserName}`).toEqual(data);

          // Edit again.
          await page.keyboard.press("Enter");
          await page.waitForSelector(
            `${editorSelector} .overlay:not(.enabled)`
          );
          await commit(page);

          content = await page.$eval(editorSelector, el =>
            el.innerText.trimEnd()
          );
          expect(content).withContext(`In ${browserName}`).toEqual(data);
        })
      );
    });

    it("must copy/paste", async () => {
      // Run sequentially to avoid clipboard issues.
      for (const [browserName, page] of pages) {

Review comment (suggestion):
Note that, as a small bonus, we don't shadow the `alert` builtin anymore with this change.

Code after:

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

    afterEach(async () => {
      await closePages(pages);
    });

    it("must write a string in a FreeText editor", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          await switchToFreeText(page);

          const rect = await getRect(page, ".annotationEditorLayer");
          const editorSelector = getEditorSelector(0);
          const data = "Hello PDF.js World !!";
          await page.mouse.click(rect.x + 100, rect.y + 100);
          await page.waitForSelector(editorSelector, { visible: true });
          await page.type(`${editorSelector} .internal`, data);
          await commit(page);

          await waitForSelectedEditor(page, editorSelector);
          await waitForStorageEntries(page, 1);

          await page.waitForFunction(
            `document.getElementById("viewer-alert").textContent === "Text added"`
          );

          let content = await page.$eval(editorSelector, el =>
            el.innerText.trimEnd()
          );
          expect(content).withContext(`In ${browserName}`).toEqual(data);

          // Edit again.
          await page.keyboard.press("Enter");
          await page.waitForSelector(
            `${editorSelector} .overlay:not(.enabled)`
          );
          await commit(page);

          content = await page.$eval(editorSelector, el =>
            el.innerText.trimEnd()
          );
          expect(content).withContext(`In ${browserName}`).toEqual(data);
        })
      );
    });

    it("must copy/paste", async () => {
      // Run sequentially to avoid clipboard issues.