Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save ajcrites/2fe2f27dbbad2b066c1f to your computer and use it in GitHub Desktop.

Select an option

Save ajcrites/2fe2f27dbbad2b066c1f to your computer and use it in GitHub Desktop.

failing tests fail, passing tests pass.

AngularJS HttpPromise methods (.success and .error) break the promise chain.

It is advisable to stick with classic Promise methods: .then and .catch.

More details in my blog post about this.

describe("foo", () => {
var $httpBackend;
var $http;
var $q;
var calledFunction = sinon.spy();
beforeEach(inject((_$httpBackend_, _$http_, _$q_) => {
$httpBackend = _$httpBackend_;
$http = _$http_;
$q = _$q_;
}));
it(`trigger error callback with rejection returned from HttpPromise.success`, () => {
$http.get("/foo")
.success(_ => $q.reject())
.error(calledFunction);
$httpBackend.expectGET("/foo").respond(200);
$httpBackend.flush();
expect(calledFunction).to.have.been.called;
});
it(`chain new promise value with returned promise resolution`, () => {
var value = "a new value";
$http.get("/foo")
.success(_ => $q.when(value))
.success(chainedValue => calledFunction(chainedValue));
$httpBackend.expectGET("/foo").respond(200, "not a new value");
$httpBackend.flush();
expect(calledFunction).to.have.been.calledWith(value);
});
});
module.exports = function (config) {
config.set({
frameworks: ["mocha", "chai-sinon"],
files: [
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js",
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-mocks.js",
"*.spec.es6",
],
browsers: ["PhantomJS"],
plugins: [
"karma-mocha",
"karma-chai-sinon",
"karma-phantomjs-launcher",
"karma-babel-preprocessor",
],
preprocessors: {
"*.spec.es6": ["babel"],
},
});
};
{
"dependencies": {
"chai": "^2.3.0",
"karma": "^0.12.31",
"karma-babel-preprocessor": "^5.2.1",
"karma-chai-sinon": "^0.1.4",
"karma-mocha": "^0.1.10",
"karma-phantomjs-launcher": "^0.1.4",
"mocha": "^2.2.4",
"sinon": "^1.14.1",
"sinon-chai": "^2.7.0",
"babel": "^5.4.7"
}
}
describe("foo", () => {
var $httpBackend;
var $http;
var $q;
var calledFunction = sinon.spy();
beforeEach(inject((_$httpBackend_, _$http_, _$q_) => {
$httpBackend = _$httpBackend_;
$http = _$http_;
$q = _$q_;
}));
it(`trigger error callback with rejection returned from HttpPromise.success`, () => {
$http.get("/foo")
.then(_ => $q.reject())
.catch(calledFunction);
$httpBackend.expectGET("/foo").respond(200);
$httpBackend.flush();
expect(calledFunction).to.have.been.called;
});
it(`chain new promise value with returned promise resolution`, () => {
var value = "a new value";
$http.get("/foo")
.then(_ => $q.when(value))
.then(chainedValue => calledFunction(chainedValue));
$httpBackend.expectGET("/foo").respond(200, "not a new value");
$httpBackend.flush();
expect(calledFunction).to.have.been.calledWith(value);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment