Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.

Commit 184d0da

Browse files
author
Sgroi
committed
Unit testing for internal paging, sorting
1 parent 981e286 commit 184d0da

5 files changed

Lines changed: 143 additions & 32 deletions

File tree

src/components/DataTableController.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ export class DataTableController {
150150
* Sorts the values of the grid for client side sorting.
151151
*/
152152
onSorted(){
153-
if(!this.rows) return;
153+
if (!this.rows) {
154+
return;
155+
}
154156

155157
// return all sorted column, in the same order in which they were sorted
156158
var sorts = this.options.columns
@@ -175,16 +177,18 @@ export class DataTableController {
175177
c.sortPriority = i + 1;
176178
return c;
177179
});
178-
179-
if(sorts.length){
180-
this.onSort({sorts: sorts});
180+
181+
if (sorts.length) {
182+
if (this.onSort) {
183+
this.onSort({sorts: sorts});
184+
}
181185

182186
if (this.options.onSort){
183187
this.options.onSort(sorts);
184188
}
185189

186190
var clientSorts = [];
187-
for(var i=0, len=sorts.length; i < len; i++) {
191+
for (var i=0, len=sorts.length; i < len; i++) {
188192
var c = sorts[i];
189193
if(c.comparator !== false){
190194
var dir = c.sort === 'asc' ? '' : '-';
@@ -195,8 +199,8 @@ export class DataTableController {
195199
}
196200
}
197201
}
198-
199-
if(clientSorts.length){
202+
203+
if (clientSorts.length) {
200204
// todo: more ideal to just resort vs splice and repush
201205
// but wasn't responding to this change ...
202206
var sortedValues = this.$filter('orderBy')(this.rows, clientSorts);
@@ -205,7 +209,9 @@ export class DataTableController {
205209
}
206210
}
207211

208-
this.options.internal.setYOffset(0);
212+
if (this.options.internal && this.options.internal.setYOffset) {
213+
this.options.internal.setYOffset(0);
214+
}
209215
}
210216

211217
/**
Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,70 @@
11
import { DataTableController } from './DataTableController';
2+
import { TableDefaults } from '../defaults';
23

34
describe('DataTableController', function () {
4-
let $controller = null;
5+
let $controller = null,
6+
ctrl = null,
7+
scope = null,
8+
setController = null;
59

6-
beforeEach(
7-
inject((_$controller_) => {
8-
$controller = _$controller_;
9-
})
10-
);
10+
beforeEach(inject(($rootScope, $filter) => {
11+
scope = $rootScope.$new();
1112

12-
it('should export a function', () => {
13-
expect(DataTableController).toEqual(jasmine.any(Function));
14-
});
13+
setController = (bindings) => {
14+
bindings.options = Object.assign({}, TableDefaults, bindings.options);
15+
bindings.data = bindings.rows;
16+
17+
ctrl = new DataTableController(scope, $filter);
18+
19+
Object.assign(ctrl, bindings);
20+
};
21+
}));
22+
23+
describe('sorting', () => {
24+
25+
beforeEach(() => {
26+
let options = {
27+
columns: [
28+
{ prop: 'name', sort: 'asc' },
29+
{ prop: 'age'}
30+
]
31+
};
32+
let rows = [
33+
{ name: 'Walter', age: 49 },
34+
{ name: 'Dude', age: 45 },
35+
{ name: 'Donnie', age: 46 },
36+
{ name: 'Maude', age: 48 }
37+
];
38+
39+
setController({
40+
options: options,
41+
rows: rows
42+
});
43+
44+
ctrl.$onInit();
45+
});
46+
47+
it('should be sorted', () => {
48+
ctrl.onSorted();
49+
50+
let sortOrder = ctrl.rows[0].name < ctrl.rows[1].name;
51+
52+
expect(sortOrder).toBe(true);
53+
});
54+
55+
it('should re-sort', () => {
56+
ctrl.onSorted();
57+
58+
let sortedAscending = ctrl.rows[0].name < ctrl.rows[1].name;
59+
60+
expect(sortedAscending).toBe(true);
61+
62+
ctrl.options.columns[0].sort = 'desc';
63+
ctrl.onSorted();
1564

16-
describe('', function () {
65+
sortedAscending = ctrl.rows[0].name < ctrl.rows[1].name;
1766

67+
expect(sortedAscending).toBe(false);
68+
});
1869
});
1970
});

src/components/body/BodyController.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ export default class BodyController {
117117
this.buildInternalPage();
118118
}
119119

120-
this.onPage({
121-
offset: newVal,
122-
size: this.options.paging.size
123-
});
120+
if (this.onPage) {
121+
this.onPage({
122+
offset: newVal,
123+
size: this.options.paging.size
124+
});
125+
}
124126
}
125127
}));
126128
}
@@ -167,6 +169,7 @@ export default class BodyController {
167169
// We're using internal paging
168170
this.buildInternalPage();
169171
} else {
172+
// No paging
170173
this.tempRows.splice(0, this.tempRows.length);
171174
this.tempRows.push(...rows);
172175
}
@@ -390,7 +393,6 @@ export default class BodyController {
390393
addChildren(groupRows, toArray, level + 1);
391394
}
392395
}
393-
394396
});
395397
}
396398

@@ -452,7 +454,9 @@ export default class BodyController {
452454
rowIndex++;
453455
}
454456

455-
this.options.internal.styleTranslator.update(this.tempRows);
457+
if (this.options.internal && this.options.internal.styleTranslator) {
458+
this.options.internal.styleTranslator.update(this.tempRows);
459+
}
456460

457461
return this.tempRows;
458462
}

src/components/body/BodyController.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,56 @@ describe('BodyController', function () {
6363

6464
expect(ctrl.data).not.toEqual(0);
6565
});
66+
67+
it('should have the correct number of rows', () => {
68+
let options = {
69+
columns: [
70+
{ name: 'Name', prop: 'name' },
71+
{ name: 'Company', prop: 'company' }
72+
],
73+
paging: {
74+
offset: 0,
75+
size: 3
76+
}
77+
};
78+
79+
setController({
80+
options: options,
81+
rows: olympicRows
82+
});
83+
84+
ctrl.$onInit();
85+
scope.$digest();
86+
87+
expect(ctrl.tempRows.length).toBe(3);
88+
});
89+
90+
it('should increment page', () => {
91+
let options = {
92+
columns: [
93+
{ name: 'Name', prop: 'name' },
94+
{ name: 'Company', prop: 'company' }
95+
],
96+
paging: {
97+
offset: 0,
98+
size: 3
99+
}
100+
};
101+
102+
setController({
103+
options: options,
104+
rows: olympicRows
105+
});
106+
107+
ctrl.$onInit();
108+
scope.$digest();
109+
110+
let name = ctrl.tempRows[0].name;
111+
ctrl.options.paging.offset = 1;
112+
scope.$digest();
113+
114+
expect(ctrl.tempRows[0].name).not.toBe(name);
115+
});
66116
});
67117

68118
describe('when setting tree and group columns', () => {

src/components/body/BodyController.spechelper.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"Olympics": {
33
"Columns": [
44
{
5-
"name": "Athlete",
6-
"prop": "athlete",
5+
"name": "Name",
6+
"prop": "name",
77
"width": 300
88
},
99
{
@@ -22,7 +22,7 @@
2222
],
2323
"Rows": [
2424
{
25-
"athlete": "Michael Phelps",
25+
"name": "Michael Phelps",
2626
"age": 19,
2727
"country": "United States",
2828
"year": 2004,
@@ -34,7 +34,7 @@
3434
"total": 8
3535
},
3636
{
37-
"athlete": "Michael Phelps",
37+
"name": "Michael Phelps",
3838
"age": 27,
3939
"country": "United States",
4040
"year": 2012,
@@ -46,7 +46,7 @@
4646
"total": 6
4747
},
4848
{
49-
"athlete": "Natalie Coughlin",
49+
"name": "Natalie Coughlin",
5050
"age": 25,
5151
"country": "United States",
5252
"year": 2008,
@@ -58,7 +58,7 @@
5858
"total": 6
5959
},
6060
{
61-
"athlete": "Aleksey Nemov",
61+
"name": "Aleksey Nemov",
6262
"age": 24,
6363
"country": "Russia",
6464
"year": 2000,
@@ -70,7 +70,7 @@
7070
"total": 6
7171
},
7272
{
73-
"athlete": "Alicia Coutts",
73+
"name": "Alicia Coutts",
7474
"age": 24,
7575
"country": "Australia",
7676
"year": 2012,
@@ -82,7 +82,7 @@
8282
"total": 5
8383
},
8484
{
85-
"athlete": "Missy Franklin",
85+
"name": "Missy Franklin",
8686
"age": 17,
8787
"country": "United States",
8888
"year": 2012,

0 commit comments

Comments
 (0)