Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 64fff67

Browse files
committed
refactor(rating): add helpers to access stars in tests
1 parent 5b1115e commit 64fff67

File tree

1 file changed

+46
-56
lines changed

1 file changed

+46
-56
lines changed

src/rating/test/rating.spec.js

+46-56
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ describe('rating directive', function () {
1010
$rootScope.$digest();
1111
}));
1212

13-
function getState(stars) {
13+
function getStars() {
14+
return element.find('i');
15+
}
16+
17+
function getStar(number) {
18+
return getStars().eq( number - 1 );
19+
}
20+
21+
function getState() {
22+
var stars = getStars();
1423
var state = [];
1524
for (var i = 0, n = stars.length; i < n; i++) {
1625
state.push( (stars.eq(i).hasClass('icon-star') && ! stars.eq(i).hasClass('icon-star-empty')) );
@@ -19,130 +28,111 @@ describe('rating directive', function () {
1928
}
2029

2130
it('contains the default number of icons', function() {
22-
expect(element.find('i').length).toBe(5);
31+
expect(getStars().length).toBe(5);
2332
});
2433

2534
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]);
35+
expect(getState()).toEqual([true, true, true, false, false]);
2836
});
2937

3038
it('handles correctly the click event', function() {
31-
var stars = element.find('i');
32-
33-
var star2 = stars.eq(1);
34-
star2.click();
39+
getStar(2).click();
3540
$rootScope.$digest();
36-
expect(getState(stars)).toEqual([true, true, false, false, false]);
41+
expect(getState()).toEqual([true, true, false, false, false]);
3742
expect($rootScope.rate).toBe(2);
3843

39-
var star5 = stars.eq(4);
40-
star5.click();
44+
getStar(5).click();
4145
$rootScope.$digest();
42-
expect(getState(stars)).toEqual([true, true, true, true, true]);
46+
expect(getState()).toEqual([true, true, true, true, true]);
4347
expect($rootScope.rate).toBe(5);
4448
});
4549

4650
it('handles correctly the hover event', function() {
47-
var stars = element.find('i');
48-
49-
var star2 = stars.eq(1);
50-
star2.trigger('mouseover');
51+
getStar(2).trigger('mouseover');
5152
$rootScope.$digest();
52-
expect(getState(stars)).toEqual([true, true, false, false, false]);
53+
expect(getState()).toEqual([true, true, false, false, false]);
5354
expect($rootScope.rate).toBe(3);
5455

55-
var star5 = stars.eq(4);
56-
star5.trigger('mouseover');
56+
getStar(5).trigger('mouseover');
5757
$rootScope.$digest();
58-
expect(getState(stars)).toEqual([true, true, true, true, true]);
58+
expect(getState()).toEqual([true, true, true, true, true]);
5959
expect($rootScope.rate).toBe(3);
6060

6161
element.trigger('mouseout');
62-
expect(getState(stars)).toEqual([true, true, true, false, false]);
62+
expect(getState()).toEqual([true, true, true, false, false]);
6363
expect($rootScope.rate).toBe(3);
6464
});
6565

6666
it('changes the number of selected icons when value changes', function() {
6767
$rootScope.rate = 2;
6868
$rootScope.$digest();
6969

70-
var stars = element.find('i');
71-
expect(getState(stars)).toEqual([true, true, false, false, false]);
70+
expect(getState()).toEqual([true, true, false, false, false]);
7271
});
7372

7473
it('shows different number of icons when `max` attribute is set', function() {
7574
element = $compile('<rating value="rate" max="7"></rating>')($rootScope);
7675
$rootScope.$digest();
7776

78-
expect(element.find('i').length).toBe(7);
77+
expect(getStars().length).toBe(7);
7978
});
8079

8180
it('handles readonly attribute', function() {
8281
$rootScope.isReadonly = true;
8382
element = $compile('<rating value="rate" readonly="isReadonly"></rating>')($rootScope);
8483
$rootScope.$digest();
8584

86-
var stars = element.find('i');
87-
expect(getState(stars)).toEqual([true, true, true, false, false]);
85+
expect(getState()).toEqual([true, true, true, false, false]);
8886

89-
var star5 = stars.eq(4);
87+
var star5 = getStar(5);
9088
star5.trigger('mouseover');
9189
$rootScope.$digest();
92-
expect(getState(stars)).toEqual([true, true, true, false, false]);
90+
expect(getState()).toEqual([true, true, true, false, false]);
9391

9492
$rootScope.isReadonly = false;
9593
$rootScope.$digest();
9694

9795
star5.trigger('mouseover');
9896
$rootScope.$digest();
99-
expect(getState(stars)).toEqual([true, true, true, true, true]);
97+
expect(getState()).toEqual([true, true, true, true, true]);
10098
});
10199

102100
it('should fire onHover', function() {
103101
$rootScope.hoveringOver = jasmine.createSpy('hoveringOver');
104-
105102
element = $compile('<rating value="rate" on-hover="hoveringOver(value)"></rating>')($rootScope);
106103
$rootScope.$digest();
107104

108-
var star3 = element.find('i').eq(2);
109-
star3.trigger('mouseover');
105+
getStar(3).trigger('mouseover');
110106
$rootScope.$digest();
111107
expect($rootScope.hoveringOver).toHaveBeenCalledWith(3);
112108
});
113109

114110
it('should fire onLeave', function() {
115111
$rootScope.leaving = jasmine.createSpy('leaving');
116-
117112
element = $compile('<rating value="rate" on-leave="leaving()"></rating>')($rootScope);
118113
$rootScope.$digest();
119114

120115
element.trigger('mouseleave');
121116
$rootScope.$digest();
122117
expect($rootScope.leaving).toHaveBeenCalled();
123118
});
124-
});
125119

126-
describe('setting ratingConfig', function() {
127-
var $rootScope, element;
128-
var originalConfig = {};
129-
beforeEach(module('ui.bootstrap.rating'));
130-
beforeEach(module('template/rating/rating.html'));
131-
beforeEach(inject(function(_$compile_, _$rootScope_, ratingConfig) {
132-
$compile = _$compile_;
133-
$rootScope = _$rootScope_;
134-
$rootScope.rate = 5;
135-
angular.extend(originalConfig, ratingConfig);
136-
ratingConfig.max = 10;
137-
element = $compile('<rating value="rate"></rating>')($rootScope);
138-
$rootScope.$digest();
139-
}));
140-
afterEach(inject(function(ratingConfig) {
141-
// return it to the original state
142-
angular.extend(ratingConfig, originalConfig);
143-
}));
144-
145-
it('should change number of icon elements', function () {
146-
expect(element.find('i').length).toBe(10);
120+
describe('setting ratingConfig', function() {
121+
var originalConfig = {};
122+
beforeEach(inject(function(ratingConfig) {
123+
$rootScope.rate = 5;
124+
angular.extend(originalConfig, ratingConfig);
125+
ratingConfig.max = 10;
126+
element = $compile('<rating value="rate"></rating>')($rootScope);
127+
$rootScope.$digest();
128+
}));
129+
afterEach(inject(function(ratingConfig) {
130+
// return it to the original state
131+
angular.extend(ratingConfig, originalConfig);
132+
}));
133+
134+
it('should change number of icon elements', function () {
135+
expect(getStars().length).toBe(10);
136+
});
147137
});
148-
});
138+
});

0 commit comments

Comments
 (0)