Friday, 1 January 2021

How to iterate over and test different child elements within an element in cypress?

So, I have the elements in the following structure:

<div class="MuiBox-root" data-testid="all-events">
  <ul ....>
    <div class="MuiBox-root" data-testid="event">
    <div class="MuiBox-root" data-testid="event">
    <div class="MuiBox-root" data-testid="event">

Now, within each event element is the following structure:

 <div class="MuiBox-root" data-testid="event">
  <li .....>
    <div class="MuiBox-root ....." data-testid="event-status">
      <div> Text Value </div>
    <div class="MuiBox-root ....." data-testid="event-name">
    

So, what I want to test is that if the element event-status is say completed or active, then the element event-name will be present. So the approach that I am using is as follows:

cy.get("[data-testid='events']").its("length").then((numEvents) => {
  for (let i = 0; i < numEvents; i++) {
    cy.get("[data-testid='event-status']").each((status, index) => {
      if (index === i) {
        if (isEventActive(status)) {
          cy.get("[data-testid='event-name']").each((name, index) => {
            if (index === i) {
              cy.get(name).should("have.text", "some value");
            }
          });
        } else {
            cy.get("[data-testid='event-name']").each((name, index) => {
            if (index === i) {
              cy.get(name).should("not.exist");
            }
          });
        }
      }
    });
  }
});

The above code is working to test what I want to test out but it is incredibly messy and any simpler approaches are welcome.



from How to iterate over and test different child elements within an element in cypress?

how to undo and redo chess moves using chessboard.js for analyze mode

I am using the chess.js and chessboard.js for my chess a project.

I just want to implement the undo and redo functionality of chess moves.

I am creating "Move Recorder" for chess moves using chessboard.js, through which users can undo and redo chess moves. User is playing from both sides to analyze chess puzzle.

Please let me know if any one can help on this.



from how to undo and redo chess moves using chessboard.js for analyze mode

quilljs copyCode module - Failed to execute 'insertBefore' on 'Node'

I am creating a copyCode plugin for QuillJs. Everything seems to be working for the plugin, however, when a space is created between the text and the code-block, you get this error:

Failed to execute 'insertBefore' on 'Node'

Here is the code:

const copyContentIntoClipboard = (rawData: string) => {
  const encodedContent = encodeURIComponent(rawData);
  const filteredEncodedContent = encodedContent.replace(/%EF%BB%BF/g, "");
  const targetContent = decodeURIComponent(filteredEncodedContent);
  const tmpHolder = document.createElement("textarea");
  tmpHolder.value = targetContent;
  document.body.appendChild(tmpHolder);
  tmpHolder.select();
  document.execCommand("copy");
  document.body.removeChild(tmpHolder);
};

const CodeBlock = Quill.import("formats/code-block");

class CopyCode extends CodeBlock {
 copyBadge: HTMLDivElement | null;
  domNode: HTMLElement;
  container: HTMLElement | null;
  parent: HTMLElement;
  copyHandler: EventListener;

  _mountContainer() {
    const container = document.createElement("div");
    container.classList.add("ql-container");
    if (this.domNode.nextSibling) {
      this.domNode.parentElement?.insertBefore(
        container,
        this.domNode
      );
      container.appendChild(this.domNode); // <-- error starts here
      this.container = container;
    }
  }
  _dismountContainer() {

    if (this.container) {
      this.container.parentElement?.insertBefore(
        this.domNode,
        this.container.nextSibling
      );
      this.domNode.parentElement?.removeChild(this.container);
    }
    this.container = null;
  }

  _mountBadge() {
    const copyBadge = document.createElement("div");
    copyBadge.contentEditable = "false";
    copyBadge.classList.add("ql-badge", "ql-badge-copy");
    copyBadge.textContent = "copy";
    this.domNode.parentElement?.insertBefore(
      copyBadge,
      this.domNode.nextSibling
    );
    const copyHandler = (e: MouseEvent) => {
      e.stopPropagation();
      e.preventDefault();
      const target = e.target as HTMLElement;
      const codeArea = target.previousSibling;
      const copyCode = codeArea?.textContent?.trim() || '';
      if (!codeArea) {
        return;
      }
      copyBadge.textContent = "copied!";
      setTimeout(function() {
        copyBadge.textContent = "copy";
      }, 2000);
      copyContentIntoClipboard(copyCode);
    };
    copyBadge.addEventListener("click", copyHandler, true);
    this.copyHandler = copyHandler;
    this.copyBadge = copyBadge;
  }
  _dismountBadge() {
    const badgeIsInDom = this.domNode.parentElement?.contains(this.copyBadge);
    if (this.copyBadge && badgeIsInDom) {
      this.copyBadge.removeEventListener("click", this.copyHandler, true);
      this.copyBadge.parentElement?.removeChild(this.copyBadge);
    }
    this.copyBadge = null;
    this.copyHandler = () => {};
  }

  _mount() {
    this._mountContainer();
    this._mountBadge();
  }

  insertInto(...args: any) {
    super.insertInto(...args);
    const allowCustomMount = !this.copyBadge && !this.container && this.parent;
    if (allowCustomMount) {
      this._mount();
    }
  }
  remove() {
    this._dismountBadge();
    this._dismountContainer();
    super.remove();
  }
}

Here is the StackBlitz: https://stackblitz.com/edit/typescript-ggvuuy?file=index.html

I believe the error is caused by QuillJS believing the code-block should be a pre block instead of a div block containing a pre block. However, I don't know how to fix it...

Any ideas?



from quilljs copyCode module - Failed to execute 'insertBefore' on 'Node'