From 10cffd83e7b3a27256c71317a098310b9597a59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Syn=C3=A1=C4=8Dek?= Date: Fri, 17 Jul 2020 23:11:07 +0200 Subject: [PATCH] fix invalid function signature for event data extraction Additionally fix specs --- lib/services/ics-retriever.js | 4 +- lib/static/index.js | 6 +- test/log-utils.spec.js | 25 +++++++++ test/services/ics-retriever.spec.js | 85 +++++++++++++++++++++++------ test/utils.spec.js | 22 -------- 5 files changed, 97 insertions(+), 45 deletions(-) create mode 100644 test/log-utils.spec.js diff --git a/lib/services/ics-retriever.js b/lib/services/ics-retriever.js index 6946b38..5092bbe 100644 --- a/lib/services/ics-retriever.js +++ b/lib/services/ics-retriever.js @@ -3,7 +3,7 @@ const parseUsingDOM = require('./dom-parser') const generateICS = require('./ics-generator') const { createParserError, getNormalizedUrl } = require('../utils') -const extractEventDataFromHTML = (html, { logger }) => { +const extractEventDataFromHTML = (html, url, { logger }) => { const LDJSONEventData = parseUsingLDJSONData(html, { logger }) const rawEventData = LDJSONEventData || parseUsingDOM(html, { logger }) @@ -23,7 +23,7 @@ const extractEventDataFromHTML = (html, { logger }) => { const retrieveICS = async (URLparameter, { logger, crawl }) => { const url = getNormalizedUrl(URLparameter) const html = await crawl(url, { logger }) - const eventData = extractEventDataFromHTML(html, { logger }) + const eventData = extractEventDataFromHTML(html, url, { logger }) const icsContent = await generateICS(eventData) return icsContent diff --git a/lib/static/index.js b/lib/static/index.js index 0f720b1..05f2835 100644 --- a/lib/static/index.js +++ b/lib/static/index.js @@ -236,11 +236,11 @@ import generateICS from '../../lib/services/ics-generator' hydrateList() - const handleHTMLResponse = (html) => { + const handleHTMLResponse = (html, url) => { try { setStatusParsing() - const eventData = extractEventDataFromHTML(html, { logger }) + const eventData = extractEventDataFromHTML(html, url, { logger }) generateICS(eventData) .then((text) => { const dataUri = encodeURIComponent(text) @@ -287,7 +287,7 @@ import generateICS from '../../lib/services/ics-generator' postURL(formData) .then((res) => { res.text() - .then(handleHTMLResponse) + .then((response) => handleHTMLResponse(response, input.value)) .catch((err) => { handleError(err) }) diff --git a/test/log-utils.spec.js b/test/log-utils.spec.js new file mode 100644 index 0000000..012910b --- /dev/null +++ b/test/log-utils.spec.js @@ -0,0 +1,25 @@ +const { expect } = require('chai') +const winston = require('winston') + +const logUtils = require('../lib/log-utils') + +describe('logUtils', () => { + it('should create console transport for logging in dev mode', () => { + expect(logUtils.createTransports(true)[0]) + .to.be.instanceOf(winston.transports.Console) + }) + + + it('should NOT create console transport for logging in undefined mode', () => { + const transports = logUtils.createTransports() + + expect(transports).to.have.length(1) + expect(transports[0]).to.be.instanceOf(winston.transports.DailyRotateFile) + }) + + + it('should create log rotate transport for logging in dev mode', () => { + expect(logUtils.createTransports(true)[1]) + .to.be.instanceOf(winston.transports.DailyRotateFile) + }) +}) diff --git a/test/services/ics-retriever.spec.js b/test/services/ics-retriever.spec.js index 720aacd..f8d6414 100644 --- a/test/services/ics-retriever.spec.js +++ b/test/services/ics-retriever.spec.js @@ -12,9 +12,7 @@ const { clearMockCrawlResult, } = require('../../mocks/crawler.mock') -const { retrieveICS } = require('../../lib/services/ics-retriever') - -jest.mock('../../lib/services/crawler', () => mockCrawl) +const { retrieveICS, extractEventDataFromHTML } = require('../../lib/services/ics-retriever') describe(retrieveICS, () => { let logger @@ -35,7 +33,10 @@ describe(retrieveICS, () => { setMockCrawlResult(html) - const icsContent = await retrieveICS('https://facebook.com/events/123', { logger }) + const icsContent = await retrieveICS('https://facebook.com/events/123', { + logger, + crawl: mockCrawl, + }) expect(icsContent).to.be.ok }) @@ -52,13 +53,35 @@ describe(retrieveICS, () => { setMockCrawlResult(html) - const icsContent = await retrieveICS('https://facebook.com/events/123', { logger }) + const icsContent = await retrieveICS('https://facebook.com/events/123', { + logger, + crawl: mockCrawl, + }) expect(icsContent).to.include('SUMMARY:Test Event') expect(icsContent).to.include('LOCATION:Location X') }) + it('should extract event data using LDJSON', () => { + const html = ` + + + + + + ` + + const { location } = extractEventDataFromHTML( + html, + 'https://facebook.com/events/123', { + logger, + }) + + expect(location).to.equal('Location X') + }) + + it('should use create ICS contents based on DOM', async () => { const html = ` @@ -76,13 +99,41 @@ describe(retrieveICS, () => { setMockCrawlResult(html) - const icsContent = await retrieveICS('https://facebook.com/events/123', { logger }) + const icsContent = await retrieveICS('https://facebook.com/events/123', { + logger, + crawl: mockCrawl, + }) expect(icsContent).to.include('LOCATION:123 Main St. AcmeTown, Main area') }) - it('should throw parser error if no event data is found', async (callback) => { + it('should extract event data based on DOM', () => { + const html = ` + + + Test + + +
+
+
123 Main St.\nAcmeTownMain area
+
+ + + ` + + const { location } = extractEventDataFromHTML( + html, + 'https://facebook.com/events/123', { + logger, + }) + + expect(location).to.equal('123 Main St. AcmeTown, Main area') + }) + + + it('should throw parser error if no event data is found', () => { const html = ` @@ -92,15 +143,13 @@ describe(retrieveICS, () => { ` - setMockCrawlResult(html) - - try { - const eventData = await retrieveICS('https://facebook.com/events/132', { logger }) - } catch (err) { - expect(err.toString()).to.include('Unable to parse event data.') - expect(err.statusCode).to.equal(422) - callback() - } + expect(() => { + extractEventDataFromHTML( + html, + 'https://facebook.com/events/132', { + logger, + }) + }).to.throw('Unable to parse event data.') }) @@ -117,8 +166,8 @@ describe(retrieveICS, () => { setMockCrawlResult(html) - const icsContent = await retrieveICS('123', { logger }) + const icsContent = await retrieveICS('123', { logger, crawl: mockCrawl }) expect(icsContent).to.include('URL:https://mobile.facebook.com/events/123') - }) }) + diff --git a/test/utils.spec.js b/test/utils.spec.js index a8e4247..f45f9cf 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -1,5 +1,4 @@ const { expect } = require('chai') -const winston = require('winston') const utils = require('../lib/utils') @@ -152,25 +151,4 @@ describe('utils', () => { .to.equal(422) }) }) - - describe('logging', () => { - it('should create console transport for logging in dev mode', () => { - expect(utils.createTransports(true)[0]) - .to.be.instanceOf(winston.transports.Console) - }) - - - it('should NOT create console transport for logging in undefined mode', () => { - const transports = utils.createTransports() - - expect(transports).to.have.length(1) - expect(transports[0]).to.be.instanceOf(winston.transports.DailyRotateFile) - }) - - - it('should create log rotate transport for logging in dev mode', () => { - expect(utils.createTransports(true)[1]) - .to.be.instanceOf(winston.transports.DailyRotateFile) - }) - }) })