import rewiremock from 'rewiremock'; import '../../../../mocks/electron'; import { expect } from 'chai'; import 'mocha'; import nock from 'nock'; import { Response } from 'node-fetch'; import sinon from 'sinon'; import { container, mockStore } from '../../core/container'; import { IWebCrawler } from './i-web-crawler'; describe('Web Crawler', function () { this.timeout(2000); before(() => { rewiremock.enable(); mockStore(); }); beforeEach(() => { if (!nock.isActive()) { nock.activate(); } }); afterEach(() => { nock.cleanAll(); }); after(() => { rewiremock.disable(); mockStore(true); }); 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(); const webCrawler: IWebCrawler = container.get(Symbol.for('web-crawler')); const res: Response = await webCrawler.fetch(testUrl); expect(callback.callCount).to.equal(1, 'multiple requests (or none) are sent when only one should be'); const json = (await res.json()) as unknown; expect(json).to.deep.equal([{ id: 12, comment: 'Hey there' }], 'response body is incorrect'); }); });