UNPKG

source-scraper-test-utils

Version:
51 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Tester_1 = require("./Tester"); const chai = require("chai"); require("mocha"); chai.should(); class ScraperTester extends Tester_1.Tester { constructor(scraper) { super(); this.scraper = scraper; } static fromStatic(runner) { return new ScraperTester(new runner()); } testUrlDetection(correctUrls, incorrectUrls) { return this.addTest('should detect a valid url', () => { correctUrls.forEach(url => this.scraper.isApplicable(url).should.be.true); if (incorrectUrls) incorrectUrls.forEach(url => this.scraper.isApplicable(url).should.be.false); }); } testScraping(urls, options) { return this.addTest('should scrap data from a test page', async () => { for (const url of urls) { const scrap = await this.scraper.scrap(url, options); scrap.should.have.property('success').that.is.true; scrap.should.have.property('data').that.is.an('object'); let data = scrap.data; if ('sources' in data) { data = data; data.should.have.property('sources').that.is.an('array'); data.sources.length.should.be.greaterThan(0); data.sources.forEach(s => s.should.have.property('url').that.is.a('string')); } else if ('hosters' in data) { data = data; data.should.have.property('hosters').that.is.an('array'); data.hosters.length.should.be.greaterThan(0); data.hosters.forEach(s => s.should.have.property('url').that.is.a('string')); } else throw new Error('Unexpected scraper type'); } }); } getTestTarget() { return this.scraper; } } exports.ScraperTester = ScraperTester; //# sourceMappingURL=ScraperTester.js.map