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

275
Views
Mock window to have nested functions and values

I have the following method which works. I am trying to write a test for it.

How could I mock window.getStatus to return the values in the String so that I could mock a return value

and also be able to validate that the String was called with the correct value?

import get from 'lodash/get';

const getData = (name) => get(window.getStatus(),
    `data.toJS()[\'base-path\'].current[\'${name}'].exclusions[\'period\']`);

Tried the following test.

But it throws this error.

TypeError: window.getStatus is not a function

Test

describe('My Test', () => {

  let windowSpy;

  beforeEach(() => {
    windowSpy = jest.spyOn(window, "window", "get");
  });

  afterEach(() => {
    windowSpy.mockRestore();
  });

  it('should ', () => {

    windowSpy.mockImplementation(() => ({
      getStatus: {
        data: {
          'base-path': {
            current: {
              'login-name': {
                exclusions: {
                  'period': 'mock name',
                }
              }
            }
          }
        }
      }
    }));
    const result = getData('login-name');
    expect(result).toEqual('mock name');
  });
});
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!