Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
Stuck on stubbing the location.href

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!