Angular Service with $http, $q and testing (test fixtures)

angular.module("myApp.services")
    .service("usersService", ["$http", '$q',
        function ($http, $q) {
            var _self = this;

            _self.getUserDetails = function () {
                return $q(function (resolve, reject) {
                    $http.get('/api/client/me/details')
                        .then(function (result) {
                            resolve(result.data);
                        })
                        .catch(function (error) {
                            reject(error);
                        });
                });
            };

            _self.setUserDetails = function (firstName, lastName) {
                return $q(function (resolve, reject) {
                    $http.post('/api/client/me/details', {
                        firstName: firstName,
                        lastName: lastName
                    })
                        .then(function (result) {
                            resolve(result.data);
                        })
                        .catch(function (error) {
                            reject(error);
                        });
                });
            };
        }]);

<hr />

describe("an usersService", function () {
    var target;

    var httpMock;
    var qMock;

    beforeEach(function () {
        httpMock = fixturesProvider.getSuccessHttpMock("api response");
        qMock = fixturesProvider.getQMock();

        module("myApp.services", function ($provide) {
            $provide.value("$http", httpMock);
            $provide.value("$q", qMock);
        });

        inject(function ($injector) {
            target = $injector.get("usersService");
        });
    });

    describe("#getUserDetails", function () {
        it("it should call the api", function () {
            spyOn(httpMock, "get").and.callThrough();
            target.getUserDetails();
            expect(httpMock.get).toHaveBeenCalledWith("/api/client/me/details")
        });

        it("it should return the result", function () {
            var result = target.getUserDetails();
            expect(result.resolved).toBeTruthy();
            expect(result.value).toBe("api response");
        });
    });

    describe("#setUserDetails", function () {
        it("it should call the api", function () {
            spyOn(httpMock, "post").and.callThrough();
            target.setUserDetails("John", "Doe");
            expect(httpMock.post).toHaveBeenCalledWith("/api/client/me/details", {
                firstName: "John",
                lastName: "Doe"
            })
        });

        it("it should return the result", function () {
            var result = target.setUserDetails("John", "Doe");
            expect(result.resolved).toBeTruthy();
            expect(result.value).toBe("api response");
        });
    });

    describe("when the api call fails", function () {
        beforeEach(function () {
            httpMock.get = fixturesProvider.getFailingHttpMethodMock(500);
            httpMock.post = fixturesProvider.getFailingHttpMethodMock(500);
        });

        describe("#getUserDetails", function () {
            it("it should return the error", function () {
                var result = target.getUserDetails();
                expect(result.resolved).toBeFalsy();
                expect(result.value.status).toBe(500);
            });
        });

        describe("#setUserDetails", function () {
            it("it should return the error", function () {
                var result = target.setUserDetails("John", "Doe");
                expect(result.resolved).toBeFalsy();
                expect(result.value.status).toBe(500);
            });
        });
    });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s