I've got a jquery ajax call that looks something like this:
$.ajax({
type: "GET",
dataType: 'json',
data: data,
url: url
}).then((data, successFlag, xhr) => {
this.props.someFunc(data)
})
In my test file, I have the jquery ajax call stubbed out with sinon and it returns a resolved promise with data:
sinon.stub($, 'ajax')
.returns(Promise.resolve({ data: 'test data' }))
And I'm also spying on my someFunc(data) function. In my test, I'm calling a function that makes the ajax call, and then expecting my someFunc(data) to be called. However, the expectation fails, but when I put a console log in my someFunc(data) function, I can see that it is clearly being called:
component.instance().makeAjaxCall()
expect($.ajax.calledOnce).to.be.true // passes
expect(someFuncSpy.calledOnce).to.be.true // fails
Now I assume that it's failing because it's checking the expectation before then .then executes and I tried looking up some solutions dealing with testing with promises but nothing I've tried works so far (or I'm implementing it wrong). How can make sure that the .then finishes executing before I check the expectation?
Aucun commentaire:
Enregistrer un commentaire