GithubHelp home page GithubHelp logo

Comments (5)

NoriSte avatar NoriSte commented on June 15, 2024 2

Here's the solution, you can find it this repository of mine. You do not need to use cy.waitUntil but I've added two tests, one with Cypress' built-in utilities and one with cy.waitUntil.

Common part:

let mutationsHistory
beforeEach(() => {
  cy.visit('/')

  mutationsHistory = []
  cy.get('#some-id').then(($targetNode) => {
    const observer = new MutationObserver((mutationsList) => mutationsHistory.push(mutationsList))
    observer.observe($targetNode[0], { childList: true })
  })
})

leveraging the MutationObserver API, Cypress records all the mutations in the mutationsList array.

Cypress built-in solution
This simple script waits until the mutations array contains something

cy.wrap(mutationsHistory)
  .should('have.length.greaterThan', 0)

With cy.waitUntil:
The same of the above code but with cy.waitUntil

cy.waitUntil(() => mutationsHistory.length)

Always opt for the built-in solution if you can šŸ˜‰

This is the full code of the test

/// <reference types="Cypress" />

context('DOM change detection', () => {
  let mutationsHistory
  beforeEach(() => {
    cy.visit('/')

    mutationsHistory = []
    cy.get('#some-id').then(($targetNode) => {
      const observer = new MutationObserver((mutationsList) => mutationsHistory.push(mutationsList))
      observer.observe($targetNode[0], { childList: true })
    })
  })

  it('Should detect the DOM change', () => {
    cy.contains('Mutate after a random delay').click()
    cy.wrap(mutationsHistory)
      .should('have.length.greaterThan', 0)
      .then((mutationsHistory) => {
        cy.log('Now you can do whatever you want with `mutationsHistory`')
        console.log(mutationsHistory)
      })
  })

  it('Should detect the DOM change with cy.waitUntil', () => {
    cy.contains('Mutate after a random delay').click()
    cy.waitUntil(() => (mutationsHistory.length ? mutationsHistory : false)).then(
      (mutationsHistory) => {
        cy.log('Now you can do whatever you want with `mutationsHistory`')
        console.log(mutationsHistory)
      }
    )
  })
})

I hope it helps šŸ˜‰

from cypress-wait-until.

NoriSte avatar NoriSte commented on June 15, 2024

Hi @Wulfheart

How can I detect a status change in the

    element

What do you mean by "status change"? A change in the children? A change in the attributes?

Is there anything that allows you to be completely sure that something changed? I guess that you have SSR and re-hydration in browserland? Timestamps could be a good idea.

Anyway: before speaking about how to use cy.waitUntil to detect a change we need to clarify how to detect a change at all, that's why I'm asking above šŸ˜‰

from cypress-wait-until.

Wulfheart avatar Wulfheart commented on June 15, 2024

@NoriSte thanks for your quick response. Generally speaking Iā€™d like to detect a DOM change in a specific container.

from cypress-wait-until.

NoriSte avatar NoriSte commented on June 15, 2024

Tomorrow I could add a more complete answer, meanwhile take a look at MutationObserver.

from cypress-wait-until.

Wulfheart avatar Wulfheart commented on June 15, 2024

Thank you for this extensive solution.

from cypress-wait-until.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.