Angular JS – Test Fixtures

var fixturesProvider = new function () {
    var _this = this;

    //---------------
    // promises
    //---------------

    _this.getSuccessPromiseMock = function (result) {
        return {
            then: function (callback) {
                callback(result);
                return {
                    catch: function () {
                    }
                };
            }
        };
    };

    _this.getFailingPromiseMock = function (error) {
        return {
            then: function () {
                return {
                    catch: function (callback) {
                        callback(error);
                    }
                }
            }
        };
    };

    //---------------
    // $http
    //---------------

    _this.getHttpResultMock = function (result) {
        return {
            data: result
        };
    };

    _this.getHttpErrorMock = function (status) {
        return {
            status: status
        };
    };

    _this.getSuccessHttpMethodMock = function (result) {
        return function () {
            return _this.getSuccessPromiseMock(_this.getHttpResultMock(result))
        };
    };

    _this.getFailingHttpMethodMock = function (status) {
        return function () {
            return _this.getFailingPromiseMock(_this.getHttpErrorMock(status))
        };
    };

    _this.getSuccessHttpMock = function (result) {
        return {
            get: _this.getSuccessHttpMethodMock(result),
            post: _this.getSuccessHttpMethodMock(result)
        };
    };

    _this.getFailingHttpMock = function (status) {
        return {
            get: _this.getFailingHttpMethodMock(status),
            post: _this.getFailingHttpMethodMock(status)
        };
    };

    //---------------
    // services
    //---------------

    _this.getSuccessApiMethodMock = function (result) {
        return function () {
            return _this.getSuccessPromiseMock(result);
        };
    };

    _this.getFailingApiMethodMock = function (status) {
        return _this.getFailingHttpMethodMock(status);
    };

    //---------------
    // $q
    //---------------

    _this.getQMock = function () {
        return function (callback) {
            var result = null;

            callback(function (resolveValue) {
                result = {
                    resolved: true,
                    value: resolveValue
                };
            }, function (rejectValue) {
                result = {
                    resolved: false,
                    value: rejectValue
                };
            });

            return result;
        };
    };

    //---------------
    // modals
    //---------------

    _this.getUibModalMock = function () {
        return {
            open: function () {
                return {
                    result: {
                        then: function (callback) {
                            callback();
                        }
                    }
                };
            }
        };
    };

    _this.getUibModalInstanceMock = function () {
        return {
            dismiss: function () {
            },
            close: function () {
            }
        };
    };

    //---------------
    // $window
    //---------------

    _this.getWindowMock = function () {
        return {
            location: {
                href: ""
            }
        };
    };

    //---------------
    // $filter
    //---------------

    _this.getFilterMock = function () {
        return function () {
            return function () {
                return Array.prototype.join.call(arguments, "-");
            };
        };
    }
}();
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