2019-11-18 22:38:51 +01:00
|
|
|
import rewiremock from 'rewiremock';
|
2020-02-08 23:26:57 +01:00
|
|
|
import '../../../../mocks/electron';
|
2019-11-18 22:38:51 +01:00
|
|
|
|
2019-10-10 23:07:33 +02:00
|
|
|
import { expect } from 'chai';
|
2019-11-18 22:38:51 +01:00
|
|
|
import { CookieJar } from 'jsdom';
|
|
|
|
import 'mocha';
|
2019-10-10 23:07:33 +02:00
|
|
|
import nock from 'nock';
|
|
|
|
import { Response } from 'node-fetch';
|
|
|
|
import sinon from 'sinon';
|
2020-02-08 23:26:57 +01:00
|
|
|
import { WebCrawler } from './web-crawler';
|
|
|
|
import { storeMock } from '../store/store.mock';
|
2019-10-10 23:07:33 +02:00
|
|
|
|
2020-04-10 05:12:14 +02:00
|
|
|
describe('Web Crawler', function () {
|
2019-10-11 22:14:30 +02:00
|
|
|
this.timeout(2000);
|
2019-10-10 23:07:33 +02:00
|
|
|
|
2019-11-18 22:38:51 +01:00
|
|
|
before(() => {
|
|
|
|
rewiremock.enable();
|
|
|
|
|
2019-12-15 01:14:39 +01:00
|
|
|
storeMock.mock.load(() => Promise.resolve(new CookieJar().serializeSync()));
|
2019-11-18 22:38:51 +01:00
|
|
|
|
2019-12-15 01:14:39 +01:00
|
|
|
storeMock.mock.save(() => Promise.resolve());
|
2019-11-18 22:38:51 +01:00
|
|
|
});
|
|
|
|
|
2019-10-10 23:07:33 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
if (!nock.isActive()) {
|
|
|
|
nock.activate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
nock.cleanAll();
|
|
|
|
});
|
|
|
|
|
2019-11-18 22:38:51 +01:00
|
|
|
after(() => {
|
|
|
|
rewiremock.disable();
|
|
|
|
storeMock.restore();
|
|
|
|
});
|
|
|
|
|
2019-10-10 23:07:33 +02:00
|
|
|
it('fetches websites', async () => {
|
|
|
|
const callback = sinon.spy();
|
|
|
|
|
|
|
|
const testUrl = 'https://example.com';
|
|
|
|
nock(testUrl)
|
|
|
|
.get(/.*/)
|
|
|
|
.reply(
|
|
|
|
HttpCode.OK,
|
|
|
|
() => {
|
|
|
|
callback();
|
|
|
|
return JSON.stringify([{ id: 12, comment: 'Hey there' }]);
|
|
|
|
},
|
|
|
|
{ 'Content-Type': 'application/json' }
|
|
|
|
)
|
|
|
|
.persist();
|
|
|
|
|
2020-02-08 23:26:57 +01:00
|
|
|
const webCrawler = new WebCrawler();
|
|
|
|
|
|
|
|
const res: Response = await webCrawler.fetch(testUrl);
|
2019-10-10 23:07:33 +02:00
|
|
|
expect(callback.callCount).to.equal(1, 'multiple requests (or none) are sent when only one should be');
|
|
|
|
const json = await res.json();
|
|
|
|
expect(json).to.deep.equal([{ id: 12, comment: 'Hey there' }], 'response body is incorrect');
|
|
|
|
});
|
|
|
|
});
|