File size: 5,226 Bytes
7e79d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
describe('cf.common.components.starRating', function () {

    var $rootScope, createController, $controller;

    beforeEach(module('cf.common.components.starRating'));
    beforeEach(module('cf.partial'));

    beforeEach(inject(function (_$rootScope_, _$controller_) {
        $rootScope = _$rootScope_;
        $controller = _$controller_;
    }));

    describe('starRatingController', function () {

        var $scope;

        beforeEach(inject(function ($controller) {
            $scope = $rootScope.$new();
            createController = $controller('starRatingController', {$scope: $scope}, true);
        }));

        it('should load controller', function () {
            createController.instance.note = 3;
            createController.instance.max = 10;
            createController.instance.onChange = function () {
            };
            createController.instance.readonly = "";

            var vm = createController();

            expect(vm.note).toBe(3);
            expect(vm.max).toBe(10);
            expect(vm.onChange).toBeDefined();
            expect(vm.readonly).toBe("");
            expect(vm.isReadonly).toBe(true);
        });

        it('should generate stars', function () {
            createController.instance.note = 1;
            createController.instance.max = 10;

            var vm = createController();
            vm.updateStars();

            expect(vm.stars[0].filled).toBeTruthy();
            expect(vm.stars[1].filled).toBeFalsy();
        });

        it('should change note', function () {
            createController.instance.note = 1;
            createController.instance.max = 10;
            createController.instance.onChange = function () {
            };

            var vm = createController();
            vm.updateStars();
            vm.changeNote(2);
            vm.updateStars();


            expect(vm.stars[0].filled).toBeTruthy();
            expect(vm.stars[1].filled).toBeTruthy();
            expect(vm.stars[2].filled).toBeFalsy();
        });

        it('should change change note and call external function', function () {
            var spyFunction = jasmine.createSpy('onChange');
            createController.instance.note = 1;
            createController.instance.max = 10;
            createController.instance.onChange = spyFunction;

            var vm = createController();
            vm.updateStars();
            vm.changeNote(2);
            vm.updateStars();


            expect(vm.stars[0].filled).toBeTruthy();
            expect(vm.stars[1].filled).toBeTruthy();
            expect(vm.stars[2].filled).toBeFalsy();
            expect(spyFunction).toHaveBeenCalled();
        });

        it('should restrict change of note', () => {
            var spyFunction = jasmine.createSpy('onChange');
            createController.instance.note = 1;
            createController.instance.max = 10;
            createController.instance.onChange = spyFunction;
            createController.instance.readonly = "";

            var vm = createController();
            vm.updateStars();
            vm.changeNote(2);
            vm.updateStars();


            expect(vm.stars[0].filled).toBeTruthy();
            expect(vm.stars[1].filled).toBeFalsy();
            expect(vm.stars[2].filled).toBeFalsy();
            expect(spyFunction).not.toHaveBeenCalled();
        });

        it('should reset the note', () => {
            createController.instance.note = 1;
            createController.instance.max = 10;
            createController.instance.onChange = function () {
            };

            var vm = createController();
            vm.updateStars();
            vm.changeNote(1);
            vm.updateStars();

            expect(vm.note).toBe(0);
        });

        it('should be read Only with string', () => {
            createController.instance.note = 1;
            createController.instance.max = 10;
            createController.instance.readonly = "TruE";

            var vm = createController();
            vm.changeNote(3);

            expect(vm.note).toBe(1);
        });
    });

    describe('starRatingDirective', function () {
        var element, $scope, ctrl;

        beforeEach(inject(function ($controller, $compile, _$rootScope_) {

            $rootScope = _$rootScope_;
            $rootScope.comment = {
                note: 5
            };

            element = angular.element('<star-rating note="comment.note" max="10"></star-rating>');

            $scope = $rootScope.$new();
            spyOn($scope, '$watch').and.callThrough();
            $compile(element)($scope);
            $rootScope.$digest();
            element.controller();

            $scope = element.isolateScope() || element.scope();
            ctrl = $scope.src;
        }));


        it('should have an isolated scope with src as controllerAs', function () {
            expect($scope.src).toBeDefined();
            expect($scope.$watch).toHaveBeenCalled();
        });

        it('should update-stars', function () {
            spyOn(ctrl, 'updateStars');
            ctrl.note = 6;
            $rootScope.$digest();
            expect(ctrl.updateStars).toHaveBeenCalled();
        });
    });

});