0

I'm using the ui-bootstrap modal window and I'm trying to test a method that fires that modal window. My controller:

app.controller('AddProductController', ['$scope', 'ProductsService', '$uibModal', function ($scope, ProductsService, $uibModal) {
        $scope.product = {};
        $scope.searchCategories = function () {
            ProductsService.getRootCategories().then(function (data) {
                $scope.categories = data.data;
            });
            $scope.modalInstance = $uibModal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'categoryContent.html',
                controller: 'AddProductController',
                scope: $scope
            });
            $scope.modalInstance.result.then(function (category) {
                $scope.searchCategory = null;
                $scope.product.category = category;
            }, function () {
            });
        };
        $scope.ok = function(){
            $scope.modalInstance.close($scope.product.category);
        };
        $scope.cancel = function(){
            $scope.modalInstance.dismiss();
    }]);

And my test:

describe("Products Controller", function () {
beforeEach(function () {
    module('productsController');
});

beforeEach(function () {
    var ProductsService, createController, scope, rootScope,

    module(function ($provide) {
        $provide.value('ProductsService', {
            getRootCategories: function () {
                return {
                    then: function (callback) {
                        return callback({data: {name: 'category1'}});
                    }
                };
            },
        });

        $provide.value('$uibModal', {
            open : function(){
                return {
                    then: function (callback) {
                        return callback({data: {name: 'category1'}});
                    }
                };
            }
        });

        return null;
    });
});
describe('AddProductController', function () {
    beforeEach(function () {
        inject(function ($controller, _$rootScope_, _ProductsService_) {
            rootScope = _$rootScope_;
            scope = _$rootScope_.$new();
            ProductsService = _ProductsService_;
            createController = function () {
                return $controller("AddProductController", {
                    $scope: scope,
                });
            };
        });
    });
    it('calling searchCategories should make $scope.categories to be defined', function () {

        createController();
        expect(scope.categories).not.toBeDefined();  
        scope.searchCategories();
        expect(scope.categories).toBeDefined();
    });
});

});

All my tests pass,except this one, where I get TypeError: $scope.modalInstance.result is undefined. Any clues?

1 Answer 1

1

It seems you're not defining result in your mock modal. Try something like this:

$provide.value('$uibModal', {
    open: function () {
        return {
            result : {
                then: function (callback) {
                    return callback({ data: { name: 'category1' } });
                }
            }
        };
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.