diff --git a/test/services/dom-parser.spec.js b/test/services/dom-parser.spec.js new file mode 100644 index 0000000..bff476e --- /dev/null +++ b/test/services/dom-parser.spec.js @@ -0,0 +1,294 @@ +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, 'abc.xyz', { logger }) + + expect(title).to.equal('Test') + }) + + + describe('time', () => { + it('should return start time', () => { + const html = ` + + + Test + + +
+
+
+
+ + + ` + const { start } = parseUsingDOM(html, 'abc.xyz', { 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, 'abc.xyz', { logger }) + + spy.mockRestore() + + expect(start).to.deep.equal([ 2020, 1, 1, 12, 0 ]) + }) + + + it('should return duration based on 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 { duration } = parseUsingDOM(html, 'abc.xyz', { logger }) + + spy.mockRestore() + + expect(duration).to.deep.equal({ minutes: 0 }) + }) + + + + + it('should return duration based on start time', () => { + const html = ` + + + Test + + +
+
+
+
+ + + ` + const { duration } = parseUsingDOM(html, 'abc.xyz', { logger }) + + expect(duration).to.deep.equal({ minutes: 120 }) + }) + }) + + it('should return passed in url', () => { + const html = ` + + + Test + + + ` + const { url } = parseUsingDOM(html, 'abc.xyz', { logger }) + + expect(url).to.equal('abc.xyz') + }) + + + describe('location', () => { + it('should return approximated location and area', () => { + const html = ` + + + Test + + +
+
+
123 Main St.\nAcmeTownMain area
+
+ + + ` + const { location } = parseUsingDOM(html, 'abc.xyz', { 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, 'abc.xyz', { logger }) + + expect(location).to.equal('123 Main St. AcmeTown') + }) + + + it('should return only approximated area', () => { + const html = ` + + + Test + + +
+
+
Some area
+
+ + + ` + const { location } = parseUsingDOM(html, 'abc.xyz', { logger }) + + expect(location).to.equal('Some area') + }) + + + it('should NOT return location or area', () => { + const html = ` + + + Test + + +
+
+
+
+ + + ` + const { location } = parseUsingDOM(html, 'abc.xyz', { 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, 'abc.xyz', { logger }) + + expect(eventData).to.be.null + }) + + + it('should NOT return start time without title', () => { + const html = ` + + + + + +
+
+
+
+ + + ` + const eventData = parseUsingDOM(html, 'abc.xyz', { logger }) + + expect(eventData).to.be.null + }) + }) +})