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 = ` Test ` const { title } = parseUsingDOM(html, { logger }) expect(title).to.equal('Test') }) describe('time', () => { it('should return start time', () => { const html = ` Test
` const { start } = parseUsingDOM(html, { logger }) 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 = ` Test
` const { start } = parseUsingDOM(html, { logger }) spy.mockRestore() expect(start).to.deep.equal([ 2020, 1, 1, 12, 0 ]) }) it('should return duration of minimum 120 minutes 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 = ` Test
` const { duration } = parseUsingDOM(html, { logger }) spy.mockRestore() expect(duration).to.deep.equal({ minutes: 120 }) }) it('should return duration based on start time', () => { const html = ` Test
` const { duration } = parseUsingDOM(html, { logger }) expect(duration).to.deep.equal({ minutes: 120 }) }) }) describe('location', () => { it('should return approximated location and area', () => { const html = ` Test
123 Main St.\nAcmeTownMain area
` const { location } = parseUsingDOM(html, { logger }) expect(location).to.equal('123 Main St. AcmeTown, Main area') }) it('should return only approximated location', () => { const html = ` Test
123 Main St.\nAcmeTown
` const { location } = parseUsingDOM(html, { logger }) expect(location).to.equal('123 Main St. AcmeTown') }) it('should return only approximated area', () => { const html = ` Test
Some area
` const { location } = parseUsingDOM(html, { logger }) expect(location).to.equal('Some area') }) it('should NOT return location or area', () => { const html = ` Test
` const { location } = parseUsingDOM(html, { logger }) expect(location).to.equal('') }) }) }) describe('logging', () => { it('should log parsing', (callback) => { logger.on('test:log', () => { callback() }) parseUsingDOM('', { logger }) }) it('should log with message', (callback) => { logger.on('test:log', ({ message }) => { expect(message).to.equal('Using fallback DOM parser') callback() }) parseUsingDOM('', { logger }) }) it('should log with log level', (callback) => { logger.on('test:log', ({ level }) => { expect(level).to.equal('info') callback() }) parseUsingDOM('', { logger }) }) it('should log with service description', (callback) => { logger.on('test:log', ({ service }) => { expect(service).to.equal('parser') callback() }) parseUsingDOM('', { logger }) }) }) describe('null results', () => { it('should return null if no title is present in page', () => { const html = ` ` const eventData = parseUsingDOM(html, { logger }) expect(eventData).to.be.null }) it('should return null if title was blacklisted', () => { const html = ` Content Not Found ` const eventData = parseUsingDOM(html, { logger }) expect(eventData).to.be.null }) it('should NOT return start time without title', () => { const html = `
` const eventData = parseUsingDOM(html, { logger }) expect(eventData).to.be.null }) }) })