Skip to content

Commit 158f852

Browse files
committed
Cleanup tests (#62)
* Cleanup tests for html, css handlers and byType generator * Fix typo
1 parent 216425f commit 158f852

File tree

4 files changed

+89
-180
lines changed

4 files changed

+89
-180
lines changed

test/unit/file-handlers/css-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('Css handler', function () {
1616

1717
beforeEach(function() {
1818
scraper = new Scraper(defaultScraperOpts);
19-
scraper.prepare();
2019
});
2120

2221
describe('#loadCss(context, resource)', function() {

test/unit/file-handlers/html-test.js

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ var _ = require('lodash');
33
var Promise = require('bluebird');
44
var sinon = require('sinon');
55
require('sinon-as-promised')(Promise);
6-
var nock = require('nock');
7-
var fs = require('fs-extra');
86
var Scraper = require('../../../lib/scraper');
97
var Resource = require('../../../lib/resource');
108
var loadHtml = require('../../../lib/file-handlers/html');
119

12-
var testDirname = __dirname + '/.html-test';
1310
var defaultScraperOpts = {
1411
urls: [ 'http://example.com' ],
15-
directory: testDirname,
12+
directory: __dirname + '/.html-test',
1613
sources: [
1714
{ selector: 'img', attr: 'src' },
1815
{ selector: 'img', attr: 'srcset' },
@@ -26,16 +23,7 @@ var scraper;
2623
describe('Html handler', function () {
2724

2825
beforeEach(function() {
29-
nock.cleanAll();
30-
nock.disableNetConnect();
3126
scraper = new Scraper(defaultScraperOpts);
32-
scraper.prepare();
33-
});
34-
35-
afterEach(function() {
36-
nock.cleanAll();
37-
nock.enableNetConnect();
38-
fs.removeSync(testDirname);
3927
});
4028

4129
describe('#loadHtml(context, resource)', function() {
@@ -96,21 +84,19 @@ describe('Html handler', function () {
9684
});
9785

9886
it('should not call loadResource if no sources in html', function(done) {
99-
var loadResourceSpy = sinon.spy(scraper, 'loadResource');
87+
var loadResourceStub = sinon.stub(scraper, 'loadResource').resolves();
10088

10189
var po = new Resource('http://example.com', 'index.html');
10290
po.setText('');
10391

10492
loadHtml(scraper, po).then(function() {
105-
loadResourceSpy.called.should.be.eql(false);
93+
loadResourceStub.called.should.be.eql(false);
10694
done();
10795
}).catch(done);
10896
});
10997

11098
it('should not call loadResource if source attr is empty', function(done) {
111-
nock('http://example.com').get('/test.png').reply(200, 'OK');
112-
113-
var loadResourceSpy = sinon.spy(scraper, 'loadResource');
99+
var loadResourceStub = sinon.stub(scraper, 'loadResource').resolves();
114100

115101
var html = ' \
116102
<html lang="en"> \
@@ -123,15 +109,13 @@ describe('Html handler', function () {
123109
po.setText(html);
124110

125111
loadHtml(scraper, po).then(function() {
126-
loadResourceSpy.called.should.be.eql(false);
112+
loadResourceStub.called.should.be.eql(false);
127113
done();
128114
}).catch(done);
129115
});
130116

131117
it('should call loadResource once with correct params', function(done) {
132-
nock('http://example.com').get('/test.png').reply(200, 'OK');
133-
134-
var loadResourceSpy = sinon.spy(scraper, 'loadResource');
118+
var loadResourceStub = sinon.stub(scraper, 'loadResource').resolves();
135119

136120
var html = ' \
137121
<html lang="en"> \
@@ -144,18 +128,18 @@ describe('Html handler', function () {
144128
po.setText(html);
145129

146130
loadHtml(scraper, po).then(function() {
147-
loadResourceSpy.calledOnce.should.be.eql(true);
148-
loadResourceSpy.args[0][0].url.should.be.eql('http://example.com/test.png');
131+
loadResourceStub.calledOnce.should.be.eql(true);
132+
loadResourceStub.args[0][0].url.should.be.eql('http://example.com/test.png');
149133
done();
150134
}).catch(done);
151135
});
152136

153137
it('should call loadResource for each found source with correct params', function(done) {
154-
nock('http://example.com').get('/a.jpg').reply(200, 'OK');
155-
nock('http://example.com').get('/b.css').reply(200, 'OK');
156-
nock('http://example.com').get('/c.js').reply(200, 'OK');
138+
var loadResourceStub = sinon.stub(scraper, 'loadResource');
139+
loadResourceStub.onFirstCall().resolves(new Resource('http://example.com/a.jpg', 'a.jpg'));
140+
loadResourceStub.onSecondCall().resolves(new Resource('http://example.com/b.css', 'b.css'));
141+
loadResourceStub.onThirdCall().resolves(new Resource('http://example.com/c.js', 'c.js'));
157142

158-
var loadResourceSpy = sinon.spy(scraper, 'loadResource');
159143
var html = '\
160144
<html> \
161145
<head> \
@@ -174,19 +158,17 @@ describe('Html handler', function () {
174158

175159
// order of loading is determined by order of sources in scraper options
176160
loadHtml(scraper, parentResource).then(function() {
177-
loadResourceSpy.calledThrice.should.be.eql(true);
178-
loadResourceSpy.args[0][0].url.should.be.eql('http://example.com/a.jpg');
179-
loadResourceSpy.args[1][0].url.should.be.eql('http://example.com/b.css');
180-
loadResourceSpy.args[2][0].url.should.be.eql('http://example.com/c.js');
161+
loadResourceStub.calledThrice.should.be.eql(true);
162+
loadResourceStub.args[0][0].url.should.be.eql('http://example.com/a.jpg');
163+
loadResourceStub.args[1][0].url.should.be.eql('http://example.com/b.css');
164+
loadResourceStub.args[2][0].url.should.be.eql('http://example.com/c.js');
181165

182166
updateChildSpy.calledThrice.should.be.eql(true);
183167
done();
184168
}).catch(done);
185169
});
186170

187171
it('should not replace the sources in text, for which loadResource returned null', function(done) {
188-
nock('http://example.com').get('/scripts/c.js').once().reply(200, 'OK');
189-
190172
var loadStub = sinon.stub(scraper, 'loadResource');
191173
loadStub.onFirstCall().returns(Promise.resolve(null));
192174
loadStub.onSecondCall().returns(Promise.resolve(null));
@@ -225,10 +207,6 @@ describe('Html handler', function () {
225207
});
226208

227209
it('should replace all sources in text with local files', function(done) {
228-
nock('http://example.com').get('/public/img/a.jpg').reply(200, 'OK');
229-
nock('http://example.com').get('/b.css').reply(200, 'OK');
230-
nock('http://example.com').get('/scripts/c.js').once().reply(200, 'OK');
231-
232210
var loadStub = sinon.stub(scraper, 'loadResource');
233211
loadStub.onFirstCall().returns(Promise.resolve(new Resource('http://example.com/public/img/a.jpg', 'local/a.jpg')));
234212
loadStub.onSecondCall().returns(Promise.resolve(new Resource('http://example.com/b.css', 'local/b.css')));
@@ -262,8 +240,6 @@ describe('Html handler', function () {
262240
});
263241

264242
it('should keep hash in url for html resources', function (done) {
265-
nock('http://example.com').get('/page1.html').reply(200, 'OK');
266-
267243
var resourceStub = new Resource('http://example.com/page1.html', 'local/page1.html');
268244
sinon.stub(resourceStub, 'getType').returns('html');
269245
sinon.stub(scraper, 'loadResource').returns(Promise.resolve(resourceStub));
@@ -287,8 +263,6 @@ describe('Html handler', function () {
287263
});
288264

289265
it('should remove hash from url for not-html resources', function (done) {
290-
nock('http://example.com').get('/page1.html').reply(200, 'OK');
291-
292266
var resourceStub = new Resource('http://example.com/page1.html', 'local/page1.html');
293267
sinon.stub(resourceStub, 'getType').returns('other');
294268
sinon.stub(scraper, 'loadResource').returns(Promise.resolve(resourceStub));
@@ -431,7 +405,6 @@ describe('Html handler', function () {
431405
defaultFilename: 'index.html',
432406
prettifyUrls: true
433407
}, defaultScraperOpts));
434-
scraper.prepare();
435408

436409
var loadStub = sinon.stub(scraper, 'loadResource');
437410
loadStub.onFirstCall().returns(Promise.resolve(new Resource('http://example.com/other-page/index.html', 'other-page/index.html')));

test/unit/file-name-generators/by-site-structure-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var bySiteStructureFilenameGenerator = require('../../../lib/filename-generators
44

55
var options = { defaultFilename: 'index.html' };
66

7-
describe('byTypeFilenameGenerator', function() {
7+
describe('byStructureFilenameGenerator', function() {
88
it('should return the normalized absolute path of the resource url', function(){
99
var r1 = new Resource('http://example.com/some/path/a.png');
1010
bySiteStructureFilenameGenerator(r1, options).should.equal('/some/path/a.png');
@@ -41,5 +41,5 @@ describe('byTypeFilenameGenerator', function() {
4141
it('should normalize to safe absolute paths, without ..', function(){
4242
var r = new Resource('http://example.com/some/path/../../../../images/a.png');
4343
bySiteStructureFilenameGenerator(r, options).should.equal('/images/a.png');
44-
})
44+
});
4545
});

0 commit comments

Comments
 (0)