mercredi 29 avril 2015

Is there a shorter way to mock an object in jasmine?

I have an angular filter function that relies on a $window function that has a very big namespace:

.filter('dbNonUtcToDate', function ($window) { //for fields like birthdate in EmployeeData which are not UTC
    return function (val, defaultVal) {
        if (val !== null) {

            var mdate = $window.moment($.jsonDateToDate(val)).format($window.Fr.Alpha.Rep.Web.context.dateFormat().toUpperCase());
            var nullMDate = $window.moment([1, 0, 1]).format($window.Fr.Alpha.Rep.Web.context.dateFormat().toUpperCase());
            if (mdate !== nullMDate) {
                return mdate;
            }
        }
        return defaultVal !== undefined ? defaultVal : '';
    };
})

Is there a way to mock the $window.Fr.Alpha.Rep.Web.context.dateFormat() without doing something like:

beforeEach(module(function ($provide) {
    var fakeWindow = {
        Fr:
        {
            Alpha:
            {
                Rep:
                {
                    Web:
                    {
                        context:
                        {
                            dateFormat: function () {
                                return ...;
                            }
                        }
                    }
                }
            }
        }
    }

    $provide.value('$window', fakeWindow);
}));

Aucun commentaire:

Enregistrer un commentaire