goatslacker / goatslacker/alt

cannot test an Action that handles a promise

Open
#409 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.4k
Forks
312
PR merge metrics
No merged PRs in 30d

Description

Hi there I Have an Action that handles a promise and I am having problems testing it

the action has a private method handleResponse() to load data asynchronously it also has a Service injected via a setService() action.

var handleResponse = function(promise) {
submissionsActions.onLoadSubmissions({
processing: true,
loading: false
});

```
// Only show the loading message if the response takes more than half a second
var loadingTimer = setTimeout(function() {
submissionsActions.onLoadSubmissions({
processing: true,
loading: true
});
}.bind(this), 500);

promise
.then(function(submissions) {
submissionsActions.initStore(submissions);
submissionsActions.onLoadSubmissionsSuccess(submissions);
}.bind(this))
["finally"](function() {
clearInterval(loadingTimer);
})
["catch"](function(error) {
try {
submissionsActions.onLoadSubmissionsFail(error);
} catch (ex) {
console.error(ex);
}
}.bind(this));
};
```

var submissionsActions = alt.createActions({
loadSubmissions: function() {
handleResponse.call(this, service.getSubmissions());
},
setService: function(ser) {
console.log('Submissions Actions setting service',ser);
service = ser;
},
});

On my test I am using Sinon to mock the service and its response to getSubmissions:

service = {
getSubmissions: function() {
console.log('IWAS CALLED');
return {"submissions": []};
},
refreshSubmissions: function() {}
};

```
Actions.dispatch = function() {};
Actions.setService(service);

defer = Q.defer();
defer.resolve({"submissions": []});
sinon.stub(service, 'getSubmissions').returns(defer.promise);
Actions.loadSubmissions();

// Push Q
this.clock.tick();
```

when the promise is fulfilled: i.e
.then(function(submissions) {

I am getting the below error:
Uncaught ReferenceError: submissions is not defined
at AltAction.eval (eval at evaluate (unknown source), :1:1)
at Object.InjectedScript._evaluateOn (:895:55)
at Object.InjectedScript._evaluateAndWrap (:828:34)
at Object.InjectedScript.evaluateOnCallFrame (:954:21)
at AltAction.handleResponse

It seems that AltAction.eval() is ignoring the resolved promise.

Any help would be greatly appreciated.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.