-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
331 lines (285 loc) · 12 KB
/
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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Setup sample data for tests
var BOOK_JSON = {
"id": "59",
"title": "Adventures of Huckleberry Finn",
"description": "The Adventures of Huckleberry Finn is a novel by Mark Twain",
"url_zip_file": "google.com/coolstuff.zip"
};
var BOOK_XML =
'<rss> <channel>' +
'<item> <title><![CDATA[Chapter 01]]></title>' +
'<enclosure url="http://example.com/ch1.mp3" length="4.2MB" type="audio/mpeg" />' +
'</item>' +
'<item> <title><![CDATA[Chapter 02]]></title>' +
'<enclosure url="http://example.com/ch2.mp3" length="4.4MB" type="audio/mpeg" />' +
'</item>' +
'</channel> </rss>';
var BOOK_OBJECT = {
"description": "How to garden.",
"title": "How do Gardening?",
"id": "1234",
"fullBookURL": "http://www.example.com/full_mp3.zip"
};
var CHAPTERS_ARR = [
{
title: "Foreword and Contents",
index: 0,
url: "http://www.example.com/gardening_00.mp3",
position: 0
},
{
title: "Chapter 1",
index: 1,
url: "http://www.example.com/gardening_01.mp3",
position: 0
}
];
var CHAPTER_OBJECT = CHAPTERS_ARR[0];
// Begin testing
describe('#stripHTMLTags()', function () {
it('removes all angle bracket pairs and enclosing strings', function () {
expect(stripHTMLTags('<html>no<em> html</em><garbage\n-tag> tags.')).equal('no html tags.');
});
it('only removes text enclosed in brackets', function () {
expect(stripHTMLTags('> Look, left and right angle brackets! <.')).equal('> Look, left and right angle brackets! <.');
});
});
describe('Book()', function () {
it('should create an instance of Book', function () {
expect(new Book({
'json': BOOK_JSON
})).instanceOf(Book); // Is this test really necessary -- seems to be testing javascript base behavior and not our code itself
});
it('should create an id field for the book, if available', function () {
expect(new Book({
'json': BOOK_JSON
}).id).equal('59');
});
it('should create a title field for the book, if available, with stripped HTML tags', function () {
expect(new Book({
'json': BOOK_JSON
}).title).equal("Adventures of Huckleberry Finn");
});
it('should create a description field for the book, if available, with stripped HTML tags', function () {
assert.equal("The Adventures of Huckleberry Finn is a novel by Mark Twain", new Book({
'json': BOOK_JSON
}).description);
});
it('should create an object for the zip file url for the book, if available', function () {
expect(new Book({
'json': BOOK_JSON
}).fullBookURL).equal("google.com/coolstuff.zip");
});
});
describe('ChaptersListPageGenerator()', function () {
describe('#generatePage()', function () {
before(function () {
var ul = $('<ul data-role="listview" id="chapters-list"></ul>');
ul.appendTo('body');
ul.listview(); // initializes jQuery mobile listview - necessary for #listview('refresh') within #generatePage
function StubHttpRequestHandler() {
this.getXML = function (url, load_callback, other_args) {
load_callback({
'response': BOOK_XML
});
}
}
var cpg = new ChaptersListPageGenerator({
'httpRequestHandler': new StubHttpRequestHandler(),
'selector': '#chapters-list'
});
cpg.generatePage(new Book({
'json': BOOK_JSON
}));
});
it('appends elements with chapter titles to selected parent element', function () {
expect($('#chapters-list').children().length).equal(2);
expect($('#chapters-list').children()[1].textContent).match(/^Chapter 02$/);
});
it('adds chapter-index attributes to each element in the selected parent', function () {
$('#chapters-list').children().each(function (i) {
expect($(this).attr('chapter-index')).equal(i + '');
});
});
});
// Should we test private methods? I don't think it's necessary.
});
describe('SearchResltsPageGenerator()', function () {
describe('#generatePage()', function () {
var spg,
bsrSelector,
books_response;
var httpReqUrl;
before(function () {
bsrSelector = '#bookSearchResults';
var ul = $('<ul data-role="listview" id="bookSearchResults"></ul>');
ul.appendTo('body');
ul.listview();
books_response = [BOOK_JSON, {
'id': 1234,
'title': 'placeholder book',
'description': 'this is a description'
}];
function StubHttpRequestHandler() {
this.getJSON = function (url, load_callback, other_args) {
httpReqUrl = url; // TODO: this is an ugly way to expose url argument
var response_arr = url.match(/\^NORESULT\?/) ? undefined : books_response;
load_callback({
'response': {
'books': response_arr
}
}); // simulate LibriVox JSON title search response
}
}
spg = new SearchResltsPageGenerator({
'httpRequestHandler': new StubHttpRequestHandler(),
'selector': bsrSelector
});
});
afterEach(function () {
$(bsrSelector).empty(); // I think Mocha / Karma might also clear #bSR, commenting this line has no effect on test outcomes
});
it('appends elements containing book results to selected parent element', function () {
spg.generatePage('abc');
expect($(bsrSelector).children().length).equal(2);
});
it('generates a LibriVox API url', function () {
spg.generatePage('abcdefg');
var url_passed_in = httpReqUrl;
expect(url_passed_in).equal("https://librivox.org/api/feed/audiobooks/title/^abcdefg?&format=json");
});
it('populates elements with book titles and descriptions', function () {
spg.generatePage('abc');
var secondBookResult = $(bsrSelector).children()[1];
var secondBookText = $(secondBookResult).text(); // these are implementation details, should the test just check whether the text exists?
expect(secondBookText).match(/placeholder book/);
expect(secondBookText).match(/this is a description/);
});
it('adds book-id attributes to each element in the selected parent', function () {
spg.generatePage('abc');
$('#bookSearchResults').children().each(function (i) {
expect($(this).attr('book-id')).equal(books_response[i].id + ''); // no == #equals() in expect()?? Why?
});
});
it('displays a message if no books are found', function () {
spg.generatePage('NORESULT');
expect($(bsrSelector).html()).match(/no books found/i);
});
it('clears results from a previous search before appending new elements', function () {
spg.generatePage('abc');
spg.generatePage('def');
expect($(bsrSelector).children().length).to.equal(2);
spg.generatePage('NORESULT');
spg.generatePage('abc');
expect($(bsrSelector).text()).not.match(/no books found/i);
});
});
});
describe('BookPlayerPageGenerator()', function () {
var bppg, dlManager, dlManagerMock, chapterObjInstance, $test_div;
before(function () {
$test_div = $('<div></div>');
$test_div.appendTo('body');
dlManager = {
downloadBook: function (book_id, chapter_obj) {},
downloadChapter: function (book_obj) {}
};
dlManagerMock = sinon.mock(dlManager); // create wrapper object for dlManager
bookPlayerArgs = {
'bookDownloadManager': dlManager,
'selectors': {
'dlFullBook': '#downloadBook',
'dlChapter': '#downloadChapter',
'audioSource': '#audioSource',
}
};
bppg = new BookPlayerPageGenerator(bookPlayerArgs);
});
beforeEach(function () {
$test_div.empty();
$('<audio id="audioSource"></audio>').appendTo($test_div);
$('<button id="downloadChapter"></button>').appendTo($test_div);
$('<button id="downloadBook"></button>').appendTo($test_div);
});
beforeEach(function () {
chapterObjInstance = Object.create(CHAPTER_OBJECT); // prevents changes made to chapter obj from persisting between tests
});
describe('#generatePage()', function () {
it('generates download book and download chapter buttons that send messages to downloadManager object on click', function () {
bppg.generatePage({
book: BOOK_OBJECT,
chapter: chapterObjInstance
});
dlManagerMock.expects("downloadBook").once().withExactArgs(BOOK_OBJECT);
$('#downloadBook').trigger('click'); // should call dlManager's #downloadBook method
dlManagerMock.verify();
dlManagerMock.expects("downloadChapter").once().withExactArgs(BOOK_OBJECT.id, chapterObjInstance);
$('#downloadChapter').trigger('click');
dlManagerMock.verify();
});
it('sets the audio element\'s src property to the chapter audio url', function () {
bppg.generatePage({
book: BOOK_OBJECT,
chapter: chapterObjInstance
});
expect($('#audioSource').prop('src')).equal(chapterObjInstance.url);
});
/*it('updates chapter position property to match audio player position', function () {
bppg.generatePage({book: BOOK_OBJECT, chapter: chapterObjInstance});
$('#audioSource').currentTime = 10; // this doesn't work
expect(chapterObjInstance.position).equal('10'); // TODO research how to set audio src position
});*/
});
});
describe("BookDownloadManager()", function () {
var bdm;
var storageMock;
before(function () {
function StubHttpRequestHandler() {
this.getBlob = function (url, load_callback, other_args) {
httpReqUrl = url; // TODO: this is an ugly way to expose url argument
load_callback(testBlob);
} // simulate LibriVox JSON title search response
}
var storage = {
addNamed: function (blob, path) {}
}
storageMock = sinon.mock(storage);
bdm = new BookDownloadManager({
'httpRequestHandler': new StubHttpRequestHandler(),
'storageDevice': storage
});
});
/* describe('#downloadBook()', function () {
it('should download the specified book', function () {
storageMock.expects("addNamed").once().withExactArgs("librifox/1234/full.mp3");
bdm.downloadBook(BOOK_OBJECT);
storageMock.verify();
});
});
describe('#downloadChapter()', function () {
it('should download the specified chapter', function () {
storageMock.expects("addNamed").once().withArgs("librifox/1234/0.mp3");
bdm.downloadChapter(BOOK_OBJECT.id, CHAPTER_OBJECT);
storageMock.verify();
});
});
describe('#write()', function () {
it('should write the specified object to the filesystem', function () {
bdm.write({
size: 4001234,
type: "audio/mpeg"
}, "123/");
});
}); */
describe('#getBookFilePath()', function () {
it('should return the filepath of the book, based on id', function () {
bdm.getBookFilePath(BOOK_OBJECT);
});
});
describe('#getChapterFilePath()', function () {
it('should return the filepath of the chapter, based on id', function () {
bdm.getChapterFilePath(BOOK_OBJECT);
});
});
});