-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathSortManager_Tests.js
156 lines (108 loc) · 4.87 KB
/
SortManager_Tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/// <reference path="qunit/qunit.js" />
/// <reference path="../lib/jquery-1.7.js" />
/// <reference path="../lib/knockout-2.0.0.debug.js" />
/// <reference path="../koGrid.debug.js" />
window.getSortingTestData = function () {
return ko.observableArray([
{ 'sortIndex': 1, 'Sku': ko.observable('abcde'), 'Vendor': 'NEWB', 'ID': 5, 'SeasonCode': '110', 'Mfg_Id': ko.observable('573-9880954'), 'UPC': '822860449228', CreatedOn: new Date("12/4/1993"), ModOn: "12/4/1977" },
{ 'sortIndex': 2, 'Sku': ko.observable('cdefg'), 'Vendor': 'NIKE', 'ID': 8, 'SeasonCode': '11', 'Mfg_Id': ko.observable('780-8855467'), 'UPC': '043208523549', CreatedOn: new Date("12/4/1997"), ModOn: "1/1/1985" },
{ 'sortIndex': 3, 'Sku': ko.observable('CDFGH'), 'Vendor': 'REEB', 'ID': 12, 'SeasonCode': '1293', 'Mfg_Id': ko.observable('355-6906843'), 'UPC': '229487568922', CreatedOn: new Date("5/4/1993"), ModOn: "05/3/2011" },
{ 'sortIndex': 4, 'Sku': ko.observable('ACDEF'), 'Vendor': 'ADID', 'ID': 2, 'SeasonCode': '6283', 'Mfg_Id': ko.observable('861-4929378'), 'UPC': '644134774391', CreatedOn: new Date("12/2/1997"), ModOn: "2/2/2001" },
{ 'sortIndex': 5, 'Sku': ko.observable('GJHLH'), 'Vendor': 'ASIC', 'ID': 49, 'SeasonCode': null, 'Mfg_Id': ko.observable('566-6546541'), 'UPC': '229487568922', CreatedOn: new Date("5/5/1993"), ModOn: "05/2/2011" },
{ 'sortIndex': 6, 'Sku': ko.observable(), 'Vendor': 'BROOK', 'ID': 17, 'SeasonCode': '', 'Mfg_Id': ko.observable('654-6565646'), 'UPC': null, CreatedOn: new Date("12/3/1997"), ModOn: undefined }
]);
};
module("Sorting Tests");
test("No Sorting Test", function () {
var testData = getSortingTestData();
var mgr = new kg.SortManager({
data: testData
});
ok(mgr, "Sort Manager Instantiated");
equals(mgr.sortedData().length, 6, "No Sorting returns correct data");
});
test("Basic Sorting Test", function () {
var testData = getSortingTestData();
var mgr = new kg.SortManager({
data: testData
});
mgr.sort({ field: 'ID' }, "asc");
ok(mgr, "Sort Manager Instantiated");
equals(testData()[0].sortIndex, 4, "Sorted By ID Column correctly");
});
test("Number Sorting Test", function () {
var testData = getSortingTestData();
var mgr = new kg.SortManager({
data: testData
});
mgr.sort({ field: 'ID' }, "asc");
equals(testData()[0].sortIndex, 4, "First Item is correct");
equals(testData()[5].sortIndex, 5, "Last Item is correct");
});
test("Obs String Sorting Test", function () {
var testData = getSortingTestData();
var mgr = new kg.SortManager({
data: testData
});
mgr.sort({ field: 'Sku' }, "asc");
equals(testData()[0].sortIndex, 1, "First Item is correct");
equals(testData()[5].sortIndex, 6, "Last Item is correct"); // null items are sorted to last
});
test("Number String Sorting Test", function () {
var testData = getSortingTestData();
var mgr = new kg.SortManager({
data: testData
});
mgr.sort({ field: 'SeasonCode' }, "asc");
equals(testData()[0].sortIndex, 2, "First Item is correct");
equals(testData()[4].sortIndex, 5, "Empty String is greater than null for our logic in this case");
equals(testData()[5].sortIndex, 6, "Last Item is correct");
});
test("Date Sorting Test", function () {
var testData = getSortingTestData();
var mgr = new kg.SortManager({
data: testData
});
mgr.sort({ field: 'CreatedOn' }, "asc");
equals(testData()[0].sortIndex, 3, "First Item is correct");
equals(testData()[5].sortIndex, 2, "Last Item is correct");
});
test("Date String Sorting Test", function () {
var testData = getSortingTestData();
var mgr = new kg.SortManager({
data: testData
});
mgr.sort({ field: 'ModOn' }, "desc");
equals(testData()[0].sortIndex, 3, "First Item is correct");
equals(testData()[5].sortIndex, 6, "Last Item is correct");
});
test("Ensure sortInfo gets called", function () {
var testData = getSortingTestData();
var mySortInfo = ko.observable();
var gotCalled = false;
mySortInfo.subscribe(function () {
gotCalled = true;
});
var mgr = new kg.SortManager({
data: testData,
sortInfo: mySortInfo
});
mgr.sort({ field: 'ModOn' }, "desc");
ok(gotCalled, "Sort Info Subscription was called");
});
test("Use External Sorting Ignores Internal Sorting", function () {
var testData = getSortingTestData();
var mySortInfo = ko.observable();
var gotCalled = false;
mySortInfo.subscribe(function () {
gotCalled = true;
});
var mgr = new kg.SortManager({
data: testData,
sortInfo: mySortInfo,
useExternalSorting: true
});
mgr.sort({ field: 'ModOn' }, "desc");
ok(gotCalled, "Sort Info Subscription was called");
equals(testData()[0].sortIndex, 1, "First Item is correct");
});