forked from ~ulysseskao/xdashangular

Dennis Kao
2016-04-25 ce0f5117e5830ade396e88594accf66c20200c81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
describe('xdashangular.index module', function() {
 
    beforeEach(angular.mock.module('xdashangular.index', function() {
    }));
 
    describe("IndexController", function() {
        var controller, deferred, applicationDataFactory, scope;
 
        beforeEach(angular.mock.inject(function($q, $controller, $rootScope) {
 
            applicationDataFactory = {
                get: function() {
                    deferred = $q.defer();
                    return deferred.promise;
                }
            };
 
            spyOn(applicationDataFactory, 'get').and.callThrough();
 
            scope = $rootScope.$new();
            controller = $controller('IndexController', {$scope: scope, applicationDataFactory: applicationDataFactory, contextPath: "/GrailsApp"});
        }));
 
        beforeEach(function() {
            expect(applicationDataFactory.get.calls.count()).toEqual(1);
        });
 
        it("should assign the contextPath to a local variable on the vm", function() {
            expect(controller.contextPath).toEqual('/GrailsApp');
        });
 
        it("should call the applicationDataFactory and assign data on success", function() {
            deferred.resolve({data: "Hello"});
            scope.$digest();
            expect(controller.applicationData).toEqual("Hello");
        });
 
        it("should call the applicationDataFactory and do nothing on error", function() {
            deferred.reject();
            scope.$digest();
            expect(controller.applicationData).not.toBeDefined();
        });
 
    });
 
});