cypress-react-selector icon indicating copy to clipboard operation
cypress-react-selector copied to clipboard

Feature: Allow updating props and states

Open abhinaba-ghosh opened this issue 5 years ago • 6 comments

it will be nice to mounting react component runtime so that user can update states and props runtime. Something like:

cy.getReact().setProps({})

cy.getReact().setState({});

abhinaba-ghosh avatar Aug 08 '20 09:08 abhinaba-ghosh

Thanks @abhinaba-ghosh, I'll have a look.

Only issue is that I do not have access to the source. I have just been told to test different states using the props.

This function is in react-dev-tools for chrome.

wing5822 avatar Aug 09 '20 03:08 wing5822

@wing5822 yes, I truly understand the pain. But the fact is once you change the prop, the component should be re-rendered. I highly doubt, it can be achieved without access to the source code. I am testing some random concepts. Keep an eye in this ticket.

abhinaba-ghosh avatar Aug 09 '20 08:08 abhinaba-ghosh

@abhinaba-ghosh, I'll have a look around too. I think it should be achievable because if you use the react-dev-tools in chrome it allows you to change the props and the component will render accordingly. If find a solution I'll come back.

wing5822 avatar Aug 09 '20 08:08 wing5822

Great demo @wing5822, someone should reverse engineer the chrome dev tools! The firefox version does not allow for props to be changed.

danieltroger avatar Dec 23 '20 12:12 danieltroger

it will be nice to mounting react component runtime so that user can update states and props runtime. Something like:

cy.getReact().setProps({})

cy.getReact().setState({});

When a component is changing props, it usually when a child component has a state dependency from a parent via that prop. That means we can simulate a dummy parent which wraps our component under test.

We are passing state from the parent, that means we need a way to control that state.

Suppose ChildComponent is the one under test, and it rerenders when the prop signal from the parent changes.

  function ParentComp(): JSX.Element {
    const [isExpanded, toggle] = useState(false)
    return (
      <>
        <button type="button" onClick={() => toggle(!isExpanded)}>
          simulate call
        </button>
        <ChildComponent signal={isExpanded} />
      </>
    )
  }
  
describe('ChildComponent', () => {  
  it("should control child component's prop", () => {
     cy.mount(<ParentComp />)
      
     cy.contains('simulate call').click()
     // we have set the prop
  })
 })

muratkeremozcan avatar Dec 22 '22 22:12 muratkeremozcan