Facebook-Events-iCal-Converter/test/services/dom-parser.spec.js

295 lines
7.7 KiB
JavaScript
Raw Permalink Normal View History

2020-07-14 18:20:49 +02:00
const { expect } = require('chai')
const parseUsingDOM = require('../../lib/services/dom-parser')
const MockLogger = require('../../mocks/logger.mock')
describe(parseUsingDOM, () => {
let logger
beforeEach(() => {
logger = new MockLogger()
})
describe('results', () => {
it('should return page title', () => {
const html = `
<html>
<head>
<title>Test</title>
</head>
</html>
`
2020-07-16 16:48:05 +02:00
const { title } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(title).to.equal('Test')
})
describe('time', () => {
it('should return start time', () => {
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" title="2020/3/2 at 13:30"></div></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const { start } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(start).to.deep.equal([ 2020, 3, 2, 13, 30 ])
})
it('should return current time if no time data is found', () => {
const now = new Date('2020-01-01 12:00:00')
const spy = jest
.spyOn(global, 'Date')
.mockImplementation(() => now)
const html = `
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="event_summary">
<div class="test_eventNode1"></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const { start } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
spy.mockRestore()
expect(start).to.deep.equal([ 2020, 1, 1, 12, 0 ])
})
2020-07-16 10:39:32 +02:00
it('should return duration of minimum 120 minutes if ' +
2020-07-14 18:20:49 +02:00
'no time data is found', () => {
const now = new Date('2020-01-01 12:00:00')
const spy = jest
.spyOn(global, 'Date')
.mockImplementation(() => now)
const html = `
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="event_summary">
<div class="test_eventNode1"></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const { duration } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
spy.mockRestore()
2020-07-16 10:39:32 +02:00
expect(duration).to.deep.equal({ minutes: 120 })
2020-07-14 18:20:49 +02:00
})
it('should return duration based on start time', () => {
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" title="2020/3/2 at 13:3015:30"></div></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const { duration } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(duration).to.deep.equal({ minutes: 120 })
})
})
describe('location', () => {
it('should return approximated location and area', () => {
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>
`
2020-07-16 16:48:05 +02:00
const { location } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(location).to.equal('123 Main St. AcmeTown, Main area')
})
it('should return only approximated location', () => {
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></td></tr></table></div></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const { location } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(location).to.equal('123 Main St. AcmeTown')
})
it('should return only approximated area', () => {
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></span><span>Some area</span></td></tr></table></div></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const { location } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(location).to.equal('Some area')
})
it('should NOT return location or area', () => {
const html = `
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="event_summary">
<div class="test_eventNode1"></div>
<div class="test_eventNode2"><div></div><div></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const { location } = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(location).to.equal('')
})
})
})
describe('logging', () => {
it('should log parsing', (callback) => {
logger.on('test:log', () => {
callback()
})
2020-07-16 16:48:05 +02:00
parseUsingDOM('', { logger })
2020-07-14 18:20:49 +02:00
})
it('should log with message', (callback) => {
logger.on('test:log', ({ message }) => {
expect(message).to.equal('Using fallback DOM parser')
callback()
})
2020-07-16 16:48:05 +02:00
parseUsingDOM('', { logger })
2020-07-14 18:20:49 +02:00
})
it('should log with log level', (callback) => {
logger.on('test:log', ({ level }) => {
expect(level).to.equal('info')
callback()
})
2020-07-16 16:48:05 +02:00
parseUsingDOM('', { logger })
2020-07-14 18:20:49 +02:00
})
it('should log with service description', (callback) => {
logger.on('test:log', ({ service }) => {
expect(service).to.equal('parser')
callback()
})
2020-07-16 16:48:05 +02:00
parseUsingDOM('', { logger })
2020-07-14 18:20:49 +02:00
})
})
describe('null results', () => {
it('should return null if no title is present in page', () => {
const html = `
<html>
<head>
<title></title>
</head>
</html>
`
2020-07-16 16:48:05 +02:00
const eventData = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(eventData).to.be.null
})
it('should return null if title was blacklisted', () => {
const html = `
<html>
<head>
<title>Content Not Found</title>
</head>
</html>
`
const eventData = parseUsingDOM(html, { logger })
expect(eventData).to.be.null
})
2020-07-14 18:20:49 +02:00
it('should NOT return start time without title', () => {
const html = `
<html>
<head>
<title></title>
</head>
<body>
<div id="event_summary">
<div class="test_eventNode1"></div>
<div class="test_eventNode2"><div class="test_timeNode" title="2020/3/2 at 13:30"></div></div>
</div>
</body>
</html>
`
2020-07-16 16:48:05 +02:00
const eventData = parseUsingDOM(html, { logger })
2020-07-14 18:20:49 +02:00
expect(eventData).to.be.null
})
})
})