abhinaba-ghosh / cypress-react-selector

:zap: cypress plugin to locate react elements by component, props and state
https://www.cypress.io/
MIT License
274 stars 28 forks source link

Using getCurrentState #364

Open hausdora opened 1 year ago

hausdora commented 1 year ago

When I run .getCurrentState I get back an object with two properties. But I'm having trouble doing anything with them.

const state = cy.getReact("Comp").getCurrentState();
cy.log(state); 
//logs: {hidden: false, context: Object{6}} 

const state = cy.getReact("Comp").getCurrentState().context;
cy.log(state); 
//logs nothing

Nothing I'm trying works, how do I make assertions against the getCurrentState return?

        cy.getReact("Comp").getCurrentState()
        .should(($state) => {
          expect($state.context.active.should("have.length", 3))
        });

Attempt 2

cy.getReact("Comp").getCurrentState("context.active").should("have.length", 3)

Attempt 3

        const state = cy
          .getReact("Graphin")
          .getCurrentState()
          .then((data) => cy.log(JSON.stringify(data)));
        cy.log(state);

//Throws TypeError:
Converting circular structure to JSON
    --> starting at object with constructor 'HTMLDivElement'
    |     property '__reactInternalInstance$977odfgpjmb' -> object with constructor 'FiberNode'
    --- property 'stateNode' closes the circle

It doesn't seem to work like getProps, but I can't find any documentation on how I would make assertions against the return.