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

Code before:
          await kbUndo(page);
          await waitForSerialized(page, 0);
        })
      );
    });

    it("must delete an existing annotation with a popup", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          await page.click("[data-annotation-id='26R']");
          // Wait for the popup to be displayed.
          await page.waitForSelector(
            "[data-annotation-id='popup_26R'] .popup",
            {
              visible: true,
            }
          );

          await switchToFreeText(page);

          const editorSelector = getEditorSelector(0);
          await selectEditor(page, editorSelector);
          await page.keyboard.press("Backspace");
          await page.waitForFunction(
            sel => !document.querySelector(sel),
            {},
            editorSelector
          );

          const serialized = await getSerialized(page);
          expect(serialized).toEqual([
            {
              pageIndex: 0,
              id: "26R",
              deleted: true,
              popupRef: "",
            },
          ]);

          // Disable editing mode.
          await switchToFreeText(page, /* disable = */ true);

          await page.waitForSelector(":not([data-annotation-id='26R'] .popup)");

          // Re-enable editing mode.
          await switchToFreeText(page);
          await page.focus(".annotationEditorLayer");

          await kbUndo(page);
          await waitForSerialized(page, 0);


Review comment (suggestion):
Let's add `await waitForSerialized(page, 1);` before this to match the usage below but also to make sure that we can safely call `getSerialized` to avoid intermittents if e.g. the element is removed from the DOM already but the serialized state is not yet updated.

Code after:
          await kbUndo(page);
          await waitForSerialized(page, 0);
        })
      );
    });

    it("must delete an existing annotation with a popup", async () => {
      await Promise.all(
        pages.map(async ([browserName, page]) => {
          await page.click("[data-annotation-id='26R']");
          // Wait for the popup to be displayed.
          const popupSelector = "[data-annotation-id='popup_26R'] .popup";
          await page.waitForSelector(popupSelector, { visible: true });

          await switchToFreeText(page);

          const editorSelector = getEditorSelector(0);
          await selectEditor(page, editorSelector);
          await page.keyboard.press("Backspace");
          await page.waitForFunction(
            sel => !document.querySelector(sel),
            {},
            editorSelector
          );

          await waitForSerialized(page, 1);
          const serialized = await getSerialized(page);
          expect(serialized).toEqual([
            {
              pageIndex: 0,
              id: "26R",
              deleted: true,
              popupRef: "",
            },
          ]);

          // Disable editing mode.
          await switchToFreeText(page, /* disable = */ true);

          await page.waitForSelector(":not([data-annotation-id='26R'] .popup)");

          // Re-enable editing mode.
          await switchToFreeText(page);
          await page.focus(".annotationEditorLayer");

          await kbUndo(page);
          await waitForSerialized(page, 0);

          // Disable editing mode.
          await switchToFreeText(page, /* disable = */ true);