mirror of
https://github.com/comatory/fb2iCal
synced 2025-01-30 17:04:48 +01:00
fix invalid function signature for event data extraction
Additionally fix specs
This commit is contained in:
parent
f14a57aa22
commit
a661e54524
@ -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
|
||||
|
@ -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)
|
||||
})
|
||||
|
25
test/log-utils.spec.js
Normal file
25
test/log-utils.spec.js
Normal file
@ -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)
|
||||
})
|
||||
})
|
@ -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 = `
|
||||
<html>
|
||||
<head>
|
||||
<script type="application/ld+json">{"name":"Test Event","location":{"name":"Location X"}}</script>
|
||||
</head>
|
||||
</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 = `
|
||||
<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 = `
|
||||
<html>
|
||||
<head>
|
||||
<title>Test</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="event_summary">
|
||||
<div class="test_eventNode1"></div>
|
||||
<div class="test_eventNode2"><div class="test_timeNode"></div><div class="test_locationNode"><table><tr><td><span></span><span>123 Main St.\nAcmeTown</span><span>Main area</span></td></tr></table></div></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
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 = `
|
||||
<html>
|
||||
<head>
|
||||
@ -92,15 +143,13 @@ describe(retrieveICS, () => {
|
||||
</html>
|
||||
`
|
||||
|
||||
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')
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user