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

147
Visualizações
Unable to mock event handler on test

So i have a very simple class which does the following:

  • Adds an event handler on an element on it's creation
  • The event handler is an instance method of the class
  • I need a cleanup method to remove the handler on demand
export default class TestClass {
  constructor() {
    this.element = document.getElementById("test");
    this.element.addEventListener("click", this.clickHandler);
  }

  clickHandler(e) {
    console.log("Click handler called");
  }

  cleanup() {
    this.element.removeEventListener("click", this.clickHandler);
  }
}

And i have a test where i stub the handler (i need to change it's behavior) and make some checks.

describe("Test", function () {
  beforeEach(function () {
    this.instance = new TestClass();
    this.clickHandlerStub = sinon.stub(this.instance, "clickHandler");
  });

  afterEach(function () {
    this.instance.cleanup();
    delete this.instance;
  });

  it("test case", function () {
    document.getElementById("test").click();
    expect(this.clickHandlerStub.calledOnce).to.equal(true);
  });
});

The expected behavior is to stub (override) the handler (so no logging should appear) and my expectation should pass since it should be called once when the element is clicked. However it seems that it keeps logging and the expectation fails.

If i change the way i bind the event handler and use an arrow function everything works fine.

this.element.addEventListener("click", () => this.clickHandler());

But then i can't remove the event handler in my cleanup method since addEventListener and removeEventListener need to pass the same handler reference in order to work properly.

I created a reproducable example in codesandbox in case it's helpful.

So what am i missing here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can stub TestClass's prototype.

this.clickHandlerStub = sinon
  .stub(TestClass.prototype, "clickHandler")
  .callsFake(() => {
    console.log(`Now it's calling the fake handler`);
  });

The cleanup would be

this.clickHandlerStub.restore();

https://codesandbox.io/s/mocha-unit-test-example-forked-sstfz?file=/src/index.js

References:

  • Restore sinon stub
  • The idea of stubing prototype

By the way, I prefer not to use this in global context. Instead, I'd create them as let variables.

describe('...', () => {
  let clickHandlerStub;

  beforeEach(() => {
    clickHandlerStub = ...
  });

  afterEach(() => {
    clickHandlerStub.restore();
  });
});
about 4 years ago · Juan Pablo Isaza Relatório
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