Stuck on stubbing the location.href

Viewed 116

I'm trying to stub location.href from this reference: https://medium.com/cypress-io-thailand/understand-stub-spy-apis-that-will-make-your-test-better-than-ever-on-cypress-io-797cb9eb205a and found out the stub properties used is already deprecated so I do workaround with the new connotations:

stub(obj, 'meth').callsFake(fn)

After several tests, I got stuck because the stub seems not called properly. Here is my code look like:

const winSetLocationHrefStub = (url) => {    
expect(url).to.eq('setLocationHref')};


cy.window().then((win) => {
cy.stub(win,'setLocationHref').callsFake(winSetLocationHrefStub)});

cy.get(.button).click()

I knew that to stub a location.href property, we need to create fn on the main.js file, so here's the main.js code looks like

export const setLocationHref = (url) => {
    window.location.href = url; };

const WhatsAppEnquireButton = (props) => {
    useEffect(() => {
          window.setLocationHref = setLocationHref;
    }, []);
    const handleOnClick = (e) => {
          setLocationHref(formatWhatsAppUrl(foo, bar));

How to make it correctly stub the button I clicked?

0 Answers
Related