Skip to content

Commit 85427c2

Browse files
author
CALABRO Raphael (UA 2118)
committed
Added unit tests for new code and fixed onSortChange emissions
1 parent ccd9cfa commit 85427c2

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/DataTable.spec.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,16 @@ describe("DataTable directive tests", ()=> {
112112
datatable.ngOnChanges({rowsOnPage: new SimpleChange(2, 3, false)});
113113
datatable.ngDoCheck();
114114
expect(datatable.data).toEqual([{id: 3, name: 'banana'}, {id: 1, name: 'Duck'}, {id: 2, name: 'ącki'}]);
115-
116-
117115
});
116+
117+
it("should emit a dataLength of 0 when inputData is null or undefined", (done) => {
118+
datatable.onPageChange.subscribe((pageOptions: PageEvent)=> {
119+
expect(pageOptions.dataLength).toEqual(0);
120+
done();
121+
});
122+
datatable.inputData = null;
123+
datatable.setPage(2, 3);
124+
})
118125
});
119126

120127
describe("sorting", ()=> {
@@ -198,6 +205,20 @@ describe("DataTable directive tests", ()=> {
198205
]);
199206
});
200207

208+
it("should set sortOrder to 'asc' if setSort is given something else than 'asc' or 'desc'", () => {
209+
datatable.setSort("id", "bulb" as any);
210+
expect(datatable.getSort()).toEqual({sortBy: "id", sortOrder: "asc"});
211+
datatable.ngDoCheck();
212+
expect(datatable.sortOrder).toEqual("asc");
213+
expect(datatable.data).toEqual([
214+
{id: 1, name: 'Duck'},
215+
{id: 2, name: 'ącki'},
216+
{id: 3, name: 'banana'},
217+
{id: 4, name: 'Ananas'},
218+
{id: 5, name: 'Ðrone'}
219+
]);
220+
});
221+
201222
it("shouldn't change order when only order provided", (done)=> {
202223
done();
203224
datatable.onSortChange.subscribe(()=> {
@@ -275,6 +296,7 @@ describe("DataTable directive tests", ()=> {
275296
{name: 'Claire', age: 9},
276297
{name: 'Anna', age: 34},
277298
{name: 'Claire', age: 16},
299+
{name: 'Anna', age: 12},
278300
{name: 'Claire', age: 7},
279301
{name: 'Anna', age: 12}
280302
];
@@ -283,6 +305,7 @@ describe("DataTable directive tests", ()=> {
283305
datatable.ngDoCheck();
284306

285307
expect(datatable.data).toEqual([
308+
{name: 'Anna', age: 12},
286309
{name: 'Anna', age: 12},
287310
{name: 'Anna', age: 34},
288311
{name: 'Claire', age: 7},

src/DataTable.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ReplaySubject } from "rxjs";
66

77
export interface SortEvent {
88
sortBy: string|string[];
9-
sortOrder: string
9+
sortOrder: SortOrder
1010
}
1111

1212
export interface PageEvent {
@@ -58,7 +58,7 @@ export class DataTable implements OnChanges, DoCheck {
5858
this.sortBy = sortBy;
5959
this.sortOrder = ["asc","desc"].indexOf(sortOrder) >= 0 ? sortOrder : "asc";
6060
this.mustRecalculateData = true;
61-
this.onSortChange.next({sortBy: sortBy, sortOrder: sortOrder});
61+
this.onSortChange.next({sortBy: this.sortBy, sortOrder: this.sortOrder});
6262
this.sortByChange.emit(this.sortBy);
6363
this.sortOrderChange.emit(this.sortOrder);
6464
}
@@ -158,13 +158,7 @@ export class DataTable implements OnChanges, DoCheck {
158158
}
159159

160160
private compare(left: any, right: any): number {
161-
if (left === right) {
162-
return 0;
163-
}
164-
if (left == null || left > right) {
165-
return 1;
166-
}
167-
return -1;
161+
return left === right ? 0 : left == null || left > right ? 1 : -1;
168162
}
169163

170164
private sorter<T>(sortBy: string | string[], sortOrder: string): (left: T, right: T) => number {

0 commit comments

Comments
 (0)