Skip to content

Commit b036609

Browse files
Portugal, Marcelomportuga
authored andcommitted
test(ui-grid-render-container): Improving code coverage of ui-grid render container.
1 parent 07644ab commit b036609

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
(function() {
2+
'use strict';
3+
4+
describe('uiGridRenderContainer', function() {
5+
var $compile, $document, $rootScope, $scope, $templateCache, data, grid, gridUtil, ScrollEvent,
6+
atTopSpy, atBottomSpy, atLeftSpy, atRightSpy, fireThrottledScrollingEventSpy;
7+
8+
function recompile() {
9+
grid = angular.element('<div style="width: 500px; height: 300px" ui-grid="gridOpts"></div>');
10+
11+
$compile(grid)($scope);
12+
$document[0].body.appendChild(grid[0]);
13+
14+
$scope.$apply();
15+
}
16+
17+
beforeEach(function() {
18+
fireThrottledScrollingEventSpy = jasmine.createSpy('fireThrottledScrollingEvent');
19+
ScrollEvent = function() {
20+
return {
21+
atTop: atTopSpy,
22+
atBottom: atBottomSpy,
23+
atLeft: atLeftSpy,
24+
atRight: atRightSpy,
25+
fireThrottledScrollingEvent: fireThrottledScrollingEventSpy
26+
};
27+
};
28+
ScrollEvent.Sources = {
29+
RenderContainerMouseWheel: 'RenderContainerMouseWheel'
30+
};
31+
32+
module('ui.grid', function($provide) {
33+
$provide.value('ScrollEvent', ScrollEvent);
34+
});
35+
36+
inject(function(_$compile_, _$document_, _$rootScope_, _$templateCache_, _gridUtil_) {
37+
$compile = _$compile_;
38+
$document = _$document_;
39+
$rootScope = _$rootScope_;
40+
$templateCache = _$templateCache_;
41+
gridUtil = _gridUtil_;
42+
});
43+
44+
$templateCache.put('ui-grid/header-render-container', '<div ui-grid-render-container></div>');
45+
$scope = $rootScope.$new();
46+
47+
data = [
48+
{name: 'Bob', age: 35},
49+
{name: 'Bill', age: 25},
50+
{name: 'Sam', age: 17},
51+
{name: 'Jane', age: 19}
52+
];
53+
54+
$scope.gridOpts = {
55+
headerTemplate: 'ui-grid/header-render-container',
56+
data: data
57+
};
58+
});
59+
describe('when something goes wrong', function() {
60+
it('should throw an error when row container name is not defined', function(done) {
61+
try {
62+
recompile();
63+
} catch (error) {
64+
expect(error).toEqual('No row render container name specified');
65+
grid.remove();
66+
done();
67+
}
68+
});
69+
it('should throw an error when column container name is not defined', function(done) {
70+
$templateCache.put('ui-grid/header-render-container', '<div ui-grid-render-container row-container-name="\'header\'"></div>');
71+
try {
72+
recompile();
73+
} catch (error) {
74+
expect(error).toEqual('No column render container name specified');
75+
grid.remove();
76+
done();
77+
}
78+
});
79+
it('should throw an error when the row container is not defined', function(done) {
80+
$templateCache.put('ui-grid/header-render-container',
81+
'<div ui-grid-render-container row-container-name="\'mock\'" col-container-name="\'mock\'"></div>');
82+
try {
83+
recompile();
84+
} catch (error) {
85+
expect(error).toEqual('Row render container \'mock\' is not registered.');
86+
grid.remove();
87+
done();
88+
}
89+
});
90+
it('should throw an error when the column container is not defined', function(done) {
91+
$templateCache.put('ui-grid/header-render-container',
92+
'<div ui-grid-render-container row-container-name="\'body\'" col-container-name="\'mock\'"></div>');
93+
try {
94+
recompile();
95+
} catch (error) {
96+
expect(error).toEqual('Column render container \'mock\' is not registered.');
97+
grid.remove();
98+
done();
99+
}
100+
});
101+
});
102+
describe('when the user scrolls with the mouse wheel', function() {
103+
var scrollEvent, mouseWheelCallback;
104+
105+
beforeEach(function() {
106+
$scope.gridOpts = {
107+
data: data
108+
};
109+
scrollEvent = new $.Event('mousewheel');
110+
scrollEvent.deltaFactor = 1;
111+
spyOn(scrollEvent, 'preventDefault').and.callThrough();
112+
spyOn(scrollEvent, 'stopPropagation').and.callThrough();
113+
spyOn(gridUtil.on, 'mousewheel').and.callFake(function(elm, callback) {
114+
scrollEvent.deltaY = 0;
115+
scrollEvent.deltaX = 0;
116+
mouseWheelCallback = callback;
117+
});
118+
spyOn(gridUtil, 'normalizeScrollLeft').and.callFake(angular.noop);
119+
recompile();
120+
});
121+
afterEach(function() {
122+
grid.remove();
123+
gridUtil.on.mousewheel.calls.reset();
124+
gridUtil.normalizeScrollLeft.calls.reset();
125+
});
126+
it('should call the on mousewheel even handler in gridUtil', function() {
127+
expect(gridUtil.on.mousewheel).toHaveBeenCalled();
128+
});
129+
130+
function testParentContainerScrolls() {
131+
it('should not prevent the default behavior', function() {
132+
expect(scrollEvent.preventDefault).not.toHaveBeenCalled();
133+
});
134+
it('should not stop propagation of the event', function() {
135+
expect(scrollEvent.stopPropagation).not.toHaveBeenCalled();
136+
});
137+
it('should not throttle scrolling', function() {
138+
expect(fireThrottledScrollingEventSpy).not.toHaveBeenCalled();
139+
});
140+
}
141+
describe('when deltaX and deltaY are equal to zero', function() {
142+
beforeEach(function() {
143+
scrollEvent.deltaY = 0;
144+
scrollEvent.deltaX = 0;
145+
mouseWheelCallback(scrollEvent);
146+
});
147+
afterEach(function() {
148+
scrollEvent.preventDefault.calls.reset();
149+
scrollEvent.stopPropagation.calls.reset();
150+
fireThrottledScrollingEventSpy.calls.reset();
151+
});
152+
it('should prevent the default behavior', function() {
153+
expect(scrollEvent.preventDefault).toHaveBeenCalled();
154+
});
155+
it('should stop propagation of the event', function() {
156+
expect(scrollEvent.stopPropagation).toHaveBeenCalled();
157+
});
158+
it('should throttle scrolling', function() {
159+
expect(fireThrottledScrollingEventSpy).toHaveBeenCalled();
160+
});
161+
});
162+
describe('when deltaX is different than 0 and user scrolled to the left', function() {
163+
beforeEach(function() {
164+
scrollEvent.deltaY = 0;
165+
scrollEvent.deltaX = -50;
166+
atLeftSpy = jasmine.createSpy('atLeft').and.returnValue(true);
167+
mouseWheelCallback(scrollEvent);
168+
});
169+
afterEach(function() {
170+
atLeftSpy.calls.reset();
171+
scrollEvent.preventDefault.calls.reset();
172+
scrollEvent.stopPropagation.calls.reset();
173+
fireThrottledScrollingEventSpy.calls.reset();
174+
});
175+
testParentContainerScrolls();
176+
});
177+
describe('when deltaX is different than 0 and user scrolled to the right', function() {
178+
beforeEach(function() {
179+
scrollEvent.deltaY = 0;
180+
scrollEvent.deltaX = 50;
181+
atLeftSpy = jasmine.createSpy('atLeft').and.returnValue(false);
182+
atRightSpy = jasmine.createSpy('atRight').and.returnValue(true);
183+
mouseWheelCallback(scrollEvent);
184+
});
185+
afterEach(function() {
186+
atLeftSpy.calls.reset();
187+
atRightSpy.calls.reset();
188+
scrollEvent.preventDefault.calls.reset();
189+
scrollEvent.stopPropagation.calls.reset();
190+
fireThrottledScrollingEventSpy.calls.reset();
191+
});
192+
testParentContainerScrolls();
193+
});
194+
describe('when deltaY is different than 0 and user scrolled to the top', function() {
195+
beforeEach(function() {
196+
scrollEvent.deltaY = -50;
197+
scrollEvent.deltaX = 0;
198+
atTopSpy = jasmine.createSpy('atTop').and.returnValue(true);
199+
mouseWheelCallback(scrollEvent);
200+
});
201+
afterEach(function() {
202+
atTopSpy.calls.reset();
203+
scrollEvent.preventDefault.calls.reset();
204+
scrollEvent.stopPropagation.calls.reset();
205+
fireThrottledScrollingEventSpy.calls.reset();
206+
});
207+
testParentContainerScrolls();
208+
});
209+
describe('when deltaY is different than 0 and user scrolled to the bottom', function() {
210+
beforeEach(function() {
211+
scrollEvent.deltaY = 50;
212+
scrollEvent.deltaX = 0;
213+
atTopSpy = jasmine.createSpy('atTop').and.returnValue(false);
214+
atBottomSpy = jasmine.createSpy('atBottom').and.returnValue(true);
215+
mouseWheelCallback(scrollEvent);
216+
});
217+
afterEach(function() {
218+
atTopSpy.calls.reset();
219+
atBottomSpy.calls.reset();
220+
scrollEvent.preventDefault.calls.reset();
221+
scrollEvent.stopPropagation.calls.reset();
222+
fireThrottledScrollingEventSpy.calls.reset();
223+
});
224+
testParentContainerScrolls();
225+
});
226+
});
227+
});
228+
})();

0 commit comments

Comments
 (0)