jqueryOption.test.js
5.05 KB
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
/*jshint expr:true */
'use strict';
var Crawler = require('../lib/crawler');
var expect = require('chai').expect;
var given = require('mocha-testdata');
var jsdom = require('jsdom');
var c;
describe('Jquery testing', function() {
afterEach(function() {
c = {};
});
describe('Jquery parsing', function() {
given.async(jsdom, 'cheerio','whacko')
.it('should work on inline html', function(done, jquery) {
c = new Crawler();
c.queue([{
html: '<p><i>great!</i></p>',
jquery: jquery,
callback: function(error, res) //noinspection BadExpressionStatementJS,BadExpressionStatementJS
{
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
done();
}
}]);
});
});
describe('Jquery injection', function() {
it('should enable cheerio by default', function(done) {
c = new Crawler({
html: '<p><i>great!</i></p>',
jquery: true,
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
done();
}
});
c.queue([{html: '<p><i>great!</i></p>'}]);
});
given.async(jsdom).it('should enable jsdom if set', function(done, jquery) {
c = new Crawler({
jquery: jquery,
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
done();
}
});
c.queue([{
html: '<p><i>great!</i></p>',
}]);
});
given.async('cheerio', {name: 'cheerio'}).it('should enable cheerio if set', function(done, jquery) {
c = new Crawler({
jquery: jquery,
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
done();
}
});
c.queue([{ html: '<p><i>great!</i></p>'}]);
});
it('should enable whacko if set',function(done){
c = new Crawler({
jquery: 'whacko',
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
done();
}
});
c.queue([{html: '<p><i>great!</i></p>'}]);
});
it('should disable jQuery if set to false', function(done) {
c = new Crawler({
jQuery: false,
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$).to.be.undefined;
done();
}
});
c.queue([{html: '<p><i>great!</i></p>' }]);
});
given.async('trucmuch', null, undefined).it('should not inject jquery', function(done, jquery) {
c = new Crawler({
jquery: jquery,
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$).to.be.undefined;
done();
}
});
c.queue([{html: '<p><i>great!</i></p>' }]);
});
given.async('cheerio', jsdom).it('should also enable jQuery even if body is empty, to prevent `$ is not a function` error', function(done, jquery) {
c = new Crawler({
jQuery: jquery,
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
done();
}
});
c.queue([{ html: '<p><i>great!</i></p>'}]);
});
given.async('cheerio', jsdom).it('should disable jQuery if body is not text/html ', function(done, jquery) {
c = new Crawler({
jQuery: jquery,
callback:function(error, res) {
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
done();
}
});
c.queue([{html: '<p><i>great!</i></p>'}]);
});
it('should work if jquery is set instead of jQuery when building Crawler', function(done) {
c = new Crawler({
maxConnections: 10,
jquery: true,
callback: function(error, res,next) {
expect(res.$).not.to.be.undefined;
expect(res.options.jQuery).to.be.true;
expect(res.options.jquery).to.be.undefined;
next();
}
});
c.on('drain',done);
c.queue([{ html: '<p><i>great!</i></p>' }]);
});
it('should work if jquery is set instead of jQuery when queuing', function(done) {
c = new Crawler({
maxConnections: 10,
jQuery: true,
callback: function(error, res,next) {
expect(res.$).to.be.undefined;
expect(res.options.jQuery).to.be.false;
next();
}
});
c.on('drain',done);
c.queue([{
html: '<p><i>great!</i></p>',
jquery: false
}]);
});
it('should not inject jquery if jquery is set to undefined', function(done) {
c = new Crawler({
maxConnections: 10,
jquery: undefined,
callback: function(error, res,next) {
expect(res.$).to.be.undefined;
expect(res.options.jQuery).to.be.undefined;
next();
}
});
c.on('drain',done);
c.queue([{ html: '<p><i>great!</i></p>'}]);
});
});
describe('Cheerio specific test', function() {
it('should inject cheerio with options', function(done) {
var cheerioConf = {
name: 'cheerio',
options: {
normalizeWhitespace: true,
xmlMode: true
}
};
c = new Crawler({
maxConnections: 10,
jquery: cheerioConf,
callback: function(error, res,next) {
expect(error).to.be.null;
expect(res.$('i').html()).to.equal('great!');
next();
}
});
c.on('drain',done);
c.queue([{html: '<p><i>great!</i></p>'}]);
});
});
});