|
| 1 | +import should from 'should'; |
| 2 | +import '../../utils/assertions.js'; |
| 3 | +import nock from 'nock'; |
| 4 | +import fs from 'fs-extra'; |
| 5 | +import cheerio from 'cheerio'; |
| 6 | +import scrape from 'website-scraper'; |
| 7 | + |
| 8 | +const testDirname = './test/functional/binary-resources/.tmp'; |
| 9 | +const mockDirname = './test/functional/binary-resources/mocks'; |
| 10 | + |
| 11 | +describe('Functional: images', () => { |
| 12 | + const options = { |
| 13 | + urls: [ 'http://example.com/' ], |
| 14 | + directory: testDirname, |
| 15 | + subdirectories: [ |
| 16 | + { directory: 'img', extensions: ['.jpg', '.png'] } |
| 17 | + ], |
| 18 | + sources: [ |
| 19 | + { selector: 'img', attr: 'src' } |
| 20 | + ], |
| 21 | + ignoreErrors: false |
| 22 | + }; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + nock.cleanAll(); |
| 26 | + nock.disableNetConnect(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + nock.cleanAll(); |
| 31 | + nock.enableNetConnect(); |
| 32 | + fs.removeSync(testDirname); |
| 33 | + }); |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + // mock base urls |
| 37 | + nock('http://example.com/').get('/').replyWithFile(200, mockDirname + '/index.html', {'content-type': 'text/html'}); |
| 38 | + |
| 39 | + // mock sources for index.html |
| 40 | + nock('http://example.com/').get('/test-image.png').replyWithFile(200, mockDirname + '/test-image.png', {'content-type': 'image/png'}); |
| 41 | + nock('http://example.com/').get('/test-image.jpg').replyWithFile(200, mockDirname + '/test-image.jpg', {'content-type': 'image/jpeg'}); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should load images and save content correctly', async () => { |
| 45 | + await scrape(options); |
| 46 | + |
| 47 | + // should create directory and subdirectories |
| 48 | + fs.existsSync(testDirname).should.be.eql(true); |
| 49 | + fs.existsSync(testDirname + '/img').should.be.eql(true); |
| 50 | + |
| 51 | + // should contain all sources found in index.html |
| 52 | + fs.existsSync(testDirname + '/img/test-image.png').should.be.eql(true); |
| 53 | + fs.existsSync(testDirname + '/img/test-image.jpg').should.be.eql(true); |
| 54 | + |
| 55 | + // all sources in index.html should be replaced with local paths |
| 56 | + let $ = cheerio.load(fs.readFileSync(testDirname + '/index.html').toString()); |
| 57 | + $('img.png').attr('src').should.be.eql('img/test-image.png'); |
| 58 | + $('img.jpg').attr('src').should.be.eql('img/test-image.jpg'); |
| 59 | + |
| 60 | + // content of downloaded images should equal original images |
| 61 | + const originalPng = fs.readFileSync(mockDirname + '/test-image.png'); |
| 62 | + const originalJpg = fs.readFileSync(mockDirname + '/test-image.jpg'); |
| 63 | + const resultPng = fs.readFileSync(testDirname + '/img/test-image.png'); |
| 64 | + const resultJpg = fs.readFileSync(testDirname + '/img/test-image.jpg'); |
| 65 | + |
| 66 | + should(resultPng).be.eql(originalPng); |
| 67 | + should(resultJpg).be.eql(originalJpg); |
| 68 | + }); |
| 69 | +}); |
0 commit comments