jeudi 30 novembre 2017

Test method call in promise then

I'm currently developing a react web application. I do testing with jest, babel-rewire-api, and shallow testing.

This test is a simple test with spy's to check if method are called the right amount of times.

Method/Function to test:

handleFoo() {
if (createDialog(`Are you sure you want to delete the record?`)) {
  if (this.props.barId) {
    this.props.deleteBar(this.props.barId).then(result => {
      this.props.deleteFoo(this.props.id);
    });
  }
}

Test written:

test("FooComponent : handleFoo : user confirmed, action called", () => {
  // Arrange
  const fooCompoment = Rewire.__GetDependency__("fooComponent");
  const confirmSpy = jest.fn(() => true);
  const deleteFooSpy = jest.fn(postId => true);
  const deleteBarSpy = jest.fn(fileId => Promise.resolve(true));
  const context = {
    props: {
      deleteBar: deleteBarSpy,
      deleteFoo: deleteFooSpy
    }
  };
  Rewire.__Rewire__("createDialog", confirmSpy);

  // Act
  FooComponent.prototype.handleFoo.call(context, null);

  // Assert
  expect(confirmSpy).toHaveBeenCalledTimes(1);
  expect(deleteFooSpy).toHaveBeenCalledTimes(1);

  Rewire.__ResetDependency__("createDialog");
});

The problem is that this.props.deleteFoo is not run according to the test result. If i run the software i know it works as expected and is called, but i want to have a test for it

Aucun commentaire:

Enregistrer un commentaire