add specs for ICS retriever

This commit is contained in:
Ondřej Synáček 2020-07-16 16:37:20 +02:00
parent 8458ae0b69
commit 456eaa1fbc
3 changed files with 143 additions and 14 deletions

View File

@ -5,22 +5,18 @@ const generateICS = require('./ics-generator')
const { createParserError, getNormalizedUrl } = require('../utils')
const retrieveICS = async (URLparameter, { logger }) => {
try {
const url = getNormalizedUrl(URLparameter)
const html = await crawl(url, { logger })
const LDJSONEventData = parseUsingLDJSONData(html, { logger })
const eventData = LDJSONEventData || parseUsingDOM(html, url, { logger })
const url = getNormalizedUrl(URLparameter)
const html = await crawl(url, { logger })
const LDJSONEventData = parseUsingLDJSONData(html, { logger })
const eventData = LDJSONEventData || parseUsingDOM(html, url, { logger })
if (!eventData) {
throw createParserError()
return
}
const icsFile = await generateICS(eventData)
return icsFile
} catch (err) {
throw err
if (!eventData) {
throw createParserError()
return
}
const icsContent = await generateICS(eventData)
return icsContent
}
module.exports = retrieveICS

28
mocks/crawler.mock.js Normal file
View File

@ -0,0 +1,28 @@
let mockCrawlResult = null
const mockCrawl = (url, { logger }) => {
return mockCrawlResult
}
const setMockCrawlResult = (result) => {
mockCrawlResult = new Promise((resolve, reject) => {
resolve(result)
})
}
const setMockCrawlErrorResult = (error) => {
mockCrawlResult = new Promise((resolve, reject) => {
reject(error)
})
}
const clearMockCrawlResult = () => {
mockCrawlResult = null
}
module.exports = {
mockCrawl,
setMockCrawlResult,
setMockCrawlErrorResult,
clearMockCrawlResult,
}

View File

@ -0,0 +1,105 @@
const chai = require('chai')
const { expect } = chai
const chaiSinon = require('chai-sinon')
chai.use(chaiSinon)
const MockLogger = require('../../mocks/logger.mock')
const {
mockCrawl,
setMockCrawlResult,
setMockCrawlErrorResult,
clearMockCrawlResult,
} = require('../../mocks/crawler.mock')
const retrieveICS = require('../../lib/services/ics-retriever')
jest.mock('../../lib/services/crawler', () => mockCrawl)
describe(retrieveICS, () => {
let logger
beforeEach(() => {
logger = new MockLogger()
clearMockCrawlResult()
})
it('should use create ICS contents', async () => {
const html = `
<html>
<head>
<script type="application/ld+json">{"name":"Test Event"}</script>
</head>
</html>
`
setMockCrawlResult(html)
const icsContent = await retrieveICS('https://facebook.com/events/123', { logger })
expect(icsContent).to.be.ok
})
it('should use create ICS contents based on LDJSON', async () => {
const html = `
<html>
<head>
<script type="application/ld+json">{"name":"Test Event","location":{"name":"Location X"}}</script>
</head>
</html>
`
setMockCrawlResult(html)
const icsContent = await retrieveICS('https://facebook.com/events/123', { logger })
expect(icsContent).to.include('SUMMARY:Test Event')
expect(icsContent).to.include('LOCATION:Location X')
})
it('should use create ICS contents based on DOM', async () => {
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>
`
setMockCrawlResult(html)
const icsContent = await retrieveICS('https://facebook.com/events/123', { logger })
expect(icsContent).to.include('LOCATION:123 Main St. AcmeTown, Main area')
})
it('should throw parser error if no event data is found', async (callback) => {
const html = `
<html>
<head>
</head>
<body>
</body>
</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()
}
})
})