/
unittest.html
198 lines (170 loc) · 7.13 KB
/
unittest.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="../widget/javascript/jquery.js"></script>
<script src="../widget/javascript/kivadata.js"></script>
<script src="fixtures/loans-simple.js"></script>
<script src="fixtures/loans-two-pages.js"></script>
<script src="../widget/javascript/map.js"></script>
<link rel="stylesheet" href="testsuite.css" type="text/css" media="screen" />
<script>
$(document).ready(function(){
module("Latitude/longitude conversion");
test("Basic latLongToPixels tests", function() {
var x, y;
[x,y] = mapMaker.latLongToPixels(12.5833, -16.2719);
equals(x, 118, "The 'x' is correct");
equals(y, 250, "The 'y' is correct");
});
module("eachLoan iterator");
test("eachLoan simple test", function() {
var kivaData = new KivaData();
kivaData.loanInfo = loansSimple;
var loanList = [];
kivaData.eachLoan(function () {
loanList.push(this);
});
equals(loanList.length, 3,
"Should have iterated over 3 loans");
equals(loanList[0].name, 'Fatoumata Coly',
"Name of first loan should be correct");
equals(loanList[1].name, 'Marie Gomis',
"Name of second loan should be correct");
equals(loanList[2].name, 'Lydia Afriyie',
"Name of third loan should be correct");
});
module("Data fetching");
test("fetchLoanData function test", function() {
var kivaData = new KivaData();
kivaData.dataSource = function (n) { return "function value" };
var result = undefined;
kivaData.fetchLoanData(1, function (data) { result = data; });
equals(result, 'function value',
"fetchLoanData should call dataSource if it's a function");
});
test("fetchLoanData object test", function() {
var kivaData = new KivaData();
kivaData.dataSource = ['some', 'list'];
var result = undefined;
kivaData.fetchLoanData(1, function (data) { result = data; });
equals(result.length, 2, "The result list should have two elements");
equals(result[0], 'some', "The result's first element should be 'some'");
equals(result[1], 'list', "The result's second element should be 'list'");
});
test("refresh method", function() {
var kivaData = new KivaData();
kivaData.dataSource = ['some', 'list'];
var result = undefined;
kivaData.fetchLoanData(1, function (data) { result = data; });
equals(result.length, 2, "The result list should have two elements");
equals(result[0], 'some', "The result's first element should be 'some'");
equals(result[1], 'list', "The result's second element should be 'list'");
});
test("addLoans method", function() {
var kivaData = new KivaData();
equals(kivaData.loanInfo.loans.length, 0,
"There should be no loans initially");
kivaData.addLoans({'loans': [{'foo': 'bar'}, {'qux': 'flux'}]});
equals(kivaData.loanInfo.loans.length, 2,
"There should be 2 loans");
// Load at most one more loan
kivaData.addLoans({'loans': [{'third': 'and last'},
{'extra': 'should not load'}]},
1);
equals(kivaData.loanInfo.loans.length, 3,
"atMost parameter should be honoured");
// Less loans than atMost
kivaData.addLoans({'loans': [{'one': 'first'}, {'two': 'last'}]},
3);
equals(kivaData.loanInfo.loans.length, 5,
"Nothing strange should happen if there are less loans than atMost");
});
test("loadLoans infinite loop protection", function() {
expect(1);
var kivaData = new KivaData();
kivaData.numberLoans = 3;
// Dummy data source, never returns loans
kivaData.dataSource = {'loans': []}
kivaData.loadLoans(1, function () {
equals(kivaData.loanInfo.loans.length, 0,
"If there are no loans, it should not get into an infinite loop");
});
});
test("loadLoans pagination tests", function() {
expect(18);
var kivaData = new KivaData();
kivaData.numberLoans = 3;
// This data source will iterate over the pages in the loanPages variable
kivaData.dataSource = function (counter) {
return loanPages[(counter-1) % loanPages.length]
};
equals(kivaData.loanInfo.loans.length, 0,
"There should be no loans initially");
kivaData.loadLoans(1, function () {
equals(kivaData.loanInfo.loans.length, 3,
"There should be 3 loans");
equals(kivaData.loanInfo.loans[0].name, 'Fatoumata Coly',
"The name for the first loan should be correct");
equals(kivaData.loanInfo.loans[1].name, 'Marie Gomis',
"The name for the second loan should be correct");
equals(kivaData.loanInfo.loans[2].name, 'Lydia Afriyie',
"The name for the third loan should be correct");
});
// Again, now needing one loan from the second page
var kivaDataSecondPage = new KivaData();
kivaDataSecondPage.numberLoans = 4;
// This data source will iterate over the pages in the loanPages variable
kivaDataSecondPage.dataSource = function (counter) {
return loanPages[(counter-1) % loanPages.length]
};
equals(kivaDataSecondPage.loanInfo.loans.length, 0,
"There should be no loans initially");
kivaDataSecondPage.loadLoans(1, function () {
equals(kivaDataSecondPage.loanInfo.loans.length, 4,
"There should be 4 loans");
equals(kivaDataSecondPage.loanInfo.loans[0].name, 'Fatoumata Coly',
"The name for the first loan should be correct");
equals(kivaDataSecondPage.loanInfo.loans[1].name, 'Marie Gomis',
"The name for the second loan should be correct");
equals(kivaDataSecondPage.loanInfo.loans[2].name, 'Lydia Afriyie',
"The name for the third loan should be correct");
equals(kivaDataSecondPage.loanInfo.loans[3].name, 'Esther Adu',
"The name for the fourth loan should be correct");
});
// Again, now needing one loan from the second page
var kivaDataThreePages = new KivaData();
kivaDataThreePages.numberLoans = 8;
// This data source will iterate over the pages in the loanPages variable
kivaDataThreePages.dataSource = function (counter) {
return loanPages[(counter-1) % loanPages.length]
};
equals(kivaDataThreePages.loanInfo.loans.length, 0,
"There should be no loans initially");
kivaDataThreePages.loadLoans(1, function () {
equals(kivaDataThreePages.loanInfo.loans.length, 8,
"There should be 8 loans");
equals(kivaDataThreePages.loanInfo.loans[0].name, 'Fatoumata Coly',
"The name for the first loan should be correct");
equals(kivaDataThreePages.loanInfo.loans[1].name, 'Marie Gomis',
"The name for the second loan should be correct");
equals(kivaDataThreePages.loanInfo.loans[2].name, 'Lydia Afriyie',
"The name for the third loan should be correct");
equals(kivaDataThreePages.loanInfo.loans[3].name, 'Esther Adu',
"The name for the fourth loan should be correct");
equals(kivaDataThreePages.loanInfo.loans[7].name, 'Fatoumata Coly',
"The name for the eighth loan should be correct");
});
});
});
</script>
</head>
<body>
<script type="text/javascript" src="testrunner.js"></script>
<h1>QUnit test suite</h1>
<h2 id="banner"></h2>
<h2 id="userAgent"></h2>
<ol id="tests"></ol>
<div id="main"></div>
</body>
</html>