|
| 1 | +describe('rating directive', function () { |
| 2 | + var $rootScope, element; |
| 3 | + beforeEach(module('ui.bootstrap.rating')); |
| 4 | + beforeEach(module('template/rating/rating.html')); |
| 5 | + beforeEach(inject(function(_$compile_, _$rootScope_) { |
| 6 | + $compile = _$compile_; |
| 7 | + $rootScope = _$rootScope_; |
| 8 | + $rootScope.rate = 3; |
| 9 | + element = $compile('<rating value="rate"></rating>')($rootScope); |
| 10 | + $rootScope.$digest(); |
| 11 | + })); |
| 12 | + |
| 13 | + function getState(stars) { |
| 14 | + var state = []; |
| 15 | + for (var i = 0, n = stars.length; i < n; i++) { |
| 16 | + state.push( (stars.eq(i).hasClass('icon-star') && ! stars.eq(i).hasClass('icon-star-empty')) ); |
| 17 | + } |
| 18 | + return state; |
| 19 | + } |
| 20 | + |
| 21 | + it('contains the default number of icons', function() { |
| 22 | + expect(element.find('i').length).toBe(5); |
| 23 | + }); |
| 24 | + |
| 25 | + it('initializes the default star icons as selected', function() { |
| 26 | + var stars = element.find('i'); |
| 27 | + expect(getState(stars)).toEqual([true, true, true, false, false]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('handles correcty the click event', function() { |
| 31 | + var stars = element.find('i'); |
| 32 | + |
| 33 | + var star2 = stars.eq(1); |
| 34 | + star2.click(); |
| 35 | + $rootScope.$digest(); |
| 36 | + expect(getState(stars)).toEqual([true, true, false, false, false]); |
| 37 | + expect($rootScope.rate).toBe(2); |
| 38 | + |
| 39 | + var star5 = stars.eq(4); |
| 40 | + star5.click(); |
| 41 | + $rootScope.$digest(); |
| 42 | + expect(getState(stars)).toEqual([true, true, true, true, true]); |
| 43 | + expect($rootScope.rate).toBe(5); |
| 44 | + }); |
| 45 | + |
| 46 | + it('handles correcty the hover event', function() { |
| 47 | + var stars = element.find('i'); |
| 48 | + |
| 49 | + var star2 = stars.eq(1); |
| 50 | + star2.trigger('mouseover'); |
| 51 | + $rootScope.$digest(); |
| 52 | + expect(getState(stars)).toEqual([true, true, false, false, false]); |
| 53 | + expect($rootScope.rate).toBe(3); |
| 54 | + |
| 55 | + var star5 = stars.eq(4); |
| 56 | + star5.trigger('mouseover'); |
| 57 | + $rootScope.$digest(); |
| 58 | + expect(getState(stars)).toEqual([true, true, true, true, true]); |
| 59 | + expect($rootScope.rate).toBe(3); |
| 60 | + |
| 61 | + element.trigger('mouseout'); |
| 62 | + expect(getState(stars)).toEqual([true, true, true, false, false]); |
| 63 | + expect($rootScope.rate).toBe(3); |
| 64 | + }); |
| 65 | + |
| 66 | + it('changes the number of selected icons when value changes', function() { |
| 67 | + $rootScope.rate = 2; |
| 68 | + $rootScope.$digest(); |
| 69 | + |
| 70 | + var stars = element.find('i'); |
| 71 | + expect(getState(stars)).toEqual([true, true, false, false, false]); |
| 72 | + }); |
| 73 | + |
| 74 | + it('shows different number of icons when `max` attribute is set', function() { |
| 75 | + element = $compile('<rating value="rate" max="7"></rating>')($rootScope); |
| 76 | + $rootScope.$digest(); |
| 77 | + |
| 78 | + expect(element.find('i').length).toBe(7); |
| 79 | + }); |
| 80 | + |
| 81 | + it('handles readonly attribute', function() { |
| 82 | + $rootScope.isReadonly = true; |
| 83 | + element = $compile('<rating value="rate" readonly="isReadonly"></rating>')($rootScope); |
| 84 | + $rootScope.$digest(); |
| 85 | + |
| 86 | + var stars = element.find('i'); |
| 87 | + expect(getState(stars)).toEqual([true, true, true, false, false]); |
| 88 | + |
| 89 | + var star5 = stars.eq(4); |
| 90 | + star5.trigger('mouseover'); |
| 91 | + $rootScope.$digest(); |
| 92 | + expect(getState(stars)).toEqual([true, true, true, false, false]); |
| 93 | + |
| 94 | + $rootScope.isReadonly = false; |
| 95 | + $rootScope.$digest(); |
| 96 | + |
| 97 | + star5.trigger('mouseover'); |
| 98 | + $rootScope.$digest(); |
| 99 | + expect(getState(stars)).toEqual([true, true, true, true, true]); |
| 100 | + }); |
| 101 | + |
| 102 | +}); |
| 103 | + |
| 104 | +describe('setting ratingConfig', function() { |
| 105 | + var $rootScope, element; |
| 106 | + var originalConfig = {}; |
| 107 | + beforeEach(module('ui.bootstrap.rating')); |
| 108 | + beforeEach(module('template/rating/rating.html')); |
| 109 | + beforeEach(inject(function(_$compile_, _$rootScope_, ratingConfig) { |
| 110 | + $compile = _$compile_; |
| 111 | + $rootScope = _$rootScope_; |
| 112 | + $rootScope.rate = 5; |
| 113 | + angular.extend(originalConfig, ratingConfig); |
| 114 | + ratingConfig.max = 10; |
| 115 | + element = $compile('<rating value="rate"></rating>')($rootScope); |
| 116 | + $rootScope.$digest(); |
| 117 | + })); |
| 118 | + afterEach(inject(function(ratingConfig) { |
| 119 | + // return it to the original state |
| 120 | + angular.extend(ratingConfig, originalConfig); |
| 121 | + })); |
| 122 | + |
| 123 | + it('should change number of icon elements', function () { |
| 124 | + expect(element.find('i').length).toBe(10); |
| 125 | + }); |
| 126 | + |
| 127 | +}); |
| 128 | + |
0 commit comments