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

Code before:

    beforeAll(async () => {
      pages = await loadAndWait(
        "bug1019475_2.pdf",
        ".annotationLayer",
        null,
        null,
        {
          enableAutoLinking: true,
        }
      );
    });

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

    it("must not repeatedly add link annotations redundantly", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          let url = await page.$$eval(
            ".annotationLayer > .linkAnnotation > a",
            annotations => annotations.map(a => a.href)
          );
          expect(url.length).withContext(`In ${browserName}`).toEqual(1);
          await page.evaluate(() => window.PDFViewerApplication.zoomReset());
          url = await page.$$eval(
            ".annotationLayer > .linkAnnotation > a",
            annotations => annotations.map(a => a.href)
          );
          expect(url.length).withContext(`In ${browserName}`).toEqual(1);
        })
      );
    });
  });
});


Review comment (suggestion):
I believe that there's a couple of issues with this line:
 - It *implicitly* relies on the initial zoom level of the document being "page-fit", and if the default zoom-value for the integration-tests is ever changed this test would no longer be testing the intended thing, so it'd obviously be much better to invoke zoomIn or zoomOut here.
 - Just awaiting the zoom-call probably doesn't really guarantee that re-rendering has finished *or* that the annotationLayer has been re-enabled (since it's hidden during zooming). Hence you need to wait for the appropriate events, since otherwise the checks below might not be testing the correct thing.
 - Lastly, having a blank-line before this "second part" of the test-case wouldn't hurt.

Code after:
            expect(id)
              .withContext(`In ${browserName}`)
              .not.toContain("inferred_link_")
          );
        })
      );
    });
  });

  describe("PR 19470", function () {
    let pages;

    beforeAll(async () => {
      pages = await loadAndWait(
        "bug1019475_2.pdf",
        ".annotationLayer",
        null,
        null,
        {
          enableAutoLinking: true,
        }
      );
    });

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

    it("must not repeatedly add link annotations redundantly", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          await waitForLinkAnnotations(page);
          let url = await page.$$eval(
            ".annotationLayer > .linkAnnotation > a",
            annotations => annotations.map(a => a.href)
          );
          expect(url.length).withContext(`In ${browserName}`).toEqual(1);

          await page.evaluate(() =>
            window.PDFViewerApplication.pdfViewer.updateScale({
              drawingDelay: -1,
              scaleFactor: 2,
            })
          );
          await waitForLinkAnnotations(page);
          url = await page.$$eval(
            ".annotationLayer > .linkAnnotation > a",
            annotations => annotations.map(a => a.href)
          );
          expect(url.length).withContext(`In ${browserName}`).toEqual(1);
        })