abundant-gold-84344
07/07/2022, 6:07 PMit.only("state mock", () => {
cy.stub(React,"useState").returns([101,cy.stub().as("setter")]);
mount(<Component2 />);
cy.get("@setter").should("have.been.calledOnce");
})
This is working fine. (Tried to mock useState)
How to stub a function or useState only when called with some particular value
eg:
1) const [num, setNum] = useState(10);
2)`const [num, setNum] = useState(50);`
I want to stub (1)
the useState when called with 10 not the (2)
Things i tried:
1)
const stub = cy.stub(React,"useState");
stub.callsFake((isFav) => {
console.log('',"Faking");
if(typeof isFav === 'boolean') {
console.log('boolean',);
return [true, cy.spy().as("setter")]
}
})
The above code throws the following error:
Uncaught TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
2)
const stub = cy.stub(React, "useState");
stub.callThrough()
stub.withArgs(Cypress.sinon.match.bool).returns([true,cy.spy().as("setter")])
The above test case is for the below button component:
const Button = ({isFavorite}:{isFavorite:boolean}) => {
const [isFavorited, setIsFavorited] = useState(isFavorite);
console.log(isFavorited, setIsFavorited);
.....
.....
.....
}
The console log gives the following: but it returns true based on stubbing.
false ƒ dispatchAction(fiber, queue, action) {
{
if (typeof arguments[3] === 'function') {
error("State updates from the useState() and useReducer() Hooks don't support the " + 'second callback a…
@gray-kilobyte-89541 Can you help me on this?