Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda