RenaiApp/src/main/modules/web-crawler/web-crawler.spec.ts

65 lines
1.6 KiB
TypeScript

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 } from '../../core/container';
import { Store } from '../store/store';
import { StoreMock } from '../store/store.mock';
import { IWebCrawler } from './i-web-crawler';
describe('Web Crawler', function () {
this.timeout(2000);
before(() => {
rewiremock.enable();
container.unbind(Symbol.for('store'));
container.bind(Symbol.for('store')).to(StoreMock);
});
beforeEach(() => {
if (!nock.isActive()) {
nock.activate();
}
});
afterEach(() => {
nock.cleanAll();
});
after(() => {
rewiremock.disable();
container.unbind(Symbol.for('store'));
container.bind(Symbol.for('store')).to(Store);
});
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');
});
});