Add unit tests for search for books with digits in the name. See https://bugs.edge...
[bookreader.git] / BookReaderIA / test / unit / Search.js
1 // Tests for Search
2
3 // $$$ TODO -- make the test host configurable/automagic
4 // $$$ Refactor common code across tests
5
6 module("Search");
7
8 // $$$ set to test host
9 //var gTestAccount = 'testflip';
10 var gTestAccount = 'mang'; // XXX
11 var gTestHost = 'http://www-' + gTestAccount + '.archive.org';
12
13 // Holds search text
14 var gSearchText = '';
15
16 test("Hello test", function() {
17     expect(1);
18     equals("Hello world", "Hello world", "Hi there");
19 });
20
21 // Returns locator URL for the given id
22 function jsLocateURL(identifier, book) {
23     var bookURL = gTestHost + '/bookreader/BookReaderJSLocate.php?id=' + identifier;
24     if (book) {
25         bookURL += '&book=' + book;
26     }
27     return bookURL;
28 }
29
30 // Build search URL
31 function searchURL(bookReader, term, callback) {
32     var url = 'http://' + bookReader.server
33                + '/BookReader/flipbook_search_br.php?url='+escape(bookReader.bookPath + '_djvu.xml')
34                +'&term='+escape(term)+'&format=XML&callback=' + callback;
35     return url;
36 }
37
38 // Search, results to xmlCallback(txtData, textstatus) or errorCallback
39 function search(searchURL, xmlCallback, errorCallback) {
40     $.ajax({
41         type: 'GET',
42         dataType: 'script',
43         url: searchURL,
44         success: xmlCallback,
45         error: errorCallback
46     }); 
47 }
48
49 function setSearchText(txtData) {
50     gSearchText = txtData;
51 }
52
53 // Set up dummy BookReader class for JSLocate
54 function BookReader() {
55 };
56
57 BookReader.prototype.init = function() {
58     return true;
59 };
60
61 asyncTest("JSLocate for TheZenithYearbook1993HighPointUniversity", function() {
62     expect(2);
63     var locateURL = jsLocateURL('TheZenithYearbook1993HighPointUniversity', 'THE_ZENITH_1993');
64     
65     $.ajax({
66         url: locateURL,
67         dataType: 'script',
68         
69         complete: function() {
70             try {
71                 equals(typeof(br) != 'undefined', true, 'br is not undefined');
72                 equals(br.bookTitle, 'The Zenith Yearbook, 1993 High Point University', 'Title');
73             } catch (e) {
74             }
75             start();
76         }
77     });
78 });
79
80 asyncTest("XML search results for cassidy", function() {
81     expect(1);
82     gSearchText = '';
83     var searchDataURL = searchURL(br, 'cassidy', 'setSearchText');
84     
85     $.ajax({
86         type: 'GET',
87         url: searchDataURL,
88         dataType: 'script',
89         
90         success: function() {
91         },
92         
93         complete: function() {
94             var expected = '<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/css" href="blank.css"?><SEARCH><PAGE file="THE_ZENITH_1993_0021.djvu" width="2700" height="3451"><CONTEXT> Carr Mike </CONTEXT><WORD coords="1350,2937,1493,2903,2932">Cassidy</WORD><CONTEXT>  Mhari Cattell  Seniors </CONTEXT></PAGE></SEARCH>';
95             equals(gSearchText, expected, "XML search results");
96             start();
97         }
98     });
99 });
100